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.collections.set;
18  
19  import java.io.IOException;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.io.Serializable;
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.SortedSet;
26  
27  import org.apache.commons.collections.Unmodifiable;
28  import org.apache.commons.collections.iterators.UnmodifiableIterator;
29  
30  /**
31   * Decorates another <code>SortedSet</code> to ensure it can't be altered.
32   * <p>
33   * This class is Serializable from Commons Collections 3.1.
34   * <p>
35   * Attempts to modify it will result in an UnsupportedOperationException. 
36   *
37   * @since 3.0
38   * @version $Id: UnmodifiableSortedSet.java 1429905 2013-01-07 17:15:14Z ggregory $
39   */
40  public final class UnmodifiableSortedSet<E>
41          extends AbstractSortedSetDecorator<E>
42          implements Unmodifiable, Serializable {
43  
44      /** Serialization version */
45      private static final long serialVersionUID = -725356885467962424L;
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 SortedSet}
53       * @throws IllegalArgumentException if set is null
54       */
55      public static <E> SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set) {
56          if (set instanceof Unmodifiable) {
57              return set;
58          }
59          return new UnmodifiableSortedSet<E>(set);
60      }
61  
62      //-----------------------------------------------------------------------
63      /**
64       * Write the collection out using a custom routine.
65       * 
66       * @param out  the output stream
67       * @throws IOException
68       */
69      private void writeObject(final ObjectOutputStream out) throws IOException {
70          out.defaultWriteObject();
71          out.writeObject(collection);
72      }
73  
74      /**
75       * Read the collection in using a custom routine.
76       * 
77       * @param in  the input stream
78       * @throws IOException
79       * @throws ClassNotFoundException
80       */
81      @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect 
82      private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
83          in.defaultReadObject();
84          collection = (Collection<E>) in.readObject(); // (1)
85      }
86  
87      //-----------------------------------------------------------------------
88      /**
89       * Constructor that wraps (not copies).
90       * 
91       * @param set  the set to decorate, must not be null
92       * @throws IllegalArgumentException if set is null
93       */
94      private UnmodifiableSortedSet(final SortedSet<E> set) {
95          super(set);
96      }
97  
98      //-----------------------------------------------------------------------
99      @Override
100     public Iterator<E> iterator() {
101         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
102     }
103 
104     @Override
105     public boolean add(final E object) {
106         throw new UnsupportedOperationException();
107     }
108 
109     @Override
110     public boolean addAll(final Collection<? extends E> coll) {
111         throw new UnsupportedOperationException();
112     }
113 
114     @Override
115     public void clear() {
116         throw new UnsupportedOperationException();
117     }
118 
119     @Override
120     public boolean remove(final Object object) {
121         throw new UnsupportedOperationException();
122     }
123 
124     @Override
125     public boolean removeAll(final Collection<?> coll) {
126         throw new UnsupportedOperationException();
127     }
128 
129     @Override
130     public boolean retainAll(final Collection<?> coll) {
131         throw new UnsupportedOperationException();
132     }
133 
134     //-----------------------------------------------------------------------
135     @Override
136     public SortedSet<E> subSet(final E fromElement, final E toElement) {
137         final SortedSet<E> sub = decorated().subSet(fromElement, toElement);
138         return new UnmodifiableSortedSet<E>(sub);
139     }
140 
141     @Override
142     public SortedSet<E> headSet(final E toElement) {
143         final SortedSet<E> sub = decorated().headSet(toElement);
144         return new UnmodifiableSortedSet<E>(sub);
145     }
146 
147     @Override
148     public SortedSet<E> tailSet(final E fromElement) {
149         final SortedSet<E> sub = decorated().tailSet(fromElement);
150         return new UnmodifiableSortedSet<E>(sub);
151     }
152 
153 }