View Javadoc

1   /*
2    *  Copyright 2003-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.collections15.set;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  import java.util.Set;
21  
22  import net.sf.collections15.Unmodifiable;
23  import net.sf.collections15.iterators.UnmodifiableIterator;
24  
25  
26  /***
27   * Decorates another <code>Set</code> to ensure it can't be altered.
28   * <p>
29   * This class is Serializable from Commons Collections 3.1.
30   *
31   * @since Commons Collections 3.0
32   * @version $Revision: 1.1 $ $Date: 2005/05/03 22:45:38 $
33   * 
34   * @author Stephen Colebourne
35   */
36  public final class UnmodifiableSet<E>
37          extends AbstractSerializableSetDecorator<E>
38          implements Unmodifiable {
39  
40  	/*** Serialization version */
41  	private static final long serialVersionUID = 3835152865663793205L;
42  
43      /***
44       * Factory method to create an unmodifiable set.
45       * 
46       * @param set  the set to decorate, must not be null
47       * @throws IllegalArgumentException if set is null
48       */
49      public static <T> Set<T> decorate(Set<T> set) {
50          if (set instanceof Unmodifiable) {
51              return set;
52          }
53          return new UnmodifiableSet<T>(set);
54      }
55  
56      //-----------------------------------------------------------------------
57      /***
58       * Constructor that wraps (not copies).
59       * 
60       * @param set  the set to decorate, must not be null
61       * @throws IllegalArgumentException if set is null
62       */
63      private UnmodifiableSet(Set<E> set) {
64          super(set);
65      }
66  
67      //-----------------------------------------------------------------------
68      public Iterator<E> iterator() {
69          return UnmodifiableIterator.<E>decorate(getCollection().iterator());
70      }
71  
72      public boolean add(E object) {
73          throw new UnsupportedOperationException();
74      }
75  
76      public boolean addAll(Collection<? extends E> coll) {
77          throw new UnsupportedOperationException();
78      }
79  
80      public void clear() {
81          throw new UnsupportedOperationException();
82      }
83  
84      public boolean remove(Object object) {
85          throw new UnsupportedOperationException();
86      }
87  
88      public boolean removeAll(Collection<?> coll) {
89          throw new UnsupportedOperationException();
90      }
91  
92      public boolean retainAll(Collection<?> coll) {
93          throw new UnsupportedOperationException();
94      }
95  
96  }