UnmodifiableSet.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.set;

  18. import java.util.Collection;
  19. import java.util.Iterator;
  20. import java.util.Set;
  21. import java.util.function.Predicate;

  22. import org.apache.commons.collections4.Unmodifiable;
  23. import org.apache.commons.collections4.iterators.UnmodifiableIterator;

  24. /**
  25.  * Decorates another {@code Set} to ensure it can't be altered.
  26.  * <p>
  27.  * This class is Serializable from Commons Collections 3.1.
  28.  * </p>
  29.  * <p>
  30.  * Attempts to modify it will result in an UnsupportedOperationException.
  31.  * </p>
  32.  *
  33.  * @param <E> the type of the elements in this set
  34.  * @since 3.0
  35.  */
  36. public final class UnmodifiableSet<E>
  37.         extends AbstractSerializableSetDecorator<E>
  38.         implements Unmodifiable {

  39.     /** Serialization version */
  40.     private static final long serialVersionUID = 6499119872185240161L;

  41.     /**
  42.      * Factory method to create an unmodifiable set.
  43.      *
  44.      * @param <E> the element type
  45.      * @param set  the set to decorate, must not be null
  46.      * @return a new unmodifiable set
  47.      * @throws NullPointerException if set is null
  48.      * @since 4.0
  49.      */
  50.     public static <E> Set<E> unmodifiableSet(final Set<? extends E> set) {
  51.         if (set instanceof Unmodifiable) {
  52.             @SuppressWarnings("unchecked") // safe to upcast
  53.             final Set<E> tmpSet = (Set<E>) set;
  54.             return tmpSet;
  55.         }
  56.         return new UnmodifiableSet<>(set);
  57.     }

  58.     /**
  59.      * Constructor that wraps (not copies).
  60.      *
  61.      * @param set  the set to decorate, must not be null
  62.      * @throws NullPointerException if set is null
  63.      */
  64.     @SuppressWarnings("unchecked") // safe to upcast
  65.     private UnmodifiableSet(final Set<? extends E> set) {
  66.         super((Set<E>) set);
  67.     }

  68.     @Override
  69.     public boolean add(final E object) {
  70.         throw new UnsupportedOperationException();
  71.     }

  72.     @Override
  73.     public boolean addAll(final Collection<? extends E> coll) {
  74.         throw new UnsupportedOperationException();
  75.     }

  76.     @Override
  77.     public void clear() {
  78.         throw new UnsupportedOperationException();
  79.     }

  80.     @Override
  81.     public Iterator<E> iterator() {
  82.         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
  83.     }

  84.     @Override
  85.     public boolean remove(final Object object) {
  86.         throw new UnsupportedOperationException();
  87.     }

  88.     @Override
  89.     public boolean removeAll(final Collection<?> coll) {
  90.         throw new UnsupportedOperationException();
  91.     }

  92.     /**
  93.      * @since 4.4
  94.      */
  95.     @Override
  96.     public boolean removeIf(final Predicate<? super E> filter) {
  97.         throw new UnsupportedOperationException();
  98.     }

  99.     @Override
  100.     public boolean retainAll(final Collection<?> coll) {
  101.         throw new UnsupportedOperationException();
  102.     }

  103. }