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;
023import java.util.function.Predicate;
024
025import org.apache.commons.collections4.Unmodifiable;
026import org.apache.commons.collections4.iterators.UnmodifiableIterator;
027import org.apache.commons.collections4.iterators.UnmodifiableListIterator;
028
029/**
030 * Decorates another {@code List} to ensure it can't be altered.
031 * <p>
032 * This class is Serializable from Commons Collections 3.1.
033 * </p>
034 * <p>
035 * Attempts to modify it will result in an UnsupportedOperationException.
036 * </p>
037 *
038 * @param <E> the type of the elements in the list.
039 * @since 3.0
040 */
041public final class UnmodifiableList<E>
042        extends AbstractSerializableListDecorator<E>
043        implements Unmodifiable {
044
045    /** Serialization version */
046    private static final long serialVersionUID = 6595182819922443652L;
047
048    /**
049     * Factory method to create an unmodifiable list.
050     *
051     * @param <E> the type of the elements in the list
052     * @param list  the list to decorate, must not be null
053     * @return a new unmodifiable list
054     * @throws NullPointerException if list is null
055     * @since 4.0
056     */
057    public static <E> List<E> unmodifiableList(final List<? extends E> list) {
058        if (list instanceof Unmodifiable) {
059            @SuppressWarnings("unchecked") // safe to upcast
060            final List<E> tmpList = (List<E>) list;
061            return tmpList;
062        }
063        return new UnmodifiableList<>(list);
064    }
065
066    /**
067     * Constructor that wraps (not copies).
068     *
069     * @param list  the list to decorate, must not be null
070     * @throws NullPointerException if list is null
071     */
072    @SuppressWarnings("unchecked") // safe to upcast
073    public UnmodifiableList(final List<? extends E> list) {
074        super((List<E>) list);
075    }
076
077    @Override
078    public void add(final int index, final E object) {
079        throw new UnsupportedOperationException();
080    }
081
082    @Override
083    public boolean add(final Object object) {
084        throw new UnsupportedOperationException();
085    }
086
087    @Override
088    public boolean addAll(final Collection<? extends E> coll) {
089        throw new UnsupportedOperationException();
090    }
091
092    @Override
093    public boolean addAll(final int index, final Collection<? extends E> coll) {
094        throw new UnsupportedOperationException();
095    }
096
097    @Override
098    public void clear() {
099        throw new UnsupportedOperationException();
100    }
101
102    @Override
103    public Iterator<E> iterator() {
104        return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
105    }
106
107    @Override
108    public ListIterator<E> listIterator() {
109        return UnmodifiableListIterator.unmodifiableListIterator(decorated().listIterator());
110    }
111
112    @Override
113    public ListIterator<E> listIterator(final int index) {
114        return UnmodifiableListIterator.unmodifiableListIterator(decorated().listIterator(index));
115    }
116
117    @Override
118    public E remove(final int index) {
119        throw new UnsupportedOperationException();
120    }
121
122    @Override
123    public boolean remove(final Object object) {
124        throw new UnsupportedOperationException();
125    }
126
127    @Override
128    public boolean removeAll(final Collection<?> coll) {
129        throw new UnsupportedOperationException();
130    }
131
132    /**
133     * @since 4.4
134     */
135    @Override
136    public boolean removeIf(final Predicate<? super E> filter) {
137        throw new UnsupportedOperationException();
138    }
139
140    @Override
141    public boolean retainAll(final Collection<?> coll) {
142        throw new UnsupportedOperationException();
143    }
144
145    @Override
146    public E set(final int index, final E object) {
147        throw new UnsupportedOperationException();
148    }
149
150    @Override
151    public List<E> subList(final int fromIndex, final int toIndex) {
152        final List<E> sub = decorated().subList(fromIndex, toIndex);
153        return new UnmodifiableList<>(sub);
154    }
155
156}