UnmodifiableSortedSet.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.SortedSet;
  24. import java.util.function.Predicate;

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

  27. /**
  28.  * Decorates another {@code SortedSet} to ensure it can't be altered.
  29.  * <p>
  30.  * This class is Serializable from Commons Collections 3.1.
  31.  * </p>
  32.  * <p>
  33.  * Attempts to modify it will result in an UnsupportedOperationException.
  34.  * </p>
  35.  *
  36.  * @param <E> the type of the elements in this set
  37.  * @since 3.0
  38.  */
  39. public final class UnmodifiableSortedSet<E>
  40.         extends AbstractSortedSetDecorator<E>
  41.         implements Unmodifiable {

  42.     /** Serialization version */
  43.     private static final long serialVersionUID = -725356885467962424L;

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

  59.     /**
  60.      * Constructor that wraps (not copies).
  61.      *
  62.      * @param set  the set to decorate, must not be null
  63.      * @throws NullPointerException if set is null
  64.      */
  65.     private UnmodifiableSortedSet(final SortedSet<E> set) {
  66.         super(set);
  67.     }

  68.     @Override
  69.     public boolean add(final E object) {
  70.         throw new UnsupportedOperationException();
  71.     }

  72.     @Override
  73.     public boolean addAll(final Collection<? extends E> coll) {
  74.         throw new UnsupportedOperationException();
  75.     }

  76.     @Override
  77.     public void clear() {
  78.         throw new UnsupportedOperationException();
  79.     }

  80.     @Override
  81.     public SortedSet<E> headSet(final E toElement) {
  82.         final SortedSet<E> head = decorated().headSet(toElement);
  83.         return unmodifiableSortedSet(head);
  84.     }

  85.     @Override
  86.     public Iterator<E> iterator() {
  87.         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
  88.     }

  89.     /**
  90.      * Deserializes the collection in using a custom routine.
  91.      *
  92.      * @param in  the input stream
  93.      * @throws IOException if an error occurs while reading from the stream
  94.      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
  95.      */
  96.     @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect
  97.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  98.         in.defaultReadObject();
  99.         setCollection((Collection<E>) in.readObject()); // (1)
  100.     }

  101.     @Override
  102.     public boolean remove(final Object object) {
  103.         throw new UnsupportedOperationException();
  104.     }

  105.     @Override
  106.     public boolean removeAll(final Collection<?> coll) {
  107.         throw new UnsupportedOperationException();
  108.     }

  109.     /**
  110.      * @since 4.4
  111.      */
  112.     @Override
  113.     public boolean removeIf(final Predicate<? super E> filter) {
  114.         throw new UnsupportedOperationException();
  115.     }

  116.     @Override
  117.     public boolean retainAll(final Collection<?> coll) {
  118.         throw new UnsupportedOperationException();
  119.     }

  120.     @Override
  121.     public SortedSet<E> subSet(final E fromElement, final E toElement) {
  122.         final SortedSet<E> sub = decorated().subSet(fromElement, toElement);
  123.         return unmodifiableSortedSet(sub);
  124.     }

  125.     @Override
  126.     public SortedSet<E> tailSet(final E fromElement) {
  127.         final SortedSet<E> tail = decorated().tailSet(fromElement);
  128.         return unmodifiableSortedSet(tail);
  129.     }

  130.     /**
  131.      * Serializes this object to an ObjectOutputStream.
  132.      *
  133.      * @param out the target ObjectOutputStream.
  134.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  135.      */
  136.     private void writeObject(final ObjectOutputStream out) throws IOException {
  137.         out.defaultWriteObject();
  138.         out.writeObject(decorated());
  139.     }

  140. }