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    *      https://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.bag;
18  
19  import java.io.IOException;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.Set;
25  import java.util.function.Predicate;
26  
27  import org.apache.commons.collections4.Bag;
28  import org.apache.commons.collections4.Unmodifiable;
29  import org.apache.commons.collections4.iterators.UnmodifiableIterator;
30  import org.apache.commons.collections4.multiset.UnmodifiableMultiSet;
31  import org.apache.commons.collections4.set.UnmodifiableSet;
32  
33  /**
34   * Decorates another {@link Bag} to ensure it can't be altered.
35   * <p>
36   * This class is Serializable from Commons Collections 3.1.
37   * </p>
38   * <p>
39   * Attempts to modify it will result in an UnsupportedOperationException.
40   * </p>
41   *
42   * @param <E> The type of elements in this bag
43   * @since 3.0
44   * @deprecated Since 4.6.0, use {@link UnmodifiableMultiSet} instead.
45   */
46  @Deprecated
47  public final class UnmodifiableBag<E>
48          extends AbstractBagDecorator<E> implements Unmodifiable {
49  
50      /** Serialization version */
51      private static final long serialVersionUID = -1873799975157099624L;
52  
53      /**
54       * Factory method to create an unmodifiable bag.
55       * <p>
56       * If the bag passed in is already unmodifiable, it is returned.
57       *
58       * @param <E> The type of the elements in the bag
59       * @param bag  The bag to decorate, must not be null
60       * @return An unmodifiable Bag
61       * @throws NullPointerException if bag is null
62       * @since 4.0
63       */
64      public static <E> Bag<E> unmodifiableBag(final Bag<? extends E> bag) {
65          if (bag instanceof Unmodifiable) {
66              @SuppressWarnings("unchecked") // safe to upcast
67              final Bag<E> tmpBag = (Bag<E>) bag;
68              return tmpBag;
69          }
70          return new UnmodifiableBag<>(bag);
71      }
72  
73      /**
74       * Constructor that wraps (not copies).
75       *
76       * @param bag  The bag to decorate, must not be null
77       * @throws NullPointerException if bag is null
78       */
79      @SuppressWarnings("unchecked") // safe to upcast
80      private UnmodifiableBag(final Bag<? extends E> bag) {
81          super((Bag<E>) bag);
82      }
83  
84      /**
85       * Always throws {@link UnsupportedOperationException}.
86       *
87       * @param object Ignored.
88       * @throws UnsupportedOperationException Always thrown.
89       */
90      @Override
91      public boolean add(final E object) {
92          throw new UnsupportedOperationException();
93      }
94  
95      /**
96       * Always throws {@link UnsupportedOperationException}.
97       *
98       * @param object Ignored.
99       * @param count Ignored.
100      * @throws UnsupportedOperationException Always thrown.
101      */
102     @Override
103     public boolean add(final E object, final int count) {
104         throw new UnsupportedOperationException();
105     }
106 
107     /**
108      * Always throws {@link UnsupportedOperationException}.
109      *
110      * @param coll Ignored.
111      * @throws UnsupportedOperationException Always thrown.
112      */
113     @Override
114     public boolean addAll(final Collection<? extends E> coll) {
115         throw new UnsupportedOperationException();
116     }
117 
118     /**
119      * Always throws {@link UnsupportedOperationException}.
120      *
121      * @throws UnsupportedOperationException Always thrown.
122      */
123     @Override
124     public void clear() {
125         throw new UnsupportedOperationException();
126     }
127 
128     @Override
129     public Iterator<E> iterator() {
130         return UnmodifiableIterator.<E>unmodifiableIterator(decorated().iterator());
131     }
132 
133     /**
134      * Deserializes the collection in using a custom routine.
135      *
136      * @param in  The input stream
137      * @throws IOException Thrown if an error occurs while reading from the stream
138      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
139      * @throws ClassCastException if deserialized object has wrong type
140      */
141     @SuppressWarnings("unchecked") // will throw CCE, see Javadoc
142     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
143         in.defaultReadObject();
144         setCollection((Collection<E>) in.readObject());
145     }
146 
147     /**
148      * Always throws {@link UnsupportedOperationException}.
149      *
150      * @param object Ignored.
151      * @throws UnsupportedOperationException Always thrown.
152      */
153     @Override
154     public boolean remove(final Object object) {
155         throw new UnsupportedOperationException();
156     }
157 
158     /**
159      * Always throws {@link UnsupportedOperationException}.
160      *
161      * @param object Ignored.
162      * @param count Ignored.
163      * @throws UnsupportedOperationException Always thrown.
164      */
165     @Override
166     public boolean remove(final Object object, final int count) {
167         throw new UnsupportedOperationException();
168     }
169 
170     /**
171      * Always throws {@link UnsupportedOperationException}.
172      *
173      * @param coll Ignored.
174      * @throws UnsupportedOperationException Always thrown.
175      */
176     @Override
177     public boolean removeAll(final Collection<?> coll) {
178         throw new UnsupportedOperationException();
179     }
180 
181     /**
182      * Always throws {@link UnsupportedOperationException}.
183      *
184      * @param filter Ignored.
185      * @throws UnsupportedOperationException Always thrown.
186      * @since 4.4
187      */
188     @Override
189     public boolean removeIf(final Predicate<? super E> filter) {
190         throw new UnsupportedOperationException();
191     }
192 
193     /**
194      * Always throws {@link UnsupportedOperationException}.
195      *
196      * @param coll Ignored.
197      * @throws UnsupportedOperationException Always thrown.
198      */
199     @Override
200     public boolean retainAll(final Collection<?> coll) {
201         throw new UnsupportedOperationException();
202     }
203 
204     @Override
205     public Set<E> uniqueSet() {
206         return UnmodifiableSet.<E>unmodifiableSet(decorated().uniqueSet());
207     }
208 
209     /**
210      * Serializes this object to an ObjectOutputStream.
211      *
212      * @param out The target ObjectOutputStream.
213      * @throws IOException thrown when an I/O errors occur writing to the target stream.
214      */
215     private void writeObject(final ObjectOutputStream out) throws IOException {
216         out.defaultWriteObject();
217         out.writeObject(decorated());
218     }
219 
220 }