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