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    *      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  
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</code> 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      /**
64       * Constructor that wraps (not copies).
65       *
66       * @param set  the set to decorate, must not be null
67       * @throws NullPointerException if set is null
68       */
69      private UnmodifiableNavigableSet(final NavigableSet<E> set) {
70          super(set);
71      }
72  
73      //-----------------------------------------------------------------------
74      @Override
75      public Iterator<E> iterator() {
76          return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
77      }
78  
79      @Override
80      public boolean add(final E object) {
81          throw new UnsupportedOperationException();
82      }
83  
84      @Override
85      public boolean addAll(final Collection<? extends E> coll) {
86          throw new UnsupportedOperationException();
87      }
88  
89      @Override
90      public void clear() {
91          throw new UnsupportedOperationException();
92      }
93  
94      @Override
95      public boolean remove(final Object object) {
96          throw new UnsupportedOperationException();
97      }
98  
99      /**
100      * @since 4.4
101      */
102     @Override
103     public boolean removeIf(Predicate<? super E> filter) {
104         throw new UnsupportedOperationException();
105     }
106 
107     @Override
108     public boolean removeAll(final Collection<?> coll) {
109         throw new UnsupportedOperationException();
110     }
111 
112     @Override
113     public boolean retainAll(final Collection<?> coll) {
114         throw new UnsupportedOperationException();
115     }
116 
117     // SortedSet
118     //-----------------------------------------------------------------------
119     @Override
120     public SortedSet<E> subSet(final E fromElement, final E toElement) {
121         final SortedSet<E> sub = decorated().subSet(fromElement, toElement);
122         return UnmodifiableSortedSet.unmodifiableSortedSet(sub);
123     }
124 
125     @Override
126     public SortedSet<E> headSet(final E toElement) {
127         final SortedSet<E> head = decorated().headSet(toElement);
128         return UnmodifiableSortedSet.unmodifiableSortedSet(head);
129     }
130 
131     @Override
132     public SortedSet<E> tailSet(final E fromElement) {
133         final SortedSet<E> tail = decorated().tailSet(fromElement);
134         return UnmodifiableSortedSet.unmodifiableSortedSet(tail);
135     }
136 
137     // NavigableSet
138     //-----------------------------------------------------------------------
139     @Override
140     public NavigableSet<E> descendingSet() {
141         return unmodifiableNavigableSet(decorated().descendingSet());
142     }
143 
144     @Override
145     public Iterator<E> descendingIterator() {
146         return UnmodifiableIterator.unmodifiableIterator(decorated().descendingIterator());
147     }
148 
149     @Override
150     public NavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement,
151             final boolean toInclusive) {
152         final NavigableSet<E> sub = decorated().subSet(fromElement, fromInclusive, toElement, toInclusive);
153         return unmodifiableNavigableSet(sub);
154     }
155 
156     @Override
157     public NavigableSet<E> headSet(final E toElement, final boolean inclusive) {
158         final NavigableSet<E> head = decorated().headSet(toElement, inclusive);
159         return unmodifiableNavigableSet(head);
160     }
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     //-----------------------------------------------------------------------
169     /**
170      * Write the collection out using a custom routine.
171      *
172      * @param out  the output stream
173      * @throws IOException if an error occurs while writing to the stream
174      */
175     private void writeObject(final ObjectOutputStream out) throws IOException {
176         out.defaultWriteObject();
177         out.writeObject(decorated());
178     }
179 
180     /**
181      * Read the collection in using a custom routine.
182      *
183      * @param in  the input stream
184      * @throws IOException if an error occurs while reading from the stream
185      * @throws ClassNotFoundException if an object read from the stream can not be loaded
186      */
187     @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect
188     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
189         in.defaultReadObject();
190         setCollection((Collection<E>) in.readObject()); // (1)
191     }
192 
193 }