1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.map;
18
19 import java.lang.reflect.Array;
20 import java.util.Collection;
21 import java.util.Iterator;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.function.Predicate;
25
26 import org.apache.commons.collections4.Unmodifiable;
27 import org.apache.commons.collections4.iterators.AbstractIteratorDecorator;
28 import org.apache.commons.collections4.keyvalue.AbstractMapEntryDecorator;
29 import org.apache.commons.collections4.set.AbstractSetDecorator;
30
31
32
33
34
35
36
37
38
39
40
41 public final class UnmodifiableEntrySet<K, V>
42 extends AbstractSetDecorator<Map.Entry<K, V>> implements Unmodifiable {
43
44
45
46
47 private final class UnmodifiableEntry extends AbstractMapEntryDecorator<K, V> {
48
49 protected UnmodifiableEntry(final Map.Entry<K, V> entry) {
50 super(entry);
51 }
52
53 @Override
54 public V setValue(final V value) {
55 throw new UnsupportedOperationException();
56 }
57 }
58
59
60
61
62 private final class UnmodifiableEntrySetIterator extends AbstractIteratorDecorator<Map.Entry<K, V>> {
63
64 protected UnmodifiableEntrySetIterator(final Iterator<Map.Entry<K, V>> iterator) {
65 super(iterator);
66 }
67
68 @Override
69 public Map.Entry<K, V> next() {
70 return new UnmodifiableEntry(getIterator().next());
71 }
72
73 @Override
74 public void remove() {
75 throw new UnsupportedOperationException();
76 }
77 }
78
79
80 private static final long serialVersionUID = 1678353579659253473L;
81
82
83
84
85
86
87
88
89
90
91
92 public static <K, V> Set<Map.Entry<K, V>> unmodifiableEntrySet(final Set<Map.Entry<K, V>> set) {
93 if (set instanceof Unmodifiable) {
94 return set;
95 }
96 return new UnmodifiableEntrySet<>(set);
97 }
98
99
100
101
102
103
104
105 private UnmodifiableEntrySet(final Set<Map.Entry<K, V>> set) {
106 super(set);
107 }
108
109 @Override
110 public boolean add(final Map.Entry<K, V> object) {
111 throw new UnsupportedOperationException();
112 }
113
114 @Override
115 public boolean addAll(final Collection<? extends Map.Entry<K, V>> coll) {
116 throw new UnsupportedOperationException();
117 }
118
119 @Override
120 public void clear() {
121 throw new UnsupportedOperationException();
122 }
123
124 @Override
125 public Iterator<Map.Entry<K, V>> iterator() {
126 return new UnmodifiableEntrySetIterator(decorated().iterator());
127 }
128
129 @Override
130 public boolean remove(final Object object) {
131 throw new UnsupportedOperationException();
132 }
133
134 @Override
135 public boolean removeAll(final Collection<?> coll) {
136 throw new UnsupportedOperationException();
137 }
138
139
140
141
142 @Override
143 public boolean removeIf(final Predicate<? super Map.Entry<K, V>> filter) {
144 throw new UnsupportedOperationException();
145 }
146
147 @Override
148 public boolean retainAll(final Collection<?> coll) {
149 throw new UnsupportedOperationException();
150 }
151
152 @Override
153 @SuppressWarnings("unchecked")
154 public Object[] toArray() {
155 final Object[] array = decorated().toArray();
156 for (int i = 0; i < array.length; i++) {
157 array[i] = new UnmodifiableEntry((Map.Entry<K, V>) array[i]);
158 }
159 return array;
160 }
161
162 @Override
163 @SuppressWarnings("unchecked")
164 public <T> T[] toArray(final T[] array) {
165 Object[] result = array;
166 if (array.length > 0) {
167
168
169 result = (Object[]) Array.newInstance(array.getClass().getComponentType(), 0);
170 }
171 result = decorated().toArray(result);
172 for (int i = 0; i < result.length; i++) {
173 result[i] = new UnmodifiableEntry((Map.Entry<K, V>) result[i]);
174 }
175
176
177 if (result.length > array.length) {
178 return (T[]) result;
179 }
180
181
182 System.arraycopy(result, 0, array, 0, result.length);
183 if (array.length > result.length) {
184 array[result.length] = null;
185 }
186 return array;
187 }
188
189 }