FixedSizeList.java

  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. import java.util.Collection;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.ListIterator;
  22. import java.util.function.Predicate;

  23. import org.apache.commons.collections4.BoundedCollection;
  24. import org.apache.commons.collections4.iterators.AbstractListIteratorDecorator;
  25. import org.apache.commons.collections4.iterators.UnmodifiableIterator;

  26. /**
  27.  * Decorates another {@code List} to fix the size preventing add/remove.
  28.  * <p>
  29.  * The add, remove, clear and retain operations are unsupported.
  30.  * The set method is allowed (as it doesn't change the list size).
  31.  * </p>
  32.  * <p>
  33.  * NOTE:
  34.  * Modifying the decorated list directly would results in influencing the outcome
  35.  * of method calls on this object. For example, the bounds of this list would reflect
  36.  * a newly added object to the underlying list.
  37.  * </p>
  38.  * <p>
  39.  * This class is Serializable from Commons Collections 3.1.
  40.  * </p>
  41.  *
  42.  * @param <E> the type of elements in this collection
  43.  * @since 3.0
  44.  */
  45. public class FixedSizeList<E>
  46.         extends AbstractSerializableListDecorator<E>
  47.         implements BoundedCollection<E> {

  48.     /**
  49.      * List iterator that only permits changes via set()
  50.      */
  51.     private final class FixedSizeListIterator extends AbstractListIteratorDecorator<E> {
  52.         protected FixedSizeListIterator(final ListIterator<E> iterator) {
  53.             super(iterator);
  54.         }
  55.         @Override
  56.         public void add(final Object object) {
  57.             throw unsupportedOperationException();
  58.         }
  59.         @Override
  60.         public void remove() {
  61.             throw unsupportedOperationException();
  62.         }
  63.     }

  64.     /** Serialization version */
  65.     private static final long serialVersionUID = -2218010673611160319L;

  66.     /**
  67.      * Factory method to create a fixed size list.
  68.      *
  69.      * @param <E> the type of the elements in the list
  70.      * @param list  the list to decorate, must not be null
  71.      * @return a new fixed size list
  72.      * @throws NullPointerException if list is null
  73.      * @since 4.0
  74.      */
  75.     public static <E> FixedSizeList<E> fixedSizeList(final List<E> list) {
  76.         return new FixedSizeList<>(list);
  77.     }

  78.     private static UnsupportedOperationException unsupportedOperationException() {
  79.         return new UnsupportedOperationException("List is fixed size");
  80.     }

  81.     /**
  82.      * Constructor that wraps (not copies).
  83.      *
  84.      * @param list  the list to decorate, must not be null
  85.      * @throws NullPointerException if list is null
  86.      */
  87.     protected FixedSizeList(final List<E> list) {
  88.         super(list);
  89.     }

  90.     @Override
  91.     public boolean add(final E object) {
  92.         throw unsupportedOperationException();
  93.     }

  94.     @Override
  95.     public void add(final int index, final E object) {
  96.         throw unsupportedOperationException();
  97.     }

  98.     @Override
  99.     public boolean addAll(final Collection<? extends E> coll) {
  100.         throw unsupportedOperationException();
  101.     }

  102.     @Override
  103.     public boolean addAll(final int index, final Collection<? extends E> coll) {
  104.         throw unsupportedOperationException();
  105.     }

  106.     @Override
  107.     public void clear() {
  108.         throw unsupportedOperationException();
  109.     }

  110.     @Override
  111.     public E get(final int index) {
  112.         return decorated().get(index);
  113.     }

  114.     @Override
  115.     public int indexOf(final Object object) {
  116.         return decorated().indexOf(object);
  117.     }

  118.     @Override
  119.     public boolean isFull() {
  120.         return true;
  121.     }

  122.     @Override
  123.     public Iterator<E> iterator() {
  124.         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
  125.     }

  126.     @Override
  127.     public int lastIndexOf(final Object object) {
  128.         return decorated().lastIndexOf(object);
  129.     }

  130.     @Override
  131.     public ListIterator<E> listIterator() {
  132.         return new FixedSizeListIterator(decorated().listIterator(0));
  133.     }

  134.     @Override
  135.     public ListIterator<E> listIterator(final int index) {
  136.         return new FixedSizeListIterator(decorated().listIterator(index));
  137.     }

  138.     @Override
  139.     public int maxSize() {
  140.         return size();
  141.     }

  142.     @Override
  143.     public E remove(final int index) {
  144.         throw unsupportedOperationException();
  145.     }

  146.     @Override
  147.     public boolean remove(final Object object) {
  148.         throw unsupportedOperationException();
  149.     }

  150.     @Override
  151.     public boolean removeAll(final Collection<?> coll) {
  152.         throw unsupportedOperationException();
  153.     }

  154.     /**
  155.      * @since 4.4
  156.      */
  157.     @Override
  158.     public boolean removeIf(final Predicate<? super E> filter) {
  159.         throw unsupportedOperationException();
  160.     }

  161.     @Override
  162.     public boolean retainAll(final Collection<?> coll) {
  163.         throw unsupportedOperationException();
  164.     }

  165.     @Override
  166.     public E set(final int index, final E object) {
  167.         return decorated().set(index, object);
  168.     }

  169.     @Override
  170.     public List<E> subList(final int fromIndex, final int toIndex) {
  171.         final List<E> sub = decorated().subList(fromIndex, toIndex);
  172.         return new FixedSizeList<>(sub);
  173.     }

  174. }