1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package org.apache.commons.classscan.util; |
15 | |
|
16 | |
import java.lang.reflect.Array; |
17 | |
import java.util.Arrays; |
18 | |
import java.util.Collection; |
19 | |
import java.util.Iterator; |
20 | |
import java.util.NoSuchElementException; |
21 | |
import java.util.Set; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public class ReadOnlySet<V> implements Set<V> { |
30 | |
|
31 | |
protected V[] values; |
32 | |
|
33 | 0 | public ReadOnlySet(V[] values) { |
34 | 0 | if(values==null) { |
35 | 0 | throw new UnsupportedOperationException(); |
36 | |
} |
37 | |
else { |
38 | 0 | this.values = values; |
39 | |
} |
40 | 0 | } |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | protected ReadOnlySet() { |
47 | 0 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
protected void setValues(V[] values) { |
54 | 0 | this.values = values; |
55 | 0 | } |
56 | |
|
57 | |
@Override |
58 | |
public boolean contains(Object o) { |
59 | 0 | for (V t : values) { |
60 | 0 | if (t.equals(o)) { |
61 | 0 | return true; |
62 | |
} |
63 | |
} |
64 | 0 | return false; |
65 | |
} |
66 | |
|
67 | |
@Override |
68 | |
public boolean containsAll(Collection<?> collection) { |
69 | 0 | for (Object single : collection) { |
70 | 0 | if (!contains(single)) { |
71 | 0 | return false; |
72 | |
} |
73 | |
} |
74 | 0 | return true; |
75 | |
} |
76 | |
|
77 | |
@Override |
78 | |
public boolean isEmpty() { |
79 | 0 | return values.length == 0; |
80 | |
} |
81 | |
|
82 | |
@Override |
83 | |
public Iterator<V> iterator() { |
84 | 0 | return new Iterator<V>() { |
85 | |
int i; |
86 | |
|
87 | |
@Override |
88 | |
public boolean hasNext() { |
89 | 0 | return i < values.length; |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
public V next() { |
94 | 0 | if (i == values.length) { |
95 | 0 | throw new NoSuchElementException("Reading past end of iterator"); |
96 | |
} |
97 | 0 | return values[i++]; |
98 | |
} |
99 | |
|
100 | |
@Override |
101 | |
public void remove() { |
102 | 0 | throw new UnsupportedOperationException("Read only iterator"); |
103 | |
} |
104 | |
}; |
105 | |
} |
106 | |
|
107 | |
@Override |
108 | |
public int size() { |
109 | 0 | return values.length; |
110 | |
} |
111 | |
|
112 | |
@Override |
113 | |
public Object[] toArray() { |
114 | 0 | return values.clone(); |
115 | |
} |
116 | |
|
117 | |
@Override |
118 | |
public <T> T[] toArray(T[] a) { |
119 | 0 | int numberToNull = a.length - values.length; |
120 | 0 | if (numberToNull >= 0) { |
121 | 0 | System.arraycopy(values, 0, a, 0, values.length); |
122 | 0 | if (numberToNull > 0) { |
123 | 0 | Arrays.fill(a, values.length, a.length, null); |
124 | |
} |
125 | 0 | return a; |
126 | |
} |
127 | |
|
128 | |
@SuppressWarnings("unchecked") |
129 | 0 | T[] rv = (T[]) Array.newInstance(a.getClass().getComponentType(), values.length); |
130 | 0 | System.arraycopy(values, 0, rv, 0, values.length); |
131 | 0 | return rv; |
132 | |
} |
133 | |
|
134 | |
@Override |
135 | |
final public boolean add(V e) { |
136 | 0 | throw new UnsupportedOperationException("Set is readonly"); |
137 | |
} |
138 | |
|
139 | |
@Override |
140 | |
final public boolean addAll(Collection<? extends V> c) { |
141 | 0 | throw new UnsupportedOperationException("Set is readonly"); |
142 | |
} |
143 | |
|
144 | |
@Override |
145 | |
final public void clear() { |
146 | 0 | throw new UnsupportedOperationException("Set is readonly"); |
147 | |
} |
148 | |
|
149 | |
@Override |
150 | |
final public boolean remove(Object e) { |
151 | 0 | throw new UnsupportedOperationException("Set is readonly"); |
152 | |
} |
153 | |
|
154 | |
@Override |
155 | |
final public boolean removeAll(Collection<?> c) { |
156 | 0 | throw new UnsupportedOperationException("Set is readonly"); |
157 | |
} |
158 | |
|
159 | |
@Override |
160 | |
final public boolean retainAll(Collection<?> c) { |
161 | 0 | throw new UnsupportedOperationException("Set is readonly"); |
162 | |
} |
163 | |
|
164 | |
} |