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 public static <E> MultiSet<E> unmodifiableMultiSet(final MultiSet<? extends E> multiset) {
58 if (multiset instanceof Unmodifiable) {
59 @SuppressWarnings("unchecked")
60 final MultiSet<E> tmpMultiSet = (MultiSet<E>) multiset;
61 return tmpMultiSet;
62 }
63 return new UnmodifiableMultiSet<>(multiset);
64 }
65
66
67
68
69
70
71
72 @SuppressWarnings("unchecked")
73 private UnmodifiableMultiSet(final MultiSet<? extends E> multiset) {
74 super((MultiSet<E>) multiset);
75 }
76
77 @Override
78 public boolean add(final E object) {
79 throw new UnsupportedOperationException();
80 }
81
82 @Override
83 public int add(final E object, final int count) {
84 throw new UnsupportedOperationException();
85 }
86
87 @Override
88 public boolean addAll(final Collection<? extends E> coll) {
89 throw new UnsupportedOperationException();
90 }
91
92 @Override
93 public void clear() {
94 throw new UnsupportedOperationException();
95 }
96
97 @Override
98 public Set<MultiSet.Entry<E>> entrySet() {
99 final Set<MultiSet.Entry<E>> set = decorated().entrySet();
100 return UnmodifiableSet.unmodifiableSet(set);
101 }
102
103 @Override
104 public Iterator<E> iterator() {
105 return UnmodifiableIterator.<E>unmodifiableIterator(decorated().iterator());
106 }
107
108
109
110
111
112
113
114
115
116 @SuppressWarnings("unchecked")
117 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
118 in.defaultReadObject();
119 setCollection((Collection<E>) in.readObject());
120 }
121
122 @Override
123 public boolean remove(final Object object) {
124 throw new UnsupportedOperationException();
125 }
126
127 @Override
128 public int remove(final Object object, final int count) {
129 throw new UnsupportedOperationException();
130 }
131
132 @Override
133 public boolean removeAll(final Collection<?> coll) {
134 throw new UnsupportedOperationException();
135 }
136
137
138
139
140 @Override
141 public boolean removeIf(final Predicate<? super E> filter) {
142 throw new UnsupportedOperationException();
143 }
144
145 @Override
146 public boolean retainAll(final Collection<?> coll) {
147 throw new UnsupportedOperationException();
148 }
149
150 @Override
151 public int setCount(final E object, final int count) {
152 throw new UnsupportedOperationException();
153 }
154
155 @Override
156 public Set<E> uniqueSet() {
157 final Set<E> set = decorated().uniqueSet();
158 return UnmodifiableSet.unmodifiableSet(set);
159 }
160
161
162
163
164
165
166
167 private void writeObject(final ObjectOutputStream out) throws IOException {
168 out.defaultWriteObject();
169 out.writeObject(decorated());
170 }
171
172 }