    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.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     * Factory method to create an unmodifiable collection.
    047     * <p>
    048     * If the collection passed in is already unmodifiable, it is returned.
    049     *
    050     * @param <T> the type of the elements in the collection
    051     * @param coll  the collection to decorate, must not be null
    052     * @return an unmodifiable collection
    053     * @throws NullPointerException if collection is null
    054     * @since 4.0
    055     */
    056    public static <T> Collection<T> unmodifiableCollection(final Collection<? extends T> coll) {
    057        if (coll instanceof Unmodifiable) {
    058            @SuppressWarnings("unchecked") // safe to upcast
    059            final Collection<T> tmpColl = (Collection<T>) coll;
    060            return tmpColl;
    061        }
    062        return new UnmodifiableCollection<>(coll);
    063    }
    064
    065    /**
    066     * Constructor that wraps (not copies).
    067     *
    068     * @param coll  the collection to decorate, must not be null
    069     * @throws NullPointerException if collection is null
    070     */
    071    @SuppressWarnings("unchecked") // safe to upcast
    072    private UnmodifiableCollection(final Collection<? extends E> coll) {
    073        super((Collection<E>) coll);
    074    }
    075
    076    @Override
    077    public boolean add(final E object) {
    078        throw new UnsupportedOperationException();
    079    }
    080
    081    @Override
    082    public boolean addAll(final Collection<? extends E> coll) {
    083        throw new UnsupportedOperationException();
    084    }
    085
    086    @Override
    087    public void clear() {
    088        throw new UnsupportedOperationException();
    089    }
    090
    091    @Override
    092    public Iterator<E> iterator() {
    093        return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
    094    }
    095
    096    @Override
    097    public boolean remove(final Object object) {
    098        throw new UnsupportedOperationException();
    099    }
    100
    101    @Override
    102    public boolean removeAll(final Collection<?> coll) {
    103        throw new UnsupportedOperationException();
    104    }
    105
    106    /**
    107     * @since 4.4
    108     */
    109    @Override
    110    public boolean removeIf(final Predicate<? super E> filter) {
    111        throw new UnsupportedOperationException();
    112    }
    113
    114    @Override
    115    public boolean retainAll(final Collection<?> coll) {
    116        throw new UnsupportedOperationException();
    117    }
    118
    119}