UnmodifiableCollection.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.collection;

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

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

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

  38.     /** Serialization version */
  39.     private static final long serialVersionUID = -239892006883819945L;

  40.     /**
  41.      * Factory method to create an unmodifiable collection.
  42.      * <p>
  43.      * If the collection passed in is already unmodifiable, it is returned.
  44.      *
  45.      * @param <T> the type of the elements in the collection
  46.      * @param coll  the collection to decorate, must not be null
  47.      * @return an unmodifiable collection
  48.      * @throws NullPointerException if collection is null
  49.      * @since 4.0
  50.      */
  51.     public static <T> Collection<T> unmodifiableCollection(final Collection<? extends T> coll) {
  52.         if (coll instanceof Unmodifiable) {
  53.             @SuppressWarnings("unchecked") // safe to upcast
  54.             final Collection<T> tmpColl = (Collection<T>) coll;
  55.             return tmpColl;
  56.         }
  57.         return new UnmodifiableCollection<>(coll);
  58.     }

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

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

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

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

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

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

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

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

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

  104. }