Skip to content

Commit

Permalink
8343485: Default implementations for NavigableMap
Browse files Browse the repository at this point in the history
  • Loading branch information
liach committed Nov 4, 2024
1 parent c125178 commit f8b120f
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 67 deletions.
64 changes: 60 additions & 4 deletions src/java.base/share/classes/java/util/NavigableMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,15 @@ public interface NavigableMap<K,V> extends SortedMap<K,V> {
* {@code removeAll}, {@code retainAll}, and {@code clear} operations.
* It does not support the {@code add} or {@code addAll} operations.
*
* @implSpec
* This implementation calls {@code descendingMap().navigableKeySet()} and
* returns its result.
*
* @return a reverse order navigable set view of the keys in this map
*/
NavigableSet<K> descendingKeySet();
default NavigableSet<K> descendingKeySet() {
return descendingMap().navigableKeySet();
}

/**
* Returns a view of the portion of this map whose keys range from
Expand Down Expand Up @@ -410,33 +416,54 @@ NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
*
* <p>Equivalent to {@code subMap(fromKey, true, toKey, false)}.
*
* @implSpec
* This default implementation calls {@code
* subMap(fromKey, true, toKey, false)} and returns its result.
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
SortedMap<K,V> subMap(K fromKey, K toKey);
@Override
default SortedMap<K,V> subMap(K fromKey, K toKey) {
return subMap(fromKey, true, toKey, false);
}

/**
* {@inheritDoc}
*
* <p>Equivalent to {@code headMap(toKey, false)}.
*
* @implSpec
* This default implementation calls {@code
* headMap(toKey, false)} and returns its result.
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
SortedMap<K,V> headMap(K toKey);
@Override
default SortedMap<K,V> headMap(K toKey) {
return headMap(toKey, false);
}

/**
* {@inheritDoc}
*
* <p>Equivalent to {@code tailMap(fromKey, true)}.
*
* @implSpec
* This default implementation calls {@code
* tailMap(fromKey, true)} and returns its result.
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
SortedMap<K,V> tailMap(K fromKey);
@Override
default SortedMap<K,V> tailMap(K fromKey) {
return tailMap(fromKey, true);
}

/**
* {@inheritDoc}
Expand All @@ -453,4 +480,33 @@ NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
default NavigableMap<K, V> reversed() {
return this.descendingMap();
}

/**
* {@inheritDoc}
*
* @implSpec
* The implementation in this interface returns the result of calling the
* {@code navigableKeySet} method.
*
* @return {@inheritDoc}
* @since 21
*/
@Override
default SequencedSet<K> sequencedKeySet() {
return navigableKeySet();
}

/**
* {@inheritDoc}
*
* @implSpec
* The implementation in this interface returns the result of calling the
* {@code navigableKeySet} method.
*
* @return {@inheritDoc}
*/
@Override
default Set<K> keySet() {
return navigableKeySet();
}
}
34 changes: 28 additions & 6 deletions src/java.base/share/classes/java/util/NavigableSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,16 @@ public interface NavigableSet<E> extends SortedSet<E> {
NavigableSet<E> descendingSet();

/**
* Returns an iterator over the elements in this set, in descending order.
* {@return an iterator over the elements in this set, in descending order}
* Equivalent in effect to {@code descendingSet().iterator()}.
*
* @return an iterator over the elements in this set, in descending order
* @implSpec
* This implementation calls {@code descendingSet().iterator()} and returns
* its result.
*/
Iterator<E> descendingIterator();
default Iterator<E> descendingIterator() {
return descendingSet().iterator();
}

/**
* Returns a view of the portion of this set whose elements range from
Expand Down Expand Up @@ -299,33 +303,51 @@ NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
*
* <p>Equivalent to {@code subSet(fromElement, true, toElement, false)}.
*
* @implSpec
* This implementation calls {@code
* subSet(fromElement, true, toElement, false)} and returns its result.
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
SortedSet<E> subSet(E fromElement, E toElement);
default SortedSet<E> subSet(E fromElement, E toElement) {
return subSet(fromElement, true, toElement, false);
}

/**
* {@inheritDoc}
*
* <p>Equivalent to {@code headSet(toElement, false)}.
*
* @implSpec
* This implementation calls {@code headSet(toElement, false)} and returns
* its result.
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
SortedSet<E> headSet(E toElement);
default SortedSet<E> headSet(E toElement) {
return headSet(toElement, false);
}

/**
* {@inheritDoc}
*
* <p>Equivalent to {@code tailSet(fromElement, true)}.
*
* @implSpec
* This implementation calls {@code tailSet(fromElement, true)} and returns
* its result.
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
SortedSet<E> tailSet(E fromElement);
default SortedSet<E> tailSet(E fromElement) {
return tailSet(fromElement, true);
}

/**
* {@inheritDoc}
Expand Down
27 changes: 1 addition & 26 deletions src/java.base/share/classes/java/util/TreeMap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -1480,15 +1480,6 @@ public NavigableSet<E> headSet(E toElement, boolean inclusive) {
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
return new KeySet<>(m.tailMap(fromElement, inclusive));
}
public SortedSet<E> subSet(E fromElement, E toElement) {
return subSet(fromElement, true, toElement, false);
}
public SortedSet<E> headSet(E toElement) {
return headSet(toElement, false);
}
public SortedSet<E> tailSet(E fromElement) {
return tailSet(fromElement, true);
}
public NavigableSet<E> descendingSet() {
return new KeySet<>(m.descendingMap());
}
Expand Down Expand Up @@ -1957,22 +1948,6 @@ public final Set<K> keySet() {
return navigableKeySet();
}

public NavigableSet<K> descendingKeySet() {
return descendingMap().navigableKeySet();
}

public final SortedMap<K,V> subMap(K fromKey, K toKey) {
return subMap(fromKey, true, toKey, false);
}

public final SortedMap<K,V> headMap(K toKey) {
return headMap(toKey, false);
}

public final SortedMap<K,V> tailMap(K fromKey) {
return tailMap(fromKey, true);
}

// View classes

abstract class EntrySetView extends AbstractSet<Map.Entry<K,V>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,37 @@ ConcurrentNavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
ConcurrentNavigableMap<K,V> tailMap(K fromKey, boolean inclusive);

/**
* @implSpec {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
ConcurrentNavigableMap<K,V> subMap(K fromKey, K toKey);
@Override
default ConcurrentNavigableMap<K,V> subMap(K fromKey, K toKey) {
return subMap(fromKey, true, toKey, false);
}

/**
* @implSpec {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
ConcurrentNavigableMap<K,V> headMap(K toKey);
@Override
default ConcurrentNavigableMap<K,V> headMap(K toKey) {
return headMap(toKey, false);
}

/**
* @implSpec {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
ConcurrentNavigableMap<K,V> tailMap(K fromKey);
@Override
default ConcurrentNavigableMap<K,V> tailMap(K fromKey) {
return tailMap(fromKey, true);
}

/**
* Returns a reverse order view of the mappings contained in this map.
Expand Down Expand Up @@ -145,9 +157,13 @@ ConcurrentNavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
*
* <p>This method is equivalent to method {@code navigableKeySet}.
*
* @implSpec {@inheritDoc}
* @return a navigable set view of the keys in this map
*/
NavigableSet<K> keySet();
@Override
default NavigableSet<K> keySet() {
return navigableKeySet();
}

/**
* Returns a reverse order {@link NavigableSet} view of the keys contained in this map.
Expand All @@ -163,7 +179,11 @@ ConcurrentNavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
* <p>The view's iterators and spliterators are
* <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
*
* @implSpec {@inheritDoc}
* @return a reverse order navigable set view of the keys in this map
*/
NavigableSet<K> descendingKeySet();
@Override
default NavigableSet<K> descendingKeySet() {
return descendingMap().keySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1689,10 +1689,6 @@ public ConcurrentNavigableMap<K,V> descendingMap() {
new SubMap<K,V>(this, null, false, null, false, true);
}

public NavigableSet<K> descendingKeySet() {
return descendingMap().navigableKeySet();
}

/* ---------------- AbstractMap Overrides -------------- */

/**
Expand Down Expand Up @@ -2237,9 +2233,6 @@ public boolean equals(Object o) {
}
public Object[] toArray() { return toList(this).toArray(); }
public <T> T[] toArray(T[] a) { return toList(this).toArray(a); }
public Iterator<K> descendingIterator() {
return descendingSet().iterator();
}
public NavigableSet<K> subSet(K fromElement,
boolean fromInclusive,
K toElement,
Expand Down Expand Up @@ -2794,18 +2787,6 @@ public SubMap<K,V> tailMap(K fromKey, boolean inclusive) {
return newSubMap(fromKey, inclusive, null, false);
}

public SubMap<K,V> subMap(K fromKey, K toKey) {
return subMap(fromKey, true, toKey, false);
}

public SubMap<K,V> headMap(K toKey) {
return headMap(toKey, false);
}

public SubMap<K,V> tailMap(K fromKey) {
return tailMap(fromKey, true);
}

public SubMap<K,V> descendingMap() {
return new SubMap<K,V>(m, lo, loInclusive,
hi, hiInclusive, !isDescending);
Expand Down Expand Up @@ -2872,9 +2853,7 @@ public Map.Entry<K,V> pollLastEntry() {
/* ---------------- Submap Views -------------- */

public NavigableSet<K> keySet() {
KeySet<K,V> ks;
if ((ks = keySetView) != null) return ks;
return keySetView = new KeySet<>(this);
return navigableKeySet();
}

public NavigableSet<K> navigableKeySet() {
Expand All @@ -2895,10 +2874,6 @@ public Set<Map.Entry<K,V>> entrySet() {
return entrySetView = new EntrySet<K,V>(this);
}

public NavigableSet<K> descendingKeySet() {
return descendingMap().navigableKeySet();
}

/**
* Variant of main Iter class to traverse through submaps.
* Also serves as back-up Spliterator for views.
Expand Down

0 comments on commit f8b120f

Please sign in to comment.