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.SortedBag;
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 UnmodifiableSortedBag<E>
45 extends AbstractSortedBagDecorator<E> implements Unmodifiable {
46
47
48 private static final long serialVersionUID = -3190437252665717841L;
49
50
51
52
53
54
55
56
57
58
59
60
61 public static <E> SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag) {
62 if (bag instanceof Unmodifiable) {
63 return bag;
64 }
65 return new UnmodifiableSortedBag<>(bag);
66 }
67
68
69
70
71
72
73
74 private UnmodifiableSortedBag(final SortedBag<E> bag) {
75 super(bag);
76 }
77
78 @Override
79 public boolean add(final E object) {
80 throw new UnsupportedOperationException();
81 }
82
83 @Override
84 public boolean 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 Iterator<E> iterator() {
100 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
101 }
102
103
104
105
106
107
108
109
110
111 @SuppressWarnings("unchecked")
112 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
113 in.defaultReadObject();
114 setCollection((Collection<E>) in.readObject());
115 }
116
117 @Override
118 public boolean remove(final Object object) {
119 throw new UnsupportedOperationException();
120 }
121
122 @Override
123 public boolean remove(final Object object, final int count) {
124 throw new UnsupportedOperationException();
125 }
126
127 @Override
128 public boolean removeAll(final Collection<?> coll) {
129 throw new UnsupportedOperationException();
130 }
131
132
133
134
135 @Override
136 public boolean removeIf(final Predicate<? super E> filter) {
137 throw new UnsupportedOperationException();
138 }
139
140 @Override
141 public boolean retainAll(final Collection<?> coll) {
142 throw new UnsupportedOperationException();
143 }
144
145 @Override
146 public Set<E> uniqueSet() {
147 final Set<E> set = decorated().uniqueSet();
148 return UnmodifiableSet.unmodifiableSet(set);
149 }
150
151
152
153
154
155
156
157 private void writeObject(final ObjectOutputStream out) throws IOException {
158 out.defaultWriteObject();
159 out.writeObject(decorated());
160 }
161
162 }