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.collection;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  
21  import net.sf.collections15.Unmodifiable;
22  import net.sf.collections15.iterators.UnmodifiableIterator;
23  
24  
25  /***
26   * Decorates another <code>Collection</code> to ensure it can't be altered.
27   * <p>
28   * This class is Serializable from Commons Collections 3.1.
29   *
30   * @since Commons Collections 3.0
31   * @version $Revision: 1.1 $ $Date: 2005/05/25 21:25:42 $
32   * 
33   * @author Stephen Colebourne
34   */
35  public final class UnmodifiableCollection<E>
36          extends AbstractSerializableCollectionDecorator<E>
37          implements Unmodifiable {
38  
39      /*** Serialization version */
40  	private static final long serialVersionUID = 3258410612462466616L;
41  
42  	/***
43       * Factory method to create an unmodifiable collection.
44       * <p>
45       * If the collection passed in is already unmodifiable, it is returned.
46       * 
47       * @param coll  the collection to decorate, must not be null
48       * @return an unmodifiable collection
49       * @throws IllegalArgumentException if collection is null
50       */
51      public static <T> Collection<T> decorate(Collection<T> coll) {
52          if (coll instanceof Unmodifiable) {
53              return coll;
54          }
55          return new UnmodifiableCollection<T>(coll);
56      }
57      
58      //-----------------------------------------------------------------------
59      /***
60       * Constructor that wraps (not copies).
61       * 
62       * @param coll  the collection to decorate, must not be null
63       * @throws IllegalArgumentException if collection is null
64       */
65      private UnmodifiableCollection(Collection<E> coll) {
66          super(coll);
67      }
68  
69      //-----------------------------------------------------------------------
70      public Iterator<E> iterator() {
71          return UnmodifiableIterator.<E>decorate(getCollection().iterator());
72      }
73  
74      public boolean add(E object) {
75          throw new UnsupportedOperationException();
76      }
77  
78      public boolean addAll(Collection<? extends E> coll) {
79          throw new UnsupportedOperationException();
80      }
81  
82      public void clear() {
83          throw new UnsupportedOperationException();
84      }
85  
86      public boolean remove(Object object) {
87          throw new UnsupportedOperationException();
88      }
89  
90      public boolean removeAll(Collection<?> coll) {
91          throw new UnsupportedOperationException();
92      }
93  
94      public boolean retainAll(Collection<?> coll) {
95          throw new UnsupportedOperationException();
96      }
97  
98  }