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.SortedBag;
28  import org.apache.commons.collections4.Unmodifiable;
29  import org.apache.commons.collections4.iterators.UnmodifiableIterator;
30  import org.apache.commons.collections4.multiset.UnmodifiableSortedMultiSet;
31  import org.apache.commons.collections4.set.UnmodifiableSet;
32  
33  /**
34   * Decorates another {@link SortedBag} 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 UnmodifiableSortedMultiSet} instead.
45   */
46  @Deprecated
47  public final class UnmodifiableSortedBag<E>
48          extends AbstractSortedBagDecorator<E> implements Unmodifiable {
49  
50      /** Serialization version */
51      private static final long serialVersionUID = -3190437252665717841L;
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 SortedBag
61       * @throws NullPointerException if bag is null
62       * @since 4.0
63       */
64      public static <E> SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag) {
65          if (bag instanceof Unmodifiable) {
66              return bag;
67          }
68          return new UnmodifiableSortedBag<>(bag);
69      }
70  
71      /**
72       * Constructor that wraps (not copies).
73       *
74       * @param bag  The bag to decorate, must not be null
75       * @throws NullPointerException if bag is null
76       */
77      private UnmodifiableSortedBag(final SortedBag<E> bag) {
78          super(bag);
79      }
80  
81      /**
82       * Always throws {@link UnsupportedOperationException}.
83       *
84       * @param object Ignored.
85       * @throws UnsupportedOperationException Always thrown.
86       */
87      @Override
88      public boolean add(final E object) {
89          throw new UnsupportedOperationException();
90      }
91  
92      /**
93       * Always throws {@link UnsupportedOperationException}.
94       *
95       * @param object Ignored.
96       * @param count Ignored.
97       * @throws UnsupportedOperationException Always thrown.
98       */
99      @Override
100     public boolean add(final E object, final int count) {
101         throw new UnsupportedOperationException();
102     }
103 
104     /**
105      * Always throws {@link UnsupportedOperationException}.
106      *
107      * @param coll Ignored.
108      * @throws UnsupportedOperationException Always thrown.
109      */
110     @Override
111     public boolean addAll(final Collection<? extends E> coll) {
112         throw new UnsupportedOperationException();
113     }
114 
115     /**
116      * Always throws {@link UnsupportedOperationException}.
117      *
118      * @throws UnsupportedOperationException Always thrown.
119      */
120     @Override
121     public void clear() {
122         throw new UnsupportedOperationException();
123     }
124 
125     @Override
126     public Iterator<E> iterator() {
127         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
128     }
129 
130     /**
131      * Deserializes the collection in using a custom routine.
132      *
133      * @param in  The input stream
134      * @throws IOException Thrown if an error occurs while reading from the stream
135      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
136      * @throws ClassCastException if deserialized object has wrong type
137      */
138     @SuppressWarnings("unchecked") // will throw CCE, see Javadoc
139     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
140         in.defaultReadObject();
141         setCollection((Collection<E>) in.readObject());
142     }
143 
144     /**
145      * Always throws {@link UnsupportedOperationException}.
146      *
147      * @param object Ignored.
148      * @throws UnsupportedOperationException Always thrown.
149      */
150     @Override
151     public boolean remove(final Object object) {
152         throw new UnsupportedOperationException();
153     }
154 
155     /**
156      * Always throws {@link UnsupportedOperationException}.
157      *
158      * @param object Ignored.
159      * @param count Ignored.
160      * @throws UnsupportedOperationException Always thrown.
161      */
162     @Override
163     public boolean remove(final Object object, final int count) {
164         throw new UnsupportedOperationException();
165     }
166 
167     /**
168      * Always throws {@link UnsupportedOperationException}.
169      *
170      * @param coll Ignored.
171      * @throws UnsupportedOperationException Always thrown.
172      */
173     @Override
174     public boolean removeAll(final Collection<?> coll) {
175         throw new UnsupportedOperationException();
176     }
177 
178     /**
179      * Always throws {@link UnsupportedOperationException}.
180      *
181      * @param filter Ignored.
182      * @throws UnsupportedOperationException Always thrown.
183      * @since 4.4
184      */
185     @Override
186     public boolean removeIf(final Predicate<? super E> filter) {
187         throw new UnsupportedOperationException();
188     }
189 
190     /**
191      * Always throws {@link UnsupportedOperationException}.
192      *
193      * @param coll Ignored.
194      * @throws UnsupportedOperationException Always thrown.
195      */
196     @Override
197     public boolean retainAll(final Collection<?> coll) {
198         throw new UnsupportedOperationException();
199     }
200 
201     @Override
202     public Set<E> uniqueSet() {
203         final Set<E> set = decorated().uniqueSet();
204         return UnmodifiableSet.unmodifiableSet(set);
205     }
206 
207     /**
208      * Serializes this object to an ObjectOutputStream.
209      *
210      * @param out The target ObjectOutputStream.
211      * @throws IOException thrown when an I/O errors occur writing to the target stream.
212      */
213     private void writeObject(final ObjectOutputStream out) throws IOException {
214         out.defaultWriteObject();
215         out.writeObject(decorated());
216     }
217 
218 }