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.Objects;
22 import java.util.function.Predicate;
23
24 import org.apache.commons.collections4.BoundedCollection;
25 import org.apache.commons.collections4.Unmodifiable;
26 import org.apache.commons.collections4.iterators.UnmodifiableIterator;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public final class UnmodifiableBoundedCollection<E> extends AbstractCollectionDecorator<E>
49 implements BoundedCollection<E>, Unmodifiable {
50
51
52 private static final long serialVersionUID = -7112672385450340330L;
53
54
55
56
57
58
59
60
61
62
63 public static <E> BoundedCollection<E> unmodifiableBoundedCollection(final BoundedCollection<? extends E> coll) {
64 if (coll instanceof Unmodifiable) {
65 @SuppressWarnings("unchecked")
66 final BoundedCollection<E> tmpColl = (BoundedCollection<E>) coll;
67 return tmpColl;
68 }
69 return new UnmodifiableBoundedCollection<>(coll);
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 @SuppressWarnings("unchecked")
86 public static <E> BoundedCollection<E> unmodifiableBoundedCollection(Collection<? extends E> collection) {
87 Objects.requireNonNull(collection, "collection");
88
89
90 for (int i = 0; i < 1000; i++) {
91 if (collection instanceof BoundedCollection) {
92 break;
93 }
94 if (collection instanceof AbstractCollectionDecorator) {
95 collection = ((AbstractCollectionDecorator<E>) collection).decorated();
96 } else if (collection instanceof SynchronizedCollection) {
97 collection = ((SynchronizedCollection<E>) collection).decorated();
98 }
99 }
100
101 if (!(collection instanceof BoundedCollection)) {
102 throw new IllegalArgumentException("Collection is not a bounded collection.");
103 }
104 return new UnmodifiableBoundedCollection<>((BoundedCollection<E>) collection);
105 }
106
107
108
109
110
111
112
113 @SuppressWarnings("unchecked")
114 private UnmodifiableBoundedCollection(final BoundedCollection<? extends E> coll) {
115 super((BoundedCollection<E>) coll);
116 }
117
118 @Override
119 public boolean add(final E object) {
120 throw new UnsupportedOperationException();
121 }
122
123 @Override
124 public boolean addAll(final Collection<? extends E> coll) {
125 throw new UnsupportedOperationException();
126 }
127
128 @Override
129 public void clear() {
130 throw new UnsupportedOperationException();
131 }
132
133 @Override
134 protected BoundedCollection<E> decorated() {
135 return (BoundedCollection<E>) super.decorated();
136 }
137
138 @Override
139 public boolean isFull() {
140 return decorated().isFull();
141 }
142
143 @Override
144 public Iterator<E> iterator() {
145 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
146 }
147
148 @Override
149 public int maxSize() {
150 return decorated().maxSize();
151 }
152
153 @Override
154 public boolean remove(final Object object) {
155 throw new UnsupportedOperationException();
156 }
157
158 @Override
159 public boolean removeAll(final Collection<?> coll) {
160 throw new UnsupportedOperationException();
161 }
162
163
164
165
166 @Override
167 public boolean removeIf(final Predicate<? super E> filter) {
168 throw new UnsupportedOperationException();
169 }
170
171 @Override
172 public boolean retainAll(final Collection<?> coll) {
173 throw new UnsupportedOperationException();
174 }
175 }