1 16 package org.apache.commons.collections.collection; 17 18 import java.io.Serializable ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 22 41 public class SynchronizedCollection implements Collection , Serializable { 42 43 44 private static final long serialVersionUID = 2412805092710877986L; 45 46 47 protected final Collection collection; 48 49 protected final Object lock; 50 51 58 public static Collection decorate(Collection coll) { 59 return new SynchronizedCollection(coll); 60 } 61 62 69 protected SynchronizedCollection(Collection collection) { 70 if (collection == null) { 71 throw new IllegalArgumentException ("Collection must not be null"); 72 } 73 this.collection = collection; 74 this.lock = this; 75 } 76 77 84 protected SynchronizedCollection(Collection collection, Object lock) { 85 if (collection == null) { 86 throw new IllegalArgumentException ("Collection must not be null"); 87 } 88 this.collection = collection; 89 this.lock = lock; 90 } 91 92 public boolean add(Object object) { 94 synchronized (lock) { 95 return collection.add(object); 96 } 97 } 98 99 public boolean addAll(Collection coll) { 100 synchronized (lock) { 101 return collection.addAll(coll); 102 } 103 } 104 105 public void clear() { 106 synchronized (lock) { 107 collection.clear(); 108 } 109 } 110 111 public boolean contains(Object object) { 112 synchronized (lock) { 113 return collection.contains(object); 114 } 115 } 116 117 public boolean containsAll(Collection coll) { 118 synchronized (lock) { 119 return collection.containsAll(coll); 120 } 121 } 122 123 public boolean isEmpty() { 124 synchronized (lock) { 125 return collection.isEmpty(); 126 } 127 } 128 129 139 public Iterator iterator() { 140 return collection.iterator(); 141 } 142 143 public Object [] toArray() { 144 synchronized (lock) { 145 return collection.toArray(); 146 } 147 } 148 149 public Object [] toArray(Object [] object) { 150 synchronized (lock) { 151 return collection.toArray(object); 152 } 153 } 154 155 public boolean remove(Object object) { 156 synchronized (lock) { 157 return collection.remove(object); 158 } 159 } 160 161 public boolean removeAll(Collection coll) { 162 synchronized (lock) { 163 return collection.removeAll(coll); 164 } 165 } 166 167 public boolean retainAll(Collection coll) { 168 synchronized (lock) { 169 return collection.retainAll(coll); 170 } 171 } 172 173 public int size() { 174 synchronized (lock) { 175 return collection.size(); 176 } 177 } 178 179 public boolean equals(Object object) { 180 synchronized (lock) { 181 if (object == this) { 182 return true; 183 } 184 return collection.equals(object); 185 } 186 } 187 188 public int hashCode() { 189 synchronized (lock) { 190 return collection.hashCode(); 191 } 192 } 193 194 public String toString() { 195 synchronized (lock) { 196 return collection.toString(); 197 } 198 } 199 200 } 201 | Popular Tags |