    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.set;
    018
    019import java.util.Collection;
    020import java.util.Iterator;
    021import java.util.Set;
    022import java.util.function.Predicate;
    023
    024import org.apache.commons.collections4.Unmodifiable;
    025import org.apache.commons.collections4.iterators.UnmodifiableIterator;
    026
    027/**
    028 * Decorates another {@code Set} to ensure it can't be altered.
    029 * <p>
    030 * This class is Serializable from Commons Collections 3.1.
    031 * </p>
    032 * <p>
    033 * Attempts to modify it will result in an UnsupportedOperationException.
    034 * </p>
    035 *
    036 * @param <E> the type of the elements in this set
    037 * @since 3.0
    038 */
    039public final class UnmodifiableSet<E>
    040        extends AbstractSerializableSetDecorator<E>
    041        implements Unmodifiable {
    042
    043    /** Serialization version */
    044    private static final long serialVersionUID = 6499119872185240161L;
    045
    046    /**
    047     * Factory method to create an unmodifiable set.
    048     *
    049     * @param <E> the element type
    050     * @param set  the set to decorate, must not be null
    051     * @return a new unmodifiable set
    052     * @throws NullPointerException if set is null
    053     * @since 4.0
    054     */
    055    public static <E> Set<E> unmodifiableSet(final Set<? extends E> set) {
    056        if (set instanceof Unmodifiable) {
    057            @SuppressWarnings("unchecked") // safe to upcast
    058            final Set<E> tmpSet = (Set<E>) set;
    059            return tmpSet;
    060        }
    061        return new UnmodifiableSet<>(set);
    062    }
    063
    064    /**
    065     * Constructor that wraps (not copies).
    066     *
    067     * @param set  the set to decorate, must not be null
    068     * @throws NullPointerException if set is null
    069     */
    070    @SuppressWarnings("unchecked") // safe to upcast
    071    private UnmodifiableSet(final Set<? extends E> set) {
    072        super((Set<E>) set);
    073    }
    074
    075    @Override
    076    public boolean add(final E object) {
    077        throw new UnsupportedOperationException();
    078    }
    079
    080    @Override
    081    public boolean addAll(final Collection<? extends E> coll) {
    082        throw new UnsupportedOperationException();
    083    }
    084
    085    @Override
    086    public void clear() {
    087        throw new UnsupportedOperationException();
    088    }
    089
    090    @Override
    091    public Iterator<E> iterator() {
    092        return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
    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    /**
    106     * @since 4.4
    107     */
    108    @Override
    109    public boolean removeIf(final Predicate<? super E> filter) {
    110        throw new UnsupportedOperationException();
    111    }
    112
    113    @Override
    114    public boolean retainAll(final Collection<?> coll) {
    115        throw new UnsupportedOperationException();
    116    }
    117
    118}