From f8b120ff578744b2599860251a54eb3dec44e07f Mon Sep 17 00:00:00 2001 From: liach Date: Sat, 2 Nov 2024 12:44:52 -0500 Subject: [PATCH] 8343485: Default implementations for NavigableMap --- .../share/classes/java/util/NavigableMap.java | 64 +++++++++++++++++-- .../share/classes/java/util/NavigableSet.java | 34 ++++++++-- .../share/classes/java/util/TreeMap.java | 27 +------- .../concurrent/ConcurrentNavigableMap.java | 30 +++++++-- .../concurrent/ConcurrentSkipListMap.java | 27 +------- 5 files changed, 115 insertions(+), 67 deletions(-) diff --git a/src/java.base/share/classes/java/util/NavigableMap.java b/src/java.base/share/classes/java/util/NavigableMap.java index fdd5d3b1245f5..b7cacb24c5ef3 100644 --- a/src/java.base/share/classes/java/util/NavigableMap.java +++ b/src/java.base/share/classes/java/util/NavigableMap.java @@ -306,9 +306,15 @@ public interface NavigableMap extends SortedMap { * {@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 descendingKeySet(); + default NavigableSet descendingKeySet() { + return descendingMap().navigableKeySet(); + } /** * Returns a view of the portion of this map whose keys range from @@ -410,33 +416,54 @@ NavigableMap subMap(K fromKey, boolean fromInclusive, * *

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 subMap(K fromKey, K toKey); + @Override + default SortedMap subMap(K fromKey, K toKey) { + return subMap(fromKey, true, toKey, false); + } /** * {@inheritDoc} * *

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 headMap(K toKey); + @Override + default SortedMap headMap(K toKey) { + return headMap(toKey, false); + } /** * {@inheritDoc} * *

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 tailMap(K fromKey); + @Override + default SortedMap tailMap(K fromKey) { + return tailMap(fromKey, true); + } /** * {@inheritDoc} @@ -453,4 +480,33 @@ NavigableMap subMap(K fromKey, boolean fromInclusive, default NavigableMap 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 sequencedKeySet() { + return navigableKeySet(); + } + + /** + * {@inheritDoc} + * + * @implSpec + * The implementation in this interface returns the result of calling the + * {@code navigableKeySet} method. + * + * @return {@inheritDoc} + */ + @Override + default Set keySet() { + return navigableKeySet(); + } } diff --git a/src/java.base/share/classes/java/util/NavigableSet.java b/src/java.base/share/classes/java/util/NavigableSet.java index 16124acbeff1c..81169e83b8c2a 100644 --- a/src/java.base/share/classes/java/util/NavigableSet.java +++ b/src/java.base/share/classes/java/util/NavigableSet.java @@ -191,12 +191,16 @@ public interface NavigableSet extends SortedSet { NavigableSet 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 descendingIterator(); + default Iterator descendingIterator() { + return descendingSet().iterator(); + } /** * Returns a view of the portion of this set whose elements range from @@ -299,33 +303,51 @@ NavigableSet subSet(E fromElement, boolean fromInclusive, * *

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 subSet(E fromElement, E toElement); + default SortedSet subSet(E fromElement, E toElement) { + return subSet(fromElement, true, toElement, false); + } /** * {@inheritDoc} * *

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 headSet(E toElement); + default SortedSet headSet(E toElement) { + return headSet(toElement, false); + } /** * {@inheritDoc} * *

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 tailSet(E fromElement); + default SortedSet tailSet(E fromElement) { + return tailSet(fromElement, true); + } /** * {@inheritDoc} diff --git a/src/java.base/share/classes/java/util/TreeMap.java b/src/java.base/share/classes/java/util/TreeMap.java index 0e544573f0130..3db4c84f47877 100644 --- a/src/java.base/share/classes/java/util/TreeMap.java +++ b/src/java.base/share/classes/java/util/TreeMap.java @@ -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 @@ -1480,15 +1480,6 @@ public NavigableSet headSet(E toElement, boolean inclusive) { public NavigableSet tailSet(E fromElement, boolean inclusive) { return new KeySet<>(m.tailMap(fromElement, inclusive)); } - public SortedSet subSet(E fromElement, E toElement) { - return subSet(fromElement, true, toElement, false); - } - public SortedSet headSet(E toElement) { - return headSet(toElement, false); - } - public SortedSet tailSet(E fromElement) { - return tailSet(fromElement, true); - } public NavigableSet descendingSet() { return new KeySet<>(m.descendingMap()); } @@ -1957,22 +1948,6 @@ public final Set keySet() { return navigableKeySet(); } - public NavigableSet descendingKeySet() { - return descendingMap().navigableKeySet(); - } - - public final SortedMap subMap(K fromKey, K toKey) { - return subMap(fromKey, true, toKey, false); - } - - public final SortedMap headMap(K toKey) { - return headMap(toKey, false); - } - - public final SortedMap tailMap(K fromKey) { - return tailMap(fromKey, true); - } - // View classes abstract class EntrySetView extends AbstractSet> { diff --git a/src/java.base/share/classes/java/util/concurrent/ConcurrentNavigableMap.java b/src/java.base/share/classes/java/util/concurrent/ConcurrentNavigableMap.java index 1666c8b0afd5c..ddedf0e64b80c 100644 --- a/src/java.base/share/classes/java/util/concurrent/ConcurrentNavigableMap.java +++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentNavigableMap.java @@ -77,25 +77,37 @@ ConcurrentNavigableMap subMap(K fromKey, boolean fromInclusive, ConcurrentNavigableMap tailMap(K fromKey, boolean inclusive); /** + * @implSpec {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ - ConcurrentNavigableMap subMap(K fromKey, K toKey); + @Override + default ConcurrentNavigableMap subMap(K fromKey, K toKey) { + return subMap(fromKey, true, toKey, false); + } /** + * @implSpec {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ - ConcurrentNavigableMap headMap(K toKey); + @Override + default ConcurrentNavigableMap headMap(K toKey) { + return headMap(toKey, false); + } /** + * @implSpec {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ - ConcurrentNavigableMap tailMap(K fromKey); + @Override + default ConcurrentNavigableMap tailMap(K fromKey) { + return tailMap(fromKey, true); + } /** * Returns a reverse order view of the mappings contained in this map. @@ -145,9 +157,13 @@ ConcurrentNavigableMap subMap(K fromKey, boolean fromInclusive, * *

This method is equivalent to method {@code navigableKeySet}. * + * @implSpec {@inheritDoc} * @return a navigable set view of the keys in this map */ - NavigableSet keySet(); + @Override + default NavigableSet keySet() { + return navigableKeySet(); + } /** * Returns a reverse order {@link NavigableSet} view of the keys contained in this map. @@ -163,7 +179,11 @@ ConcurrentNavigableMap subMap(K fromKey, boolean fromInclusive, *

The view's iterators and spliterators are * weakly consistent. * + * @implSpec {@inheritDoc} * @return a reverse order navigable set view of the keys in this map */ - NavigableSet descendingKeySet(); + @Override + default NavigableSet descendingKeySet() { + return descendingMap().keySet(); + } } diff --git a/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java b/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java index e8dafaa0ac463..11f9751144b95 100644 --- a/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java +++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java @@ -1689,10 +1689,6 @@ public ConcurrentNavigableMap descendingMap() { new SubMap(this, null, false, null, false, true); } - public NavigableSet descendingKeySet() { - return descendingMap().navigableKeySet(); - } - /* ---------------- AbstractMap Overrides -------------- */ /** @@ -2237,9 +2233,6 @@ public boolean equals(Object o) { } public Object[] toArray() { return toList(this).toArray(); } public T[] toArray(T[] a) { return toList(this).toArray(a); } - public Iterator descendingIterator() { - return descendingSet().iterator(); - } public NavigableSet subSet(K fromElement, boolean fromInclusive, K toElement, @@ -2794,18 +2787,6 @@ public SubMap tailMap(K fromKey, boolean inclusive) { return newSubMap(fromKey, inclusive, null, false); } - public SubMap subMap(K fromKey, K toKey) { - return subMap(fromKey, true, toKey, false); - } - - public SubMap headMap(K toKey) { - return headMap(toKey, false); - } - - public SubMap tailMap(K fromKey) { - return tailMap(fromKey, true); - } - public SubMap descendingMap() { return new SubMap(m, lo, loInclusive, hi, hiInclusive, !isDescending); @@ -2872,9 +2853,7 @@ public Map.Entry pollLastEntry() { /* ---------------- Submap Views -------------- */ public NavigableSet keySet() { - KeySet ks; - if ((ks = keySetView) != null) return ks; - return keySetView = new KeySet<>(this); + return navigableKeySet(); } public NavigableSet navigableKeySet() { @@ -2895,10 +2874,6 @@ public Set> entrySet() { return entrySetView = new EntrySet(this); } - public NavigableSet descendingKeySet() { - return descendingMap().navigableKeySet(); - } - /** * Variant of main Iter class to traverse through submaps. * Also serves as back-up Spliterator for views.