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