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.BoundedCollection;
26  import org.apache.commons.collections4.iterators.AbstractListIteratorDecorator;
27  import org.apache.commons.collections4.iterators.UnmodifiableIterator;
28  
29  /**
30   * Decorates another {@code List} to fix the size preventing add/remove.
31   * <p>
32   * The add, remove, clear and retain operations are unsupported.
33   * The set method is allowed (as it doesn't change the list size).
34   * </p>
35   * <p>
36   * NOTE:
37   * Modifying the decorated list directly would results in influencing the outcome
38   * of method calls on this object. For example, the bounds of this list would reflect
39   * a newly added object to the underlying list.
40   * </p>
41   * <p>
42   * This class is Serializable from Commons Collections 3.1.
43   * </p>
44   *
45   * @param <E> the type of elements in this collection
46   * @since 3.0
47   */
48  public class FixedSizeList<E>
49          extends AbstractSerializableListDecorator<E>
50          implements BoundedCollection<E> {
51  
52      /**
53       * List iterator that only permits changes via set()
54       */
55      private final class FixedSizeListIterator extends AbstractListIteratorDecorator<E> {
56          protected FixedSizeListIterator(final ListIterator<E> iterator) {
57              super(iterator);
58          }
59          @Override
60          public void add(final Object object) {
61              throw unsupportedOperationException();
62          }
63          @Override
64          public void remove() {
65              throw unsupportedOperationException();
66          }
67      }
68  
69      /** Serialization version */
70      private static final long serialVersionUID = -2218010673611160319L;
71  
72      /**
73       * Factory method to create a fixed size list.
74       *
75       * @param <E> the type of the elements in the list
76       * @param list  the list to decorate, must not be null
77       * @return a new fixed size list
78       * @throws NullPointerException if list is null
79       * @since 4.0
80       */
81      public static <E> FixedSizeList<E> fixedSizeList(final List<E> list) {
82          return new FixedSizeList<>(list);
83      }
84  
85      private static UnsupportedOperationException unsupportedOperationException() {
86          return new UnsupportedOperationException("List is fixed size");
87      }
88  
89      /**
90       * Constructor that wraps (not copies).
91       *
92       * @param list  the list to decorate, must not be null
93       * @throws NullPointerException if list is null
94       */
95      protected FixedSizeList(final List<E> list) {
96          super(list);
97      }
98  
99      @Override
100     public boolean add(final E object) {
101         throw unsupportedOperationException();
102     }
103 
104     @Override
105     public void add(final int index, final E object) {
106         throw unsupportedOperationException();
107     }
108 
109     @Override
110     public boolean addAll(final Collection<? extends E> coll) {
111         throw unsupportedOperationException();
112     }
113 
114     @Override
115     public boolean addAll(final int index, final Collection<? extends E> coll) {
116         throw unsupportedOperationException();
117     }
118 
119     @Override
120     public void clear() {
121         throw unsupportedOperationException();
122     }
123 
124     @Override
125     public E get(final int index) {
126         return decorated().get(index);
127     }
128 
129     @Override
130     public int indexOf(final Object object) {
131         return decorated().indexOf(object);
132     }
133 
134     @Override
135     public boolean isFull() {
136         return true;
137     }
138 
139     @Override
140     public Iterator<E> iterator() {
141         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
142     }
143 
144     @Override
145     public int lastIndexOf(final Object object) {
146         return decorated().lastIndexOf(object);
147     }
148 
149     @Override
150     public ListIterator<E> listIterator() {
151         return new FixedSizeListIterator(decorated().listIterator(0));
152     }
153 
154     @Override
155     public ListIterator<E> listIterator(final int index) {
156         return new FixedSizeListIterator(decorated().listIterator(index));
157     }
158 
159     @Override
160     public int maxSize() {
161         return size();
162     }
163 
164     @Override
165     public E remove(final int index) {
166         throw unsupportedOperationException();
167     }
168 
169     @Override
170     public boolean remove(final Object object) {
171         throw unsupportedOperationException();
172     }
173 
174     @Override
175     public boolean removeAll(final Collection<?> coll) {
176         throw unsupportedOperationException();
177     }
178 
179     /**
180      * @since 4.4
181      */
182     @Override
183     public boolean removeIf(final Predicate<? super E> filter) {
184         throw unsupportedOperationException();
185     }
186 
187     @Override
188     public boolean retainAll(final Collection<?> coll) {
189         throw unsupportedOperationException();
190     }
191 
192     @Override
193     public E set(final int index, final E object) {
194         return decorated().set(index, object);
195     }
196 
197     @Override
198     public List<E> subList(final int fromIndex, final int toIndex) {
199         final List<E> sub = decorated().subList(fromIndex, toIndex);
200         return new FixedSizeList<>(sub);
201     }
202 
203 }