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