1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.Unmodifiable;
29 import org.apache.commons.collections4.iterators.UnmodifiableIterator;
30 import org.apache.commons.collections4.set.UnmodifiableSet;
31
32
33
34
35
36
37
38
39
40
41 public final class UnmodifiableMultiSet<E>
42 extends AbstractMultiSetDecorator<E> implements Unmodifiable {
43
44
45 private static final long serialVersionUID = 20150611L;
46
47
48
49
50
51
52
53
54
55
56
57
58 public static <E> MultiSet<E> unmodifiableMultiSet(final MultiSet<? extends E> multiset) {
59 if (multiset instanceof Unmodifiable) {
60 @SuppressWarnings("unchecked")
61 final MultiSet<E> tmpMultiSet = (MultiSet<E>) multiset;
62 return tmpMultiSet;
63 }
64 return new UnmodifiableMultiSet<>(multiset);
65 }
66
67
68
69
70
71
72
73 @SuppressWarnings("unchecked")
74 private UnmodifiableMultiSet(final MultiSet<? extends E> multiset) {
75 super((MultiSet<E>) multiset);
76 }
77
78 @Override
79 public boolean add(final E object) {
80 throw new UnsupportedOperationException();
81 }
82
83 @Override
84 public int add(final E object, final int count) {
85 throw new UnsupportedOperationException();
86 }
87
88 @Override
89 public boolean addAll(final Collection<? extends E> coll) {
90 throw new UnsupportedOperationException();
91 }
92
93 @Override
94 public void clear() {
95 throw new UnsupportedOperationException();
96 }
97
98 @Override
99 public Set<MultiSet.Entry<E>> entrySet() {
100 final Set<MultiSet.Entry<E>> set = decorated().entrySet();
101 return UnmodifiableSet.unmodifiableSet(set);
102 }
103
104 @Override
105 public Iterator<E> iterator() {
106 return UnmodifiableIterator.<E>unmodifiableIterator(decorated().iterator());
107 }
108
109
110
111
112
113
114
115
116
117 @SuppressWarnings("unchecked")
118 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
119 in.defaultReadObject();
120 setCollection((Collection<E>) in.readObject());
121 }
122
123 @Override
124 public boolean remove(final Object object) {
125 throw new UnsupportedOperationException();
126 }
127
128 @Override
129 public int remove(final Object object, final int count) {
130 throw new UnsupportedOperationException();
131 }
132
133 @Override
134 public boolean removeAll(final Collection<?> coll) {
135 throw new UnsupportedOperationException();
136 }
137
138
139
140
141 @Override
142 public boolean removeIf(final Predicate<? super E> filter) {
143 throw new UnsupportedOperationException();
144 }
145
146 @Override
147 public boolean retainAll(final Collection<?> coll) {
148 throw new UnsupportedOperationException();
149 }
150
151 @Override
152 public int setCount(final E object, final int count) {
153 throw new UnsupportedOperationException();
154 }
155
156 @Override
157 public Set<E> uniqueSet() {
158 final Set<E> set = decorated().uniqueSet();
159 return UnmodifiableSet.unmodifiableSet(set);
160 }
161
162
163
164
165
166
167
168 private void writeObject(final ObjectOutputStream out) throws IOException {
169 out.defaultWriteObject();
170 out.writeObject(decorated());
171 }
172
173 }