Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is just a guess, but part of the reason why an unmodifiable implementation was not provided may be due to the fact that the NavigableSet/Map interfaces expose mutating methods that aren't marked as optional:</p> <ul> <li>NavigableSet.pollFirst</li> <li>NavigableSet.pollLast</li> <li>NavigableMap.pollFirstEntry </li> <li>NavigableMap.pollLastEntry</li> </ul> <p>That said, it seems reasonable when providing an unmodifiable implementation to just throw an UnsupportedOperationException. That is what is done in these implementations (which assume you're using GoogleGuava):</p> <p><strong>NavigableSet:</strong> </p> <pre><code>import static java.util.Collections.unmodifiableSortedSet; import java.util.Collections; import java.util.Iterator; import java.util.NavigableSet; import java.util.SortedSet; import com.google.common.collect.ForwardingSortedSet; /** * {@link NavigableSet} equivalent of {@link Collections#unmodifiableSortedSet(SortedSet)}. * This is unfortunately needed because {@link Collections} lacks "UnmodifiableNavigableSet" * (see http://stackoverflow.com/questions/2577706/unmodifiable-navigableset-navigablemap-in-java * and http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6907251). * * This is just a guess, but part of the reason why an unmodifiable implementation was not provided may be due to the fact that * {@link NavigableSet} exposes mutating methods that aren't marked as optional: * - {@link NavigableSet#pollFirst()} * - {@link NavigableSet#pollLast()} * * In addition, one can't go the immutable route, as Google Guava doesn't provide an Immutable variant * (see http://code.google.com/p/guava-libraries/issues/detail?id=664). * * @param &lt;E&gt; See {@link NavigableSet} */ public final class UnmodifiableNavigableSet&lt;E&gt; extends ForwardingSortedSet&lt;E&gt; implements NavigableSet&lt;E&gt; { private final NavigableSet&lt;E&gt; delegate; private final SortedSet&lt;E&gt; unmodifiableDelegate; /** * @param delegate See {@link ForwardingSortedSet#delegate()}. */ public UnmodifiableNavigableSet(NavigableSet&lt;E&gt; delegate) { this.delegate = delegate; unmodifiableDelegate = unmodifiableSortedSet(delegate); } /** * @param delegate * @return {@link #UnmodifiableNavigableSet(NavigableSet)} * @see EffectiveJava#Static_factories_instead_of_constructors */ public static &lt;E&gt; UnmodifiableNavigableSet&lt;E&gt; newUnmodifiableNavigableSet(NavigableSet&lt;E&gt; delegate) { return new UnmodifiableNavigableSet&lt;E&gt;(delegate); } @Override protected SortedSet&lt;E&gt; delegate() { return unmodifiableDelegate; } @Override public E ceiling(E e) { return delegate.ceiling(e); } @Override public Iterator&lt;E&gt; descendingIterator() { // NavigableSet.descendingIterator explicitly states this behavior. // By doing this, we don't need to do anything extra to ensure the iterator is unmodifiable. return descendingSet().iterator(); } @Override public NavigableSet&lt;E&gt; descendingSet() { return newUnmodifiableNavigableSet(delegate.descendingSet()); } @Override public E floor(E e) { return delegate.floor(e); } @Override public NavigableSet&lt;E&gt; headSet(E toElement, boolean inclusive) { return newUnmodifiableNavigableSet(delegate.headSet(toElement, inclusive)); } @Override public E higher(E e) { return delegate.higher(e); } @Override public E lower(E e) { return delegate.lower(e); } @Override public E pollFirst() { throw new UnsupportedOperationException(); } @Override public E pollLast() { throw new UnsupportedOperationException(); } @Override public NavigableSet&lt;E&gt; subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { return newUnmodifiableNavigableSet(delegate.subSet(fromElement, fromInclusive, toElement, toInclusive)); } @Override public NavigableSet&lt;E&gt; tailSet(E fromElement, boolean inclusive) { return newUnmodifiableNavigableSet(delegate.tailSet(fromElement, inclusive)); } } </code></pre> <p><strong>NavigableMap:</strong> </p> <pre><code>import static UnmodifiableNavigableSet.newUnmodifiableNavigableSet; import static java.util.Collections.unmodifiableSortedMap; import java.util.AbstractMap; import java.util.Map; import java.util.NavigableMap; import java.util.NavigableSet; import java.util.SortedMap; import com.google.common.collect.ForwardingSortedMap; /** * This class has the same rational as {@link UnmodifiableNavigableSet}. * @param &lt;K&gt; See {@link NavigableMap} * @param &lt;V&gt; See {@link NavigableMap} */ public final class UnmodifiableNavigableMap&lt;K,V&gt; extends ForwardingSortedMap&lt;K,V&gt; implements NavigableMap&lt;K,V&gt; { private final NavigableMap&lt;K,V&gt; delegate; private final SortedMap&lt;K,V&gt; unmodifiableDelegate; /** * @param delegate See {@link ForwardingSortedMap#delegate()}. */ public UnmodifiableNavigableMap(NavigableMap&lt;K,V&gt; delegate) { this.delegate = delegate; unmodifiableDelegate = unmodifiableSortedMap(delegate); } /** * @param delegate * @return {@link #UnmodifiableNavigableMap(NavigableMap)} * @see EffectiveJava#Static_factories_instead_of_constructors */ public static &lt;K,V&gt; UnmodifiableNavigableMap&lt;K,V&gt; newUnmodifiableNavigableMap(NavigableMap&lt;K,V&gt; delegate) { return new UnmodifiableNavigableMap&lt;K,V&gt;(delegate); } @Override protected SortedMap&lt;K,V&gt; delegate() { return unmodifiableDelegate; } private Map.Entry&lt;K,V&gt; newImmutableEntry(Map.Entry&lt;K,V&gt; entry) { return entry == null ? null : new AbstractMap.SimpleImmutableEntry&lt;K,V&gt;(entry); } @Override public Map.Entry&lt;K,V&gt; ceilingEntry(K key) { return newImmutableEntry(delegate.ceilingEntry(key)); } @Override public K ceilingKey(K key) { return delegate.ceilingKey(key); } @Override public NavigableSet&lt;K&gt; descendingKeySet() { return newUnmodifiableNavigableSet(delegate.descendingKeySet()); } @Override public NavigableMap&lt;K,V&gt; descendingMap() { return newUnmodifiableNavigableMap(delegate.descendingMap()); } @Override public Map.Entry&lt;K,V&gt; firstEntry() { return newImmutableEntry(delegate.firstEntry()); } @Override public Map.Entry&lt;K,V&gt; floorEntry(K key) { return newImmutableEntry(delegate.floorEntry(key)); } @Override public K floorKey(K key) { return delegate.floorKey(key); } @Override public NavigableMap&lt;K,V&gt; headMap(K toKey, boolean inclusive) { return newUnmodifiableNavigableMap(delegate.headMap(toKey, inclusive)); } @Override public Map.Entry&lt;K,V&gt; higherEntry(K key) { return newImmutableEntry(delegate.higherEntry(key)); } @Override public K higherKey(K key) { return delegate.higherKey(key); } @Override public Map.Entry&lt;K,V&gt; lastEntry() { return newImmutableEntry(delegate.lastEntry()); } @Override public Map.Entry&lt;K,V&gt; lowerEntry(K key) { return newImmutableEntry(delegate.lowerEntry(key)); } @Override public K lowerKey(K key) { return delegate.lowerKey(key); } @Override public NavigableSet&lt;K&gt; navigableKeySet() { return newUnmodifiableNavigableSet(delegate.navigableKeySet()); } @Override public Map.Entry&lt;K,V&gt; pollFirstEntry() { throw new UnsupportedOperationException(); } @Override public Map.Entry&lt;K,V&gt; pollLastEntry() { throw new UnsupportedOperationException(); } @Override public NavigableMap&lt;K,V&gt; subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) { return newUnmodifiableNavigableMap(delegate.subMap(fromKey, fromInclusive, toKey, toInclusive)); } @Override public NavigableMap&lt;K,V&gt; tailMap(K fromKey, boolean inclusive) { return newUnmodifiableNavigableMap(delegate.tailMap(fromKey, inclusive)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload