    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.collections4.list;
    018
    019import java.util.Collection;
    020import java.util.Iterator;
    021import java.util.List;
    022import java.util.ListIterator;
    023
    024import org.apache.commons.collections4.Unmodifiable;
    025import org.apache.commons.collections4.iterators.UnmodifiableIterator;
    026import org.apache.commons.collections4.iterators.UnmodifiableListIterator;
    027
    028/**
    029 * Decorates another <code>List</code> to ensure it can't be altered.
    030 * <p>
    031 * This class is Serializable from Commons Collections 3.1.
    032 * <p>
    033 * Attempts to modify it will result in an UnsupportedOperationException.
    034 *
    035 * @since 3.0
    036 */
    037public final class UnmodifiableList<E>
    038        extends AbstractSerializableListDecorator<E>
    039        implements Unmodifiable {
    040
    041    /** Serialization version */
    042    private static final long serialVersionUID = 6595182819922443652L;
    043
    044    /**
    045     * Factory method to create an unmodifiable list.
    046     *
    047     * @param <E> the type of the elements in the list
    048     * @param list  the list to decorate, must not be null
    049     * @return a new unmodifiable list
    050     * @throws NullPointerException if list is null
    051     * @since 4.0
    052     */
    053    public static <E> List<E> unmodifiableList(final List<? extends E> list) {
    054        if (list instanceof Unmodifiable) {
    055            @SuppressWarnings("unchecked") // safe to upcast
    056            final List<E> tmpList = (List<E>) list;
    057            return tmpList;
    058        }
    059        return new UnmodifiableList<>(list);
    060    }
    061
    062    //-----------------------------------------------------------------------
    063    /**
    064     * Constructor that wraps (not copies).
    065     *
    066     * @param list  the list to decorate, must not be null
    067     * @throws NullPointerException if list is null
    068     */
    069    @SuppressWarnings("unchecked") // safe to upcast
    070    public UnmodifiableList(final List<? extends E> list) {
    071        super((List<E>) list);
    072    }
    073
    074    //-----------------------------------------------------------------------
    075    @Override
    076    public Iterator<E> iterator() {
    077        return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
    078    }
    079
    080    @Override
    081    public boolean add(final Object object) {
    082        throw new UnsupportedOperationException();
    083    }
    084
    085    @Override
    086    public boolean addAll(final Collection<? extends E> coll) {
    087        throw new UnsupportedOperationException();
    088    }
    089
    090    @Override
    091    public void clear() {
    092        throw new UnsupportedOperationException();
    093    }
    094
    095    @Override
    096    public boolean remove(final Object object) {
    097        throw new UnsupportedOperationException();
    098    }
    099
    100    @Override
    101    public boolean removeAll(final Collection<?> coll) {
    102        throw new UnsupportedOperationException();
    103    }
    104
    105    @Override
    106    public boolean retainAll(final Collection<?> coll) {
    107        throw new UnsupportedOperationException();
    108    }
    109
    110    //-----------------------------------------------------------------------
    111    @Override
    112    public ListIterator<E> listIterator() {
    113        return UnmodifiableListIterator.umodifiableListIterator(decorated().listIterator());
    114    }
    115
    116    @Override
    117    public ListIterator<E> listIterator(final int index) {
    118        return UnmodifiableListIterator.umodifiableListIterator(decorated().listIterator(index));
    119    }
    120
    121    @Override
    122    public void add(final int index, final E object) {
    123        throw new UnsupportedOperationException();
    124    }
    125
    126    @Override
    127    public boolean addAll(final int index, final Collection<? extends E> coll) {
    128        throw new UnsupportedOperationException();
    129    }
    130
    131    @Override
    132    public E remove(final int index) {
    133        throw new UnsupportedOperationException();
    134    }
    135
    136    @Override
    137    public E set(final int index, final E object) {
    138        throw new UnsupportedOperationException();
    139    }
    140
    141    @Override
    142    public List<E> subList(final int fromIndex, final int toIndex) {
    143        final List<E> sub = decorated().subList(fromIndex, toIndex);
    144        return new UnmodifiableList<>(sub);
    145    }
    146
    147}