UnmodifiableNavigableSet.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.set;

  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.util.Collection;
  22. import java.util.Iterator;
  23. import java.util.NavigableSet;
  24. import java.util.SortedSet;
  25. import java.util.function.Predicate;

  26. import org.apache.commons.collections4.Unmodifiable;
  27. import org.apache.commons.collections4.iterators.UnmodifiableIterator;

  28. /**
  29.  * Decorates another {@code NavigableSet} to ensure it can't be altered.
  30.  * <p>
  31.  * Attempts to modify it will result in an UnsupportedOperationException.
  32.  * </p>
  33.  *
  34.  * @param <E> the type of the elements in this set
  35.  * @since 4.1
  36.  */
  37. public final class UnmodifiableNavigableSet<E>
  38.         extends AbstractNavigableSetDecorator<E>
  39.         implements Unmodifiable {

  40.     /** Serialization version */
  41.     private static final long serialVersionUID = 20150528L;

  42.     /**
  43.      * Factory method to create an unmodifiable set.
  44.      *
  45.      * @param <E> the element type
  46.      * @param set  the set to decorate, must not be null
  47.      * @return a new unmodifiable {@link NavigableSet}
  48.      * @throws NullPointerException if set is null
  49.      */
  50.     public static <E> NavigableSet<E> unmodifiableNavigableSet(final NavigableSet<E> set) {
  51.         if (set instanceof Unmodifiable) {
  52.             return set;
  53.         }
  54.         return new UnmodifiableNavigableSet<>(set);
  55.     }

  56.     /**
  57.      * Constructor that wraps (not copies).
  58.      *
  59.      * @param set  the set to decorate, must not be null
  60.      * @throws NullPointerException if set is null
  61.      */
  62.     private UnmodifiableNavigableSet(final NavigableSet<E> set) {
  63.         super(set);
  64.     }

  65.     @Override
  66.     public boolean add(final E object) {
  67.         throw new UnsupportedOperationException();
  68.     }

  69.     @Override
  70.     public boolean addAll(final Collection<? extends E> coll) {
  71.         throw new UnsupportedOperationException();
  72.     }

  73.     @Override
  74.     public void clear() {
  75.         throw new UnsupportedOperationException();
  76.     }

  77.     @Override
  78.     public Iterator<E> descendingIterator() {
  79.         return UnmodifiableIterator.unmodifiableIterator(decorated().descendingIterator());
  80.     }

  81.     // NavigableSet
  82.     @Override
  83.     public NavigableSet<E> descendingSet() {
  84.         return unmodifiableNavigableSet(decorated().descendingSet());
  85.     }

  86.     @Override
  87.     public SortedSet<E> headSet(final E toElement) {
  88.         final SortedSet<E> head = decorated().headSet(toElement);
  89.         return UnmodifiableSortedSet.unmodifiableSortedSet(head);
  90.     }

  91.     @Override
  92.     public NavigableSet<E> headSet(final E toElement, final boolean inclusive) {
  93.         final NavigableSet<E> head = decorated().headSet(toElement, inclusive);
  94.         return unmodifiableNavigableSet(head);
  95.     }

  96.     @Override
  97.     public Iterator<E> iterator() {
  98.         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
  99.     }

  100.     /**
  101.      * @since 4.5.0-M1
  102.      */
  103.     @Override
  104.     public E pollFirst() {
  105.         throw new UnsupportedOperationException();
  106.     }

  107.     /**
  108.      * @since 4.5.0-M1
  109.      */
  110.     @Override
  111.     public E pollLast() {
  112.         throw new UnsupportedOperationException();
  113.     }

  114.     /**
  115.      * Deserializes the collection in using a custom routine.
  116.      *
  117.      * @param in  the input stream
  118.      * @throws IOException if an error occurs while reading from the stream
  119.      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
  120.      */
  121.     @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect
  122.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  123.         in.defaultReadObject();
  124.         setCollection((Collection<E>) in.readObject()); // (1)
  125.     }

  126.     @Override
  127.     public boolean remove(final Object object) {
  128.         throw new UnsupportedOperationException();
  129.     }

  130.     @Override
  131.     public boolean removeAll(final Collection<?> coll) {
  132.         throw new UnsupportedOperationException();
  133.     }

  134.     /**
  135.      * @since 4.4
  136.      */
  137.     @Override
  138.     public boolean removeIf(final Predicate<? super E> filter) {
  139.         throw new UnsupportedOperationException();
  140.     }

  141.     @Override
  142.     public boolean retainAll(final Collection<?> coll) {
  143.         throw new UnsupportedOperationException();
  144.     }

  145.     @Override
  146.     public NavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement,
  147.             final boolean toInclusive) {
  148.         final NavigableSet<E> sub = decorated().subSet(fromElement, fromInclusive, toElement, toInclusive);
  149.         return unmodifiableNavigableSet(sub);
  150.     }

  151.     // SortedSet
  152.     @Override
  153.     public SortedSet<E> subSet(final E fromElement, final E toElement) {
  154.         final SortedSet<E> sub = decorated().subSet(fromElement, toElement);
  155.         return UnmodifiableSortedSet.unmodifiableSortedSet(sub);
  156.     }

  157.     @Override
  158.     public SortedSet<E> tailSet(final E fromElement) {
  159.         final SortedSet<E> tail = decorated().tailSet(fromElement);
  160.         return UnmodifiableSortedSet.unmodifiableSortedSet(tail);
  161.     }

  162.     @Override
  163.     public NavigableSet<E> tailSet(final E fromElement, final boolean inclusive) {
  164.         final NavigableSet<E> tail = decorated().tailSet(fromElement, inclusive);
  165.         return unmodifiableNavigableSet(tail);
  166.     }

  167.     /**
  168.      * Serializes this object to an ObjectOutputStream.
  169.      *
  170.      * @param out the target ObjectOutputStream.
  171.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  172.      */
  173.     private void writeObject(final ObjectOutputStream out) throws IOException {
  174.         out.defaultWriteObject();
  175.         out.writeObject(decorated());
  176.     }

  177. }