View Javadoc
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  
19  import java.util.Collection;
20  import java.util.Iterator;
21  import java.util.function.Predicate;
22  
23  import org.apache.commons.collections4.Unmodifiable;
24  import org.apache.commons.collections4.iterators.UnmodifiableIterator;
25  
26  /**
27   * Decorates another {@link Collection} to ensure it can't be altered.
28   * <p>
29   * This class is Serializable from Commons Collections 3.1.
30   * </p>
31   * <p>
32   * Attempts to modify it will result in an UnsupportedOperationException.
33   * </p>
34   *
35   * @param <E> the type of the elements in the collection
36   * @since 3.0
37   */
38  public final class UnmodifiableCollection<E>
39          extends AbstractCollectionDecorator<E>
40          implements Unmodifiable {
41  
42      /** Serialization version */
43      private static final long serialVersionUID = -239892006883819945L;
44  
45      /**
46       * Factory method to create an unmodifiable collection.
47       * <p>
48       * If the collection passed in is already unmodifiable, it is returned.
49       *
50       * @param <T> the type of the elements in the collection
51       * @param coll  the collection to decorate, must not be null
52       * @return an unmodifiable collection
53       * @throws NullPointerException if collection is null
54       * @since 4.0
55       */
56      public static <T> Collection<T> unmodifiableCollection(final Collection<? extends T> coll) {
57          if (coll instanceof Unmodifiable) {
58              @SuppressWarnings("unchecked") // safe to upcast
59              final Collection<T> tmpColl = (Collection<T>) coll;
60              return tmpColl;
61          }
62          return new UnmodifiableCollection<>(coll);
63      }
64  
65      /**
66       * Constructor that wraps (not copies).
67       *
68       * @param coll  the collection to decorate, must not be null
69       * @throws NullPointerException if collection is null
70       */
71      @SuppressWarnings("unchecked") // safe to upcast
72      private UnmodifiableCollection(final Collection<? extends E> coll) {
73          super((Collection<E>) coll);
74      }
75  
76      @Override
77      public boolean add(final E object) {
78          throw new UnsupportedOperationException();
79      }
80  
81      @Override
82      public boolean addAll(final Collection<? extends E> coll) {
83          throw new UnsupportedOperationException();
84      }
85  
86      @Override
87      public void clear() {
88          throw new UnsupportedOperationException();
89      }
90  
91      @Override
92      public Iterator<E> iterator() {
93          return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
94      }
95  
96      @Override
97      public boolean remove(final Object object) {
98          throw new UnsupportedOperationException();
99      }
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 }