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.Objects;
22  import java.util.function.Predicate;
23  
24  import org.apache.commons.collections4.BoundedCollection;
25  import org.apache.commons.collections4.Unmodifiable;
26  import org.apache.commons.collections4.iterators.UnmodifiableIterator;
27  
28  /**
29   * {@link UnmodifiableBoundedCollection} decorates another
30   * {@link BoundedCollection} to ensure it can't be altered.
31   * <p>
32   * If a BoundedCollection is first wrapped in some other collection decorator,
33   * such as synchronized or predicated, the BoundedCollection methods are no
34   * longer accessible.
35   * The factory on this class will attempt to retrieve the bounded nature by
36   * examining the package scope variables.
37   * </p>
38   * <p>
39   * This class is Serializable from Commons Collections 3.1.
40   * </p>
41   * <p>
42   * Attempts to modify it will result in an UnsupportedOperationException.
43   * </p>
44   *
45   * @param <E> the type of elements in this collection
46   * @since 3.0
47   */
48  public final class UnmodifiableBoundedCollection<E> extends AbstractCollectionDecorator<E>
49          implements BoundedCollection<E>, Unmodifiable {
50  
51      /** Serialization version */
52      private static final long serialVersionUID = -7112672385450340330L;
53  
54      /**
55       * Factory method to create an unmodifiable bounded collection.
56       *
57       * @param <E> the type of the elements in the collection
58       * @param coll  the {@code BoundedCollection} to decorate, must not be null
59       * @return a new unmodifiable bounded collection
60       * @throws NullPointerException if {@code coll} is {@code null}
61       * @since 4.0
62       */
63      public static <E> BoundedCollection<E> unmodifiableBoundedCollection(final BoundedCollection<? extends E> coll) {
64          if (coll instanceof Unmodifiable) {
65              @SuppressWarnings("unchecked") // safe to upcast
66              final BoundedCollection<E> tmpColl = (BoundedCollection<E>) coll;
67              return tmpColl;
68          }
69          return new UnmodifiableBoundedCollection<>(coll);
70      }
71  
72      /**
73       * Factory method to create an unmodifiable bounded collection.
74       * <p>
75       * This method is capable of drilling down through up to 1000 other decorators
76       * to find a suitable BoundedCollection.
77       *
78       * @param <E> the type of the elements in the collection
79       * @param collection  the {@code BoundedCollection} to decorate, must not be null
80       * @return a new unmodifiable bounded collection
81       * @throws NullPointerException if coll is null
82       * @throws IllegalArgumentException if coll is not a {@code BoundedCollection}
83       * @since 4.0
84       */
85      @SuppressWarnings("unchecked")
86      public static <E> BoundedCollection<E> unmodifiableBoundedCollection(Collection<? extends E> collection) {
87          Objects.requireNonNull(collection, "collection");
88  
89          // handle decorators
90          for (int i = 0; i < 1000; i++) {  // counter to prevent infinite looping
91              if (collection instanceof BoundedCollection) {
92                  break;  // normal loop exit
93              }
94              if (collection instanceof AbstractCollectionDecorator) {
95                  collection = ((AbstractCollectionDecorator<E>) collection).decorated();
96              } else if (collection instanceof SynchronizedCollection) {
97                  collection = ((SynchronizedCollection<E>) collection).decorated();
98              }
99          }
100 
101         if (!(collection instanceof BoundedCollection)) {
102             throw new IllegalArgumentException("Collection is not a bounded collection.");
103         }
104         return new UnmodifiableBoundedCollection<>((BoundedCollection<E>) collection);
105     }
106 
107     /**
108      * Constructor that wraps (not copies).
109      *
110      * @param coll  the collection to decorate, must not be null
111      * @throws NullPointerException if coll is null
112      */
113     @SuppressWarnings("unchecked") // safe to upcast
114     private UnmodifiableBoundedCollection(final BoundedCollection<? extends E> coll) {
115         super((BoundedCollection<E>) coll);
116     }
117 
118     @Override
119     public boolean add(final E object) {
120         throw new UnsupportedOperationException();
121     }
122 
123     @Override
124     public boolean addAll(final Collection<? extends E> coll) {
125         throw new UnsupportedOperationException();
126     }
127 
128     @Override
129     public void clear() {
130         throw new UnsupportedOperationException();
131     }
132 
133     @Override
134     protected BoundedCollection<E> decorated() {
135         return (BoundedCollection<E>) super.decorated();
136     }
137 
138     @Override
139     public boolean isFull() {
140         return decorated().isFull();
141     }
142 
143     @Override
144     public Iterator<E> iterator() {
145         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
146     }
147 
148     @Override
149     public int maxSize() {
150         return decorated().maxSize();
151     }
152 
153     @Override
154     public boolean remove(final Object object) {
155         throw new UnsupportedOperationException();
156     }
157 
158     @Override
159     public boolean removeAll(final Collection<?> coll) {
160         throw new UnsupportedOperationException();
161     }
162 
163     /**
164      * @since 4.4
165      */
166     @Override
167     public boolean removeIf(final Predicate<? super E> filter) {
168         throw new UnsupportedOperationException();
169     }
170 
171     @Override
172     public boolean retainAll(final Collection<?> coll) {
173         throw new UnsupportedOperationException();
174     }
175 }