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.list;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.ListIterator;
22  
23  import net.sf.collections15.Unmodifiable;
24  import net.sf.collections15.iterators.UnmodifiableIterator;
25  import net.sf.collections15.iterators.UnmodifiableListIterator;
26  
27  /***
28   * Decorates another <code>List</code> to ensure it can't be altered.
29   * <p>
30   * This class is Serializable from Commons Collections 3.1.
31   *
32   * @since Commons Collections 3.0
33   * @version $Revision: 1.2 $ $Date: 2005/05/03 22:45:38 $
34   * 
35   * @author Stephen Colebourne
36   */
37  public final class UnmodifiableList<E>
38          extends AbstractSerializableListDecorator<E>
39          implements Unmodifiable {
40  
41  	// Class fields
42  	// -------------------------------------------------------------------------
43  	
44      /*** Serialization version */
45      private static final long serialVersionUID = 6595182819922443652L;
46  
47      // Class methods
48      // -------------------------------------------------------------------------
49      
50      /***
51       * Factory method to create an unmodifiable list.
52       * 
53       * @param list the list to decorate, must not be <code>null</code>
54       * 
55       * @return an <code>{@link Unmodifiable unmodifiable}</code> version of the
56       * 	provided list; if it is already unmodifiable, the same instance is
57       * 	returned
58       * 
59       * @throws IllegalArgumentException if list is <code>null</code>
60       */
61      public static <T> List<T> decorate(final List<T> list) {
62          if (list instanceof Unmodifiable) {
63              return list;
64          }
65          return new UnmodifiableList<T>(list);
66      }
67  
68      // Constructors
69      // -------------------------------------------------------------------------
70      
71      /***
72       * Constructor that wraps (not copies).
73       * 
74       * @param list the list to decorate, must not be <code>null</code>
75       * @throws IllegalArgumentException if list is <code>null</code>
76       */
77      private UnmodifiableList(final List<E> list) {
78          super(list);
79      }
80  
81      // List interface methods.
82      // -------------------------------------------------------------------------
83      
84      public Iterator<E> iterator() {
85          return UnmodifiableIterator.decorate(getCollection().iterator());
86      }
87  
88      public boolean add(final E object) {
89          throw new UnsupportedOperationException();
90      }
91  
92      public boolean addAll(final Collection<? extends E> coll) {
93          throw new UnsupportedOperationException();
94      }
95  
96      public void clear() {
97          throw new UnsupportedOperationException();
98      }
99  
100     public boolean remove(final Object object) {
101         throw new UnsupportedOperationException();
102     }
103 
104     public boolean removeAll(final Collection<?> coll) {
105         throw new UnsupportedOperationException();
106     }
107 
108     public boolean retainAll(final Collection<?> coll) {
109         throw new UnsupportedOperationException();
110     }
111 
112     public ListIterator<E> listIterator() {
113         return UnmodifiableListIterator.decorate(getList().listIterator());
114     }
115 
116     public ListIterator<E> listIterator(final int index) {
117         return UnmodifiableListIterator.decorate(getList().listIterator(index));
118     }
119 
120     public void add(final int index, final E object) {
121         throw new UnsupportedOperationException();
122     }
123 
124     public boolean addAll(final int index, final Collection<? extends E> coll) {
125         throw new UnsupportedOperationException();
126     }
127 
128     public E remove(final int index) {
129         throw new UnsupportedOperationException();
130     }
131 
132     public E set(final int index, final E object) {
133         throw new UnsupportedOperationException();
134     }
135 
136     public List<E> subList(final int fromIndex, final int toIndex) {
137         List<E> sub = getList().subList(fromIndex, toIndex);
138         return UnmodifiableList.decorate(sub);
139     }
140 }