8307840: SequencedMap view method specification and implementation adjustments

Reviewed-by: darcy, alanb
This commit is contained in:
Stuart Marks 2023-06-06 17:02:32 +00:00
parent 74dc50b7f0
commit 9526190863
3 changed files with 110 additions and 49 deletions

View file

@ -259,45 +259,56 @@ public interface SequencedMap<K, V> extends Map<K, V> {
}
/**
* Returns a {@link SequencedSet} view of this map's keySet.
* Returns a {@code SequencedSet} view of this map's {@link #keySet keySet}.
*
* @implSpec
* The implementation in this interface returns a {@code SequencedSet}
* implementation that delegates all operations either to this map or to this map's
* {@link #keySet}, except for its {@link SequencedSet#reversed reversed} method,
* which instead returns the result of calling {@code sequencedKeySet} on this map's
* reverse-ordered view.
* The implementation in this interface returns a {@code SequencedSet} instance
* that behaves as follows. Its {@link SequencedSet#add add} and {@link
* SequencedSet#addAll addAll} methods throw {@link UnsupportedOperationException}.
* Its {@link SequencedSet#reversed reversed} method returns the {@link
* #sequencedKeySet sequencedKeySet} view of the {@link #reversed reversed} view of
* this map. Each of its other methods calls the corresponding method of the {@link
* #keySet keySet} view of this map.
*
* @return a SequencedSet view of this map's keySet
* @return a {@code SequencedSet} view of this map's {@code keySet}
*/
default SequencedSet<K> sequencedKeySet() {
class SeqKeySet extends AbstractMap.ViewCollection<K> implements SequencedSet<K> {
SeqKeySet() {
super(SequencedMap.this.keySet());
Collection<K> view() {
return SequencedMap.this.keySet();
}
public SequencedSet<K> reversed() {
return SequencedMap.this.reversed().sequencedKeySet();
}
public boolean equals(Object other) {
return view().equals(other);
}
public int hashCode() {
return view().hashCode();
}
}
return new SeqKeySet();
}
/**
* Returns a {@link SequencedCollection} view of this map's values collection.
* Returns a {@code SequencedCollection} view of this map's {@link #values values} collection.
*
* @implSpec
* The implementation in this interface returns a {@code SequencedCollection}
* implementation that delegates all operations either to this map or to this map's
* {@link #values} collection, except for its {@link SequencedCollection#reversed reversed}
* method, which instead returns the result of calling {@code sequencedValues} on this map's
* reverse-ordered view.
* The implementation in this interface returns a {@code SequencedCollection} instance
* that behaves as follows. Its {@link SequencedCollection#add add} and {@link
* SequencedCollection#addAll addAll} methods throw {@link UnsupportedOperationException}.
* Its {@link SequencedCollection#reversed reversed} method returns the {@link
* #sequencedValues sequencedValues} view of the {@link #reversed reversed} view of
* this map. Its {@link Object#equals equals} and {@link Object#hashCode hashCode} methods
* are inherited from {@link Object}. Each of its other methods calls the corresponding
* method of the {@link #values values} view of this map.
*
* @return a SequencedCollection view of this map's values collection
* @return a {@code SequencedCollection} view of this map's {@code values} collection
*/
default SequencedCollection<V> sequencedValues() {
class SeqValues extends AbstractMap.ViewCollection<V> implements SequencedCollection<V> {
SeqValues() {
super(SequencedMap.this.values());
Collection<V> view() {
return SequencedMap.this.values();
}
public SequencedCollection<V> reversed() {
return SequencedMap.this.reversed().sequencedValues();
@ -307,26 +318,34 @@ public interface SequencedMap<K, V> extends Map<K, V> {
}
/**
* Returns a {@link SequencedSet} view of this map's entrySet.
* Returns a {@code SequencedSet} view of this map's {@link #entrySet entrySet}.
*
* @implSpec
* The implementation in this interface returns a {@code SequencedSet}
* implementation that delegates all operations either to this map or to this map's
* {@link #entrySet}, except for its {@link SequencedSet#reversed reversed} method,
* which instead returns the result of calling {@code sequencedEntrySet} on this map's
* reverse-ordered view.
* The implementation in this interface returns a {@code SequencedSet} instance
* that behaves as follows. Its {@link SequencedSet#add add} and {@link
* SequencedSet#addAll addAll} methods throw {@link UnsupportedOperationException}.
* Its {@link SequencedSet#reversed reversed} method returns the {@link
* #sequencedEntrySet sequencedEntrySet} view of the {@link #reversed reversed} view of
* this map. Each of its other methods calls the corresponding method of the {@link
* #entrySet entrySet} view of this map.
*
* @return a SequencedSet view of this map's entrySet
* @return a {@code SequencedSet} view of this map's {@code entrySet}
*/
default SequencedSet<Map.Entry<K, V>> sequencedEntrySet() {
class SeqEntrySet extends AbstractMap.ViewCollection<Map.Entry<K, V>>
implements SequencedSet<Map.Entry<K, V>> {
SeqEntrySet() {
super(SequencedMap.this.entrySet());
Collection<Map.Entry<K, V>> view() {
return SequencedMap.this.entrySet();
}
public SequencedSet<Map.Entry<K, V>> reversed() {
return SequencedMap.this.reversed().sequencedEntrySet();
}
public boolean equals(Object other) {
return view().equals(other);
}
public int hashCode() {
return view().hashCode();
}
}
return new SeqEntrySet();
}