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