1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.set;
18
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.Set;
22 import java.util.function.Predicate;
23
24 import org.apache.commons.collections4.Unmodifiable;
25 import org.apache.commons.collections4.iterators.UnmodifiableIterator;
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public final class UnmodifiableSet<E>
40 extends AbstractSerializableSetDecorator<E>
41 implements Unmodifiable {
42
43
44 private static final long serialVersionUID = 6499119872185240161L;
45
46
47
48
49
50
51
52
53
54
55 public static <E> Set<E> unmodifiableSet(final Set<? extends E> set) {
56 if (set instanceof Unmodifiable) {
57 @SuppressWarnings("unchecked")
58 final Set<E> tmpSet = (Set<E>) set;
59 return tmpSet;
60 }
61 return new UnmodifiableSet<>(set);
62 }
63
64
65
66
67
68
69
70 @SuppressWarnings("unchecked")
71 private UnmodifiableSet(final Set<? extends E> set) {
72 super((Set<E>) set);
73 }
74
75 @Override
76 public boolean add(final E object) {
77 throw new UnsupportedOperationException();
78 }
79
80 @Override
81 public boolean addAll(final Collection<? extends E> coll) {
82 throw new UnsupportedOperationException();
83 }
84
85 @Override
86 public void clear() {
87 throw new UnsupportedOperationException();
88 }
89
90 @Override
91 public Iterator<E> iterator() {
92 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
93 }
94
95 @Override
96 public boolean remove(final Object object) {
97 throw new UnsupportedOperationException();
98 }
99
100 @Override
101 public boolean removeAll(final Collection<?> coll) {
102 throw new UnsupportedOperationException();
103 }
104
105
106
107
108 @Override
109 public boolean removeIf(final Predicate<? super E> filter) {
110 throw new UnsupportedOperationException();
111 }
112
113 @Override
114 public boolean retainAll(final Collection<?> coll) {
115 throw new UnsupportedOperationException();
116 }
117
118 }