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