22
33public class Collections
44{
5- public static List EMPTY_LIST = new UnmodifiableList (new ArrayList ());
6-
7- public static Set EMPTY_SET = new UnmodifiableSet (new HashSet ());
5+ public static final List EMPTY_LIST = unmodifiableList (new ArrayList ());
6+ public static final Set EMPTY_SET = unmodifiableSet (new HashSet ());
87
98 private Collections ()
109 {
1110 }
1211
12+ public static Set singleton (Object o )
13+ {
14+ Set rv = new HashSet ();
15+
16+ rv .add (o );
17+
18+ return rv ;
19+ }
20+
1321 public static Collection unmodifiableCollection (Collection c )
1422 {
1523 return new UnmodifiableCollection (c );
@@ -119,15 +127,6 @@ public static Set unmodifiableSet(Set s)
119127 return new UnmodifiableSet (s );
120128 }
121129
122- public static Set singleton (Object o )
123- {
124- Set rv = new HashSet ();
125-
126- rv .add (o );
127-
128- return rv ;
129- }
130-
131130 static class UnmodifiableSet
132131 extends UnmodifiableCollection
133132 implements Set
@@ -148,88 +147,6 @@ public int hashCode()
148147 }
149148 }
150149
151- public static Map unmodifiableMap (Map map )
152- {
153- return new UnmodifiableMap (map );
154- }
155-
156- static class UnmodifiableMap
157- implements Map
158- {
159- private Map map ;
160-
161- UnmodifiableMap (Map map )
162- {
163- this .map = map ;
164- }
165-
166- public int size ()
167- {
168- return map .size ();
169- }
170-
171- public boolean isEmpty ()
172- {
173- return map .isEmpty ();
174- }
175-
176- public boolean containsKey (Object key )
177- throws ClassCastException , NullPointerException
178- {
179- return map .containsKey (key );
180- }
181-
182- public boolean containsValue (Object value )
183- {
184- return map .containsValue (value );
185- }
186-
187- public Object get (Object key )
188- throws ClassCastException , NullPointerException
189- {
190- return map .get (key );
191- }
192-
193- public Object put (Object key , Object value )
194- throws RuntimeException , ClassCastException , IllegalArgumentException , NullPointerException
195- {
196- throw new RuntimeException ("unsupported operation - map unmodifiable" );
197- }
198-
199- public Object remove (Object key )
200- throws RuntimeException
201- {
202- throw new RuntimeException ("unsupported operation - map unmodifiable" );
203- }
204-
205- public void putAll (Map t )
206- throws RuntimeException , ClassCastException , IllegalArgumentException , NullPointerException
207- {
208- throw new RuntimeException ("unsupported operation - map unmodifiable" );
209- }
210-
211- public void clear ()
212- throws RuntimeException
213- {
214- throw new RuntimeException ("unsupported operation - map unmodifiable" );
215- }
216-
217- public Set keySet ()
218- {
219- return map .keySet ();
220- }
221-
222- public Collection values ()
223- {
224- return map .values ();
225- }
226-
227- public Set entrySet ()
228- {
229- return map .entrySet ();
230- }
231- }
232-
233150 public static List unmodifiableList (List list )
234151 {
235152 return new UnmodifiableList (list );
@@ -373,4 +290,214 @@ public Object nextElement()
373290 }
374291 };
375292 }
293+
294+ public static Map unmodifiableMap (Map s )
295+ {
296+ return new UnmodifiableMap (s );
297+ }
298+
299+ static class UnmodifiableMap
300+ implements Map
301+ {
302+ private Map c ;
303+
304+ UnmodifiableMap (Map map )
305+ {
306+ this .c = map ;
307+ }
308+
309+ public int size ()
310+ {
311+ return c .size ();
312+ }
313+
314+ public boolean isEmpty ()
315+ {
316+ return c .isEmpty ();
317+ }
318+
319+ public boolean containsKey (Object o )
320+ {
321+ return c .containsKey (o );
322+ }
323+
324+ public boolean containsValue (Object o )
325+ {
326+ return c .containsValue (o );
327+ }
328+
329+ public Object get (Object o )
330+ {
331+ return c .get (o );
332+ }
333+
334+ public Object put (Object o , Object o2 )
335+ {
336+ throw new RuntimeException ();
337+ }
338+
339+ public Object remove (Object o )
340+ {
341+ throw new RuntimeException ();
342+ }
343+
344+ public void putAll (Map map )
345+ {
346+ throw new RuntimeException ();
347+ }
348+
349+ public void clear ()
350+ {
351+ throw new RuntimeException ();
352+ }
353+
354+ public Set keySet ()
355+ {
356+ return Collections .unmodifiableSet (c .keySet ());
357+ }
358+
359+ public Collection values ()
360+ {
361+ return new UnmodifiableCollection (c .values ());
362+ }
363+
364+ public Set entrySet ()
365+ {
366+ return Collections .unmodifiableSet (c .entrySet ());
367+ }
368+
369+ public boolean equals (Object o )
370+ {
371+ return c .equals (o );
372+ }
373+
374+ public int hashCode ()
375+ {
376+ return c .hashCode ();
377+ }
378+
379+ public String toString ()
380+ {
381+ return c .toString ();
382+ }
383+ }
384+
385+ public static Set synchronizedSet (Set set )
386+ {
387+ return new SyncSet (set );
388+ }
389+
390+ static class SyncSet implements Set
391+ {
392+ private Set base ;
393+
394+ SyncSet (Set base )
395+ {
396+ this .base = base ;
397+ }
398+
399+ public int size ()
400+ {
401+ synchronized (base )
402+ {
403+ return base .size ();
404+ }
405+ }
406+
407+ public boolean isEmpty ()
408+ {
409+ synchronized (base )
410+ {
411+ return base .isEmpty ();
412+ }
413+ }
414+
415+ public boolean contains (Object o )
416+ {
417+ synchronized (base )
418+ {
419+ return base .contains (o );
420+ }
421+ }
422+
423+ public Iterator iterator ()
424+ {
425+ synchronized (base )
426+ {
427+ return new ArrayList (base ).iterator ();
428+ }
429+ }
430+
431+ public Object [] toArray ()
432+ {
433+ synchronized (base )
434+ {
435+ return base .toArray ();
436+ }
437+ }
438+
439+ public boolean add (Object o )
440+ {
441+ synchronized (base )
442+ {
443+ return base .add (o );
444+ }
445+ }
446+
447+ public boolean remove (Object o )
448+ {
449+ synchronized (base )
450+ {
451+ return base .remove (o );
452+ }
453+ }
454+
455+ public boolean addAll (Collection collection )
456+ {
457+ synchronized (base )
458+ {
459+ return base .addAll (collection );
460+ }
461+ }
462+
463+ public void clear ()
464+ {
465+ synchronized (base )
466+ {
467+ base .clear ();
468+ }
469+ }
470+
471+ public boolean removeAll (Collection collection )
472+ {
473+ synchronized (base )
474+ {
475+ return base .removeAll (collection );
476+ }
477+ }
478+
479+ public boolean retainAll (Collection collection )
480+ {
481+ synchronized (base )
482+ {
483+ return base .retainAll (collection );
484+ }
485+ }
486+
487+ public boolean containsAll (Collection collection )
488+ {
489+ synchronized (base )
490+ {
491+ return base .containsAll (collection );
492+ }
493+ }
494+
495+ public Object [] toArray (Object [] objects )
496+ {
497+ synchronized (base )
498+ {
499+ return base .toArray (objects );
500+ }
501+ }
502+ }
376503}
0 commit comments