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.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.set.UnmodifiableSet;
31  
32  /**
33   * Decorates another {@link Bag} to ensure it can't be altered.
34   * <p>
35   * This class is Serializable from Commons Collections 3.1.
36   * </p>
37   * <p>
38   * Attempts to modify it will result in an UnsupportedOperationException.
39   * </p>
40   *
41   * @param <E> the type of elements in this bag
42   * @since 3.0
43   */
44  public final class UnmodifiableBag<E>
45          extends AbstractBagDecorator<E> implements Unmodifiable {
46  
47      /** Serialization version */
48      private static final long serialVersionUID = -1873799975157099624L;
49  
50      /**
51       * Factory method to create an unmodifiable bag.
52       * <p>
53       * If the bag passed in is already unmodifiable, it is returned.
54       *
55       * @param <E> the type of the elements in the bag
56       * @param bag  the bag to decorate, must not be null
57       * @return an unmodifiable Bag
58       * @throws NullPointerException if bag is null
59       * @since 4.0
60       */
61      public static <E> Bag<E> unmodifiableBag(final Bag<? extends E> bag) {
62          if (bag instanceof Unmodifiable) {
63              @SuppressWarnings("unchecked") // safe to upcast
64              final Bag<E> tmpBag = (Bag<E>) bag;
65              return tmpBag;
66          }
67          return new UnmodifiableBag<>(bag);
68      }
69  
70      /**
71       * Constructor that wraps (not copies).
72       *
73       * @param bag  the bag to decorate, must not be null
74       * @throws NullPointerException if bag is null
75       */
76      @SuppressWarnings("unchecked") // safe to upcast
77      private UnmodifiableBag(final Bag<? extends E> bag) {
78          super((Bag<E>) bag);
79      }
80  
81      @Override
82      public boolean add(final E object) {
83          throw new UnsupportedOperationException();
84      }
85  
86      @Override
87      public boolean add(final E object, final int count) {
88          throw new UnsupportedOperationException();
89      }
90  
91      @Override
92      public boolean addAll(final Collection<? extends E> coll) {
93          throw new UnsupportedOperationException();
94      }
95  
96      @Override
97      public void clear() {
98          throw new UnsupportedOperationException();
99      }
100 
101     @Override
102     public Iterator<E> iterator() {
103         return UnmodifiableIterator.<E>unmodifiableIterator(decorated().iterator());
104     }
105 
106     /**
107      * Read the collection in using a custom routine.
108      *
109      * @param in  the input stream
110      * @throws IOException if an error occurs while reading from the stream
111      * @throws ClassNotFoundException if an object read from the stream can not be loaded
112      * @throws ClassCastException if deserialized object has wrong type
113      */
114     @SuppressWarnings("unchecked") // will throw CCE, see Javadoc
115     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
116         in.defaultReadObject();
117         setCollection((Collection<E>) in.readObject());
118     }
119 
120     @Override
121     public boolean remove(final Object object) {
122         throw new UnsupportedOperationException();
123     }
124 
125     @Override
126     public boolean remove(final Object object, final int count) {
127         throw new UnsupportedOperationException();
128     }
129 
130     @Override
131     public boolean removeAll(final Collection<?> coll) {
132         throw new UnsupportedOperationException();
133     }
134 
135     /**
136      * @since 4.4
137      */
138     @Override
139     public boolean removeIf(final Predicate<? super E> filter) {
140         throw new UnsupportedOperationException();
141     }
142 
143     @Override
144     public boolean retainAll(final Collection<?> coll) {
145         throw new UnsupportedOperationException();
146     }
147 
148     @Override
149     public Set<E> uniqueSet() {
150         final Set<E> set = decorated().uniqueSet();
151         return UnmodifiableSet.<E>unmodifiableSet(set);
152     }
153 
154     /**
155      * Write the collection out using a custom routine.
156      *
157      * @param out  the output stream
158      * @throws IOException if an error occurs while writing to the stream
159      */
160     private void writeObject(final ObjectOutputStream out) throws IOException {
161         out.defaultWriteObject();
162         out.writeObject(decorated());
163     }
164 
165 }