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    *      https://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.collections4.list;
18  
19  import java.util.Collection;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.ListIterator;
23  import java.util.function.Predicate;
24  
25  import org.apache.commons.collections4.Unmodifiable;
26  import org.apache.commons.collections4.iterators.UnmodifiableIterator;
27  import org.apache.commons.collections4.iterators.UnmodifiableListIterator;
28  
29  /**
30   * Decorates another {@code List} to ensure it can't be altered.
31   * <p>
32   * This class is Serializable from Commons Collections 3.1.
33   * </p>
34   * <p>
35   * Attempts to modify it will result in an UnsupportedOperationException.
36   * </p>
37   *
38   * @param <E> The type of the elements in the list.
39   * @since 3.0
40   */
41  public final class UnmodifiableList<E>
42          extends AbstractSerializableListDecorator<E>
43          implements Unmodifiable {
44  
45      /** Serialization version */
46      private static final long serialVersionUID = 6595182819922443652L;
47  
48      /**
49       * Factory method to create an unmodifiable list.
50       *
51       * @param <E> The type of the elements in the list
52       * @param list  The list to decorate, must not be null
53       * @return A new unmodifiable list
54       * @throws NullPointerException if list is null
55       * @since 4.0
56       */
57      public static <E> List<E> unmodifiableList(final List<? extends E> list) {
58          if (list instanceof Unmodifiable) {
59              @SuppressWarnings("unchecked") // safe to upcast
60              final List<E> tmpList = (List<E>) list;
61              return tmpList;
62          }
63          return new UnmodifiableList<>(list);
64      }
65  
66      /**
67       * Constructor that wraps (not copies).
68       *
69       * @param list  The list to decorate, must not be null
70       * @throws NullPointerException if list is null
71       */
72      @SuppressWarnings("unchecked") // safe to upcast
73      public UnmodifiableList(final List<? extends E> list) {
74          super((List<E>) list);
75      }
76  
77      /**
78       * Always throws {@link UnsupportedOperationException}.
79       *
80       * @param index Ignored.
81       * @param object Ignored.
82       * @throws UnsupportedOperationException Always thrown.
83       */
84      @Override
85      public void add(final int index, final E object) {
86          throw new UnsupportedOperationException();
87      }
88  
89      /**
90       * Always throws {@link UnsupportedOperationException}.
91       *
92       * @param object Ignored.
93       * @throws UnsupportedOperationException Always thrown.
94       */
95      @Override
96      public boolean add(final Object object) {
97          throw new UnsupportedOperationException();
98      }
99  
100     /**
101      * Always throws {@link UnsupportedOperationException}.
102      *
103      * @param coll Ignored.
104      * @throws UnsupportedOperationException Always thrown.
105      */
106     @Override
107     public boolean addAll(final Collection<? extends E> coll) {
108         throw new UnsupportedOperationException();
109     }
110 
111     /**
112      * Always throws {@link UnsupportedOperationException}.
113      *
114      * @param index Ignored.
115      * @param coll Ignored.
116      * @throws UnsupportedOperationException Always thrown.
117      */
118     @Override
119     public boolean addAll(final int index, final Collection<? extends E> coll) {
120         throw new UnsupportedOperationException();
121     }
122 
123     /**
124      * Always throws {@link UnsupportedOperationException}.
125      *
126      * @throws UnsupportedOperationException Always thrown.
127      */
128     @Override
129     public void clear() {
130         throw new UnsupportedOperationException();
131     }
132 
133     @Override
134     public Iterator<E> iterator() {
135         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
136     }
137 
138     @Override
139     public ListIterator<E> listIterator() {
140         return UnmodifiableListIterator.unmodifiableListIterator(decorated().listIterator());
141     }
142 
143     @Override
144     public ListIterator<E> listIterator(final int index) {
145         return UnmodifiableListIterator.unmodifiableListIterator(decorated().listIterator(index));
146     }
147 
148     /**
149      * Always throws {@link UnsupportedOperationException}.
150      *
151      * @param index Ignored.
152      * @throws UnsupportedOperationException Always thrown.
153      */
154     @Override
155     public E remove(final int index) {
156         throw new UnsupportedOperationException();
157     }
158 
159     /**
160      * Always throws {@link UnsupportedOperationException}.
161      *
162      * @param object Ignored.
163      * @throws UnsupportedOperationException Always thrown.
164      */
165     @Override
166     public boolean remove(final Object object) {
167         throw new UnsupportedOperationException();
168     }
169 
170     /**
171      * Always throws {@link UnsupportedOperationException}.
172      *
173      * @param coll Ignored.
174      * @throws UnsupportedOperationException Always thrown.
175      */
176     @Override
177     public boolean removeAll(final Collection<?> coll) {
178         throw new UnsupportedOperationException();
179     }
180 
181     /**
182      * Always throws {@link UnsupportedOperationException}.
183      *
184      * @param filter Ignored.
185      * @throws UnsupportedOperationException Always thrown.
186      * @since 4.4
187      */
188     @Override
189     public boolean removeIf(final Predicate<? super E> filter) {
190         throw new UnsupportedOperationException();
191     }
192 
193     /**
194      * Always throws {@link UnsupportedOperationException}.
195      *
196      * @param coll Ignored.
197      * @throws UnsupportedOperationException Always thrown.
198      */
199     @Override
200     public boolean retainAll(final Collection<?> coll) {
201         throw new UnsupportedOperationException();
202     }
203 
204     /**
205      * Always throws {@link UnsupportedOperationException}.
206      *
207      * @param index Ignored.
208      * @param object Ignored.
209      * @throws UnsupportedOperationException Always thrown.
210      */
211     @Override
212     public E set(final int index, final E object) {
213         throw new UnsupportedOperationException();
214     }
215 
216     @Override
217     public List<E> subList(final int fromIndex, final int toIndex) {
218         return new UnmodifiableList<>(decorated().subList(fromIndex, toIndex));
219     }
220 
221 }