1 22 package org.jboss.util.collection; 23 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.Set ; 29 30 36 public class LazySet implements Set 37 { 38 39 private Set delegate = Collections.EMPTY_SET; 40 41 public boolean add(Object o) 42 { 43 if (delegate == Collections.EMPTY_SET) 44 { 45 delegate = Collections.singleton(o); 46 return true; 47 } 48 else 49 { 50 if (delegate instanceof HashSet == false) 51 delegate = new HashSet (delegate); 52 return delegate.add(o); 53 } 54 } 55 56 public boolean addAll(Collection c) 57 { 58 if (delegate instanceof HashSet == false) 59 delegate = new HashSet (delegate); 60 return delegate.addAll(c); 61 } 62 63 public void clear() 64 { 65 delegate = Collections.EMPTY_SET; 66 } 67 68 public boolean contains(Object o) 69 { 70 return delegate.contains(o); 71 } 72 73 public boolean containsAll(Collection c) 74 { 75 return delegate.containsAll(c); 76 } 77 78 public boolean isEmpty() 79 { 80 return delegate.isEmpty(); 81 } 82 83 public Iterator iterator() 84 { 85 return delegate.iterator(); 86 } 87 88 public boolean remove(Object o) 89 { 90 if (delegate instanceof HashSet == false) 91 delegate = new HashSet (delegate); 92 return delegate.remove(o); 93 } 94 95 public boolean removeAll(Collection c) 96 { 97 if (delegate instanceof HashSet == false) 98 delegate = new HashSet (delegate); 99 return delegate.removeAll(c); 100 } 101 102 public boolean retainAll(Collection c) 103 { 104 if (delegate instanceof HashSet == false) 105 delegate = new HashSet (delegate); 106 return delegate.retainAll(c); 107 } 108 109 public int size() 110 { 111 return delegate.size(); 112 } 113 114 public Object [] toArray() 115 { 116 return delegate.toArray(); 117 } 118 119 public Object [] toArray(Object [] a) 120 { 121 return delegate.toArray(a); 122 } 123 } 124 | Popular Tags |