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