UnmodifiableList.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.Unmodifiable;
  24. import org.apache.commons.collections4.iterators.UnmodifiableIterator;
  25. import org.apache.commons.collections4.iterators.UnmodifiableListIterator;

  26. /**
  27.  * Decorates another {@code List} to ensure it can't be altered.
  28.  * <p>
  29.  * This class is Serializable from Commons Collections 3.1.
  30.  * </p>
  31.  * <p>
  32.  * Attempts to modify it will result in an UnsupportedOperationException.
  33.  * </p>
  34.  *
  35.  * @param <E> the type of the elements in the list.
  36.  * @since 3.0
  37.  */
  38. public final class UnmodifiableList<E>
  39.         extends AbstractSerializableListDecorator<E>
  40.         implements Unmodifiable {

  41.     /** Serialization version */
  42.     private static final long serialVersionUID = 6595182819922443652L;

  43.     /**
  44.      * Factory method to create an unmodifiable list.
  45.      *
  46.      * @param <E> the type of the elements in the list
  47.      * @param list  the list to decorate, must not be null
  48.      * @return a new unmodifiable list
  49.      * @throws NullPointerException if list is null
  50.      * @since 4.0
  51.      */
  52.     public static <E> List<E> unmodifiableList(final List<? extends E> list) {
  53.         if (list instanceof Unmodifiable) {
  54.             @SuppressWarnings("unchecked") // safe to upcast
  55.             final List<E> tmpList = (List<E>) list;
  56.             return tmpList;
  57.         }
  58.         return new UnmodifiableList<>(list);
  59.     }

  60.     /**
  61.      * Constructor that wraps (not copies).
  62.      *
  63.      * @param list  the list to decorate, must not be null
  64.      * @throws NullPointerException if list is null
  65.      */
  66.     @SuppressWarnings("unchecked") // safe to upcast
  67.     public UnmodifiableList(final List<? extends E> list) {
  68.         super((List<E>) list);
  69.     }

  70.     @Override
  71.     public void add(final int index, final E object) {
  72.         throw new UnsupportedOperationException();
  73.     }

  74.     @Override
  75.     public boolean add(final Object object) {
  76.         throw new UnsupportedOperationException();
  77.     }

  78.     @Override
  79.     public boolean addAll(final Collection<? extends E> coll) {
  80.         throw new UnsupportedOperationException();
  81.     }

  82.     @Override
  83.     public boolean addAll(final int index, final Collection<? extends E> coll) {
  84.         throw new UnsupportedOperationException();
  85.     }

  86.     @Override
  87.     public void clear() {
  88.         throw new UnsupportedOperationException();
  89.     }

  90.     @Override
  91.     public Iterator<E> iterator() {
  92.         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
  93.     }

  94.     @Override
  95.     public ListIterator<E> listIterator() {
  96.         return UnmodifiableListIterator.unmodifiableListIterator(decorated().listIterator());
  97.     }

  98.     @Override
  99.     public ListIterator<E> listIterator(final int index) {
  100.         return UnmodifiableListIterator.unmodifiableListIterator(decorated().listIterator(index));
  101.     }

  102.     @Override
  103.     public E remove(final int index) {
  104.         throw new UnsupportedOperationException();
  105.     }

  106.     @Override
  107.     public boolean remove(final Object object) {
  108.         throw new UnsupportedOperationException();
  109.     }

  110.     @Override
  111.     public boolean removeAll(final Collection<?> coll) {
  112.         throw new UnsupportedOperationException();
  113.     }

  114.     /**
  115.      * @since 4.4
  116.      */
  117.     @Override
  118.     public boolean removeIf(final Predicate<? super E> filter) {
  119.         throw new UnsupportedOperationException();
  120.     }

  121.     @Override
  122.     public boolean retainAll(final Collection<?> coll) {
  123.         throw new UnsupportedOperationException();
  124.     }

  125.     @Override
  126.     public E set(final int index, final E object) {
  127.         throw new UnsupportedOperationException();
  128.     }

  129.     @Override
  130.     public List<E> subList(final int fromIndex, final int toIndex) {
  131.         final List<E> sub = decorated().subList(fromIndex, toIndex);
  132.         return new UnmodifiableList<>(sub);
  133.     }

  134. }