View Javadoc
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    *      https://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  
19  import java.io.IOException;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.SortedSet;
25  import java.util.function.Predicate;
26  
27  import org.apache.commons.collections4.Unmodifiable;
28  import org.apache.commons.collections4.iterators.UnmodifiableIterator;
29  
30  /**
31   * Decorates another {@code SortedSet} to ensure it can't be altered.
32   * <p>
33   * This class is Serializable from Commons Collections 3.1.
34   * </p>
35   * <p>
36   * Attempts to modify it will result in an UnsupportedOperationException.
37   * </p>
38   *
39   * @param <E> The type of the elements in this set
40   * @since 3.0
41   */
42  public final class UnmodifiableSortedSet<E>
43          extends AbstractSortedSetDecorator<E>
44          implements Unmodifiable {
45  
46      /** Serialization version */
47      private static final long serialVersionUID = -725356885467962424L;
48  
49      /**
50       * Factory method to create an unmodifiable set.
51       *
52       * @param <E> The element type
53       * @param set  The set to decorate, must not be null
54       * @return A new unmodifiable {@link SortedSet}
55       * @throws NullPointerException if set is null
56       * @since 4.0
57       */
58      public static <E> SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set) {
59          if (set instanceof Unmodifiable) {
60              return set;
61          }
62          return new UnmodifiableSortedSet<>(set);
63      }
64  
65      /**
66       * Constructor that wraps (not copies).
67       *
68       * @param set  The set to decorate, must not be null
69       * @throws NullPointerException if set is null
70       */
71      private UnmodifiableSortedSet(final SortedSet<E> set) {
72          super(set);
73      }
74  
75      /**
76       * Always throws {@link UnsupportedOperationException}.
77       *
78       * @param object Ignored.
79       * @throws UnsupportedOperationException Always thrown.
80       */
81      @Override
82      public boolean add(final E object) {
83          throw new UnsupportedOperationException();
84      }
85  
86      /**
87       * Always throws {@link UnsupportedOperationException}.
88       *
89       * @param coll Ignored.
90       * @throws UnsupportedOperationException Always thrown.
91       */
92      @Override
93      public boolean addAll(final Collection<? extends E> coll) {
94          throw new UnsupportedOperationException();
95      }
96  
97      /**
98       * Always throws {@link UnsupportedOperationException}.
99       *
100      * @throws UnsupportedOperationException Always thrown.
101      */
102     @Override
103     public void clear() {
104         throw new UnsupportedOperationException();
105     }
106 
107     @Override
108     public SortedSet<E> headSet(final E toElement) {
109         return unmodifiableSortedSet(decorated().headSet(toElement));
110     }
111 
112     @Override
113     public Iterator<E> iterator() {
114         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
115     }
116 
117     /**
118      * Deserializes the collection in using a custom routine.
119      *
120      * @param in  The input stream
121      * @throws IOException Thrown if an error occurs while reading from the stream
122      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
123      */
124     @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect
125     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
126         in.defaultReadObject();
127         setCollection((Collection<E>) in.readObject()); // (1)
128     }
129 
130     /**
131      * Always throws {@link UnsupportedOperationException}.
132      *
133      * @param object Ignored.
134      * @throws UnsupportedOperationException Always thrown.
135      */
136     @Override
137     public boolean remove(final Object object) {
138         throw new UnsupportedOperationException();
139     }
140 
141     /**
142      * Always throws {@link UnsupportedOperationException}.
143      *
144      * @param coll Ignored.
145      * @throws UnsupportedOperationException Always thrown.
146      */
147     @Override
148     public boolean removeAll(final Collection<?> coll) {
149         throw new UnsupportedOperationException();
150     }
151 
152     /**
153      * Always throws {@link UnsupportedOperationException}.
154      *
155      * @param filter Ignored.
156      * @throws UnsupportedOperationException Always thrown.
157      * @since 4.4
158      */
159     @Override
160     public boolean removeIf(final Predicate<? super E> filter) {
161         throw new UnsupportedOperationException();
162     }
163 
164     /**
165      * Always throws {@link UnsupportedOperationException}.
166      *
167      * @throws UnsupportedOperationException Always thrown.
168      */
169     @Override
170     public boolean retainAll(final Collection<?> coll) {
171         throw new UnsupportedOperationException();
172     }
173 
174     @Override
175     public SortedSet<E> subSet(final E fromElement, final E toElement) {
176         return unmodifiableSortedSet(decorated().subSet(fromElement, toElement));
177     }
178 
179     @Override
180     public SortedSet<E> tailSet(final E fromElement) {
181         return unmodifiableSortedSet(decorated().tailSet(fromElement));
182     }
183 
184     /**
185      * Serializes this object to an ObjectOutputStream.
186      *
187      * @param out The target ObjectOutputStream.
188      * @throws IOException thrown when an I/O errors occur writing to the target stream.
189      */
190     private void writeObject(final ObjectOutputStream out) throws IOException {
191         out.defaultWriteObject();
192         out.writeObject(decorated());
193     }
194 
195 }