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