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.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</code> 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   * @since 3.0
39   */
40  public final class UnmodifiableList<E>
41          extends AbstractSerializableListDecorator<E>
42          implements Unmodifiable {
43  
44      /** Serialization version */
45      private static final long serialVersionUID = 6595182819922443652L;
46  
47      /**
48       * Factory method to create an unmodifiable list.
49       *
50       * @param <E> the type of the elements in the list
51       * @param list  the list to decorate, must not be null
52       * @return a new unmodifiable list
53       * @throws NullPointerException if list is null
54       * @since 4.0
55       */
56      public static <E> List<E> unmodifiableList(final List<? extends E> list) {
57          if (list instanceof Unmodifiable) {
58              @SuppressWarnings("unchecked") // safe to upcast
59              final List<E> tmpList = (List<E>) list;
60              return tmpList;
61          }
62          return new UnmodifiableList<>(list);
63      }
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      @Override
79      public Iterator<E> iterator() {
80          return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
81      }
82  
83      @Override
84      public boolean add(final Object object) {
85          throw new UnsupportedOperationException();
86      }
87  
88      @Override
89      public boolean addAll(final Collection<? extends E> coll) {
90          throw new UnsupportedOperationException();
91      }
92  
93      @Override
94      public void clear() {
95          throw new UnsupportedOperationException();
96      }
97  
98      @Override
99      public boolean remove(final Object object) {
100         throw new UnsupportedOperationException();
101     }
102 
103     /**
104      * @since 4.4
105      */
106     @Override
107     public boolean removeIf(Predicate<? super E> filter) {
108         throw new UnsupportedOperationException();
109     }
110 
111     @Override
112     public boolean removeAll(final Collection<?> coll) {
113         throw new UnsupportedOperationException();
114     }
115 
116     @Override
117     public boolean retainAll(final Collection<?> coll) {
118         throw new UnsupportedOperationException();
119     }
120 
121     //-----------------------------------------------------------------------
122     @Override
123     public ListIterator<E> listIterator() {
124         return UnmodifiableListIterator.umodifiableListIterator(decorated().listIterator());
125     }
126 
127     @Override
128     public ListIterator<E> listIterator(final int index) {
129         return UnmodifiableListIterator.umodifiableListIterator(decorated().listIterator(index));
130     }
131 
132     @Override
133     public void add(final int index, final E object) {
134         throw new UnsupportedOperationException();
135     }
136 
137     @Override
138     public boolean addAll(final int index, final Collection<? extends E> coll) {
139         throw new UnsupportedOperationException();
140     }
141 
142     @Override
143     public E remove(final int index) {
144         throw new UnsupportedOperationException();
145     }
146 
147     @Override
148     public E set(final int index, final E object) {
149         throw new UnsupportedOperationException();
150     }
151 
152     @Override
153     public List<E> subList(final int fromIndex, final int toIndex) {
154         final List<E> sub = decorated().subList(fromIndex, toIndex);
155         return new UnmodifiableList<>(sub);
156     }
157 
158 }