1 4 package com.tctest.perf.collections; 5 6 import com.tctest.perf.collections.ElementType.Factory; 7 8 import java.util.ArrayList ; 9 import java.util.Collection ; 10 import java.util.Collections ; 11 import java.util.HashMap ; 12 import java.util.HashSet ; 13 import java.util.Hashtable ; 14 import java.util.IdentityHashMap ; 15 import java.util.Iterator ; 16 import java.util.LinkedList ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Random ; 20 import java.util.Set ; 21 import java.util.TreeMap ; 22 import java.util.TreeSet ; 23 import java.util.Vector ; 24 25 public interface CollectionType { 26 void add(int count, ElementType.Factory typeFactory); 27 28 void remove(int count); 29 30 void sort(); 31 32 void iterate(); 33 34 Vector getRandom(int count); 35 36 Iterator getValues(); 37 38 String describeType(); 39 40 int size(); 41 42 void clear(); 43 44 boolean isSorted(); 45 46 void setSorted(boolean yesno); 47 48 abstract public static class CollectionImpl implements CollectionType { 49 50 protected Object collect; 51 private boolean sorted = false; 52 53 public CollectionImpl() { 54 } 56 57 public boolean isSorted() { 58 return sorted; 59 } 60 61 public void setSorted(boolean yesno) { 62 sorted = yesno; 63 } 64 65 public CollectionImpl(Collection c) { 66 collect = c; 67 } 68 69 public Collection asCollection() { 70 return (Collection ) collect; 71 } 72 73 public void clear() { 74 asCollection().clear(); 75 } 76 77 public int size() { 78 int sz = 0; 79 synchronized (collect) { 80 sz = asCollection().size(); 81 } 82 return sz; 83 } 84 85 public void add(int count, Factory typeFactory) { 86 Collection c = asCollection(); 87 setSorted(false); 88 for (int i = 0; i < count; i++) 89 c.add(typeFactory.create()); 90 } 91 92 public void remove(int count) { 93 setSorted(false); 94 Vector toRemove = getRandom(count); 95 asCollection().removeAll(toRemove); 96 } 97 98 public void sort() { 99 setSorted(true); 100 } 102 103 public Vector getRandom(int count) { 104 Iterator it = getValues(); 105 Vector ret = new Vector (count); 106 for (int i = 0; it.hasNext() && (i < count); i++) 107 ret.add(it.next()); 108 return ret; 109 } 110 111 public Iterator getValues() { 112 return asCollection().iterator(); 113 } 114 115 public void iterate() { 116 for (Iterator it = getValues(); it.hasNext();) { 117 ((ElementType) it.next()).traverse(); 118 } 119 } 120 } 121 122 abstract public static class ListCollection extends CollectionImpl { 123 public ListCollection() { 124 super(); 125 } 126 127 public void sort() { 128 setSorted(true); 129 Collections.sort((List ) collect); 130 } 131 132 public void clear() { 133 asList().clear(); 134 } 135 136 protected List asList() { 137 return (List ) collect; 138 } 139 140 public Vector getRandom(int count) { 141 int[] indices = new int[count]; 142 Random rand = new Random (); 143 for (int i = 0; i < count; i++) 144 indices[i] = rand.nextInt(count); 145 Vector ret = new Vector (count); 146 List l = asList(); 147 for (int i = 0; i < count; i++) 148 ret.add(l.get(indices[i])); 149 150 return ret; 151 } 152 } 153 154 public static class VectorCollection extends ListCollection { 155 public VectorCollection() { 156 super(); 157 collect = new Vector (); 158 } 159 160 public String describeType() { 161 return "Vector"; 162 } 163 } 164 165 public static class ArrayListCollection extends ListCollection { 166 public ArrayListCollection() { 167 super(); 168 collect = new ArrayList (); 169 } 170 171 public String describeType() { 172 return "ArrayList"; 173 } 174 } 175 176 public static class LinkedListCollection extends ListCollection { 177 public LinkedListCollection() { 178 super(); 179 collect = new LinkedList (); 180 } 181 182 public String describeType() { 183 return "LinkedList"; 184 } 185 } 186 187 abstract static public class SetCollection extends ListCollection { 188 public Vector getRandom(int cnt) { 189 Iterator it = getValues(); 190 Vector res = new Vector (cnt); 191 for (int i = 0; (i < cnt) && it.hasNext(); i++) 192 res.add(it.next()); 193 return res; 194 } 195 196 public Iterator getValues() { 197 return asSet().iterator(); 198 } 199 200 public void sort() { 201 } 203 204 Set asSet() { 205 return (Set ) collect; 206 } 207 208 public void clear() { 209 asSet().clear(); 210 } 211 212 public boolean isSorted() { 213 return true; } 215 } 216 217 public static class HashSetCollection extends SetCollection { 218 public HashSetCollection() { 219 super(); 220 collect = new HashSet (); 221 } 222 223 public String describeType() { 224 return "HashSet"; 225 } 226 } 227 228 public static class TreeSetCollection extends SetCollection { 229 public TreeSetCollection() { 230 super(); 231 collect = new TreeSet (); 232 } 233 234 public String describeType() { 235 return "TreeSet"; 236 } 237 } 238 239 abstract public static class MapCollection extends CollectionImpl { 240 241 protected ElementType.Factory keyFactory = new ElementType.LongFactory(); 242 243 public void add(int count, ElementType.Factory valueFactory) { 244 Map me = asMap(); 245 for (int i = 0; i < count; i++) 246 me.put(keyFactory.create(), valueFactory.create()); 247 } 248 249 public MapCollection() { 250 super(); 251 } 252 253 public boolean isSorted() { 254 return true; } 256 257 protected Map asMap() { 258 return (Map ) collect; 259 } 260 261 public void clear() { 262 asMap().clear(); 263 } 264 265 public int size() { 266 267 int sz = 0; 268 synchronized (collect) { 269 sz = asMap().values().size(); 270 } 271 return sz; 272 } 273 274 public Vector getRandom(int count) { 276 Vector ret = new Vector (count); 277 Iterator keys = asMap().keySet().iterator(); 278 for (int i = 0; keys.hasNext() && (i < count); i++) 279 ret.add(keys.next()); 280 return ret; 281 } 282 283 public void remove(int count) { 284 Iterator removeKeys = getRandom(count).iterator(); 285 while (removeKeys.hasNext()) 286 asMap().remove(removeKeys.next()); 287 } 288 289 public Iterator getValues() { 290 return asMap().values().iterator(); 291 } 292 } 293 294 public static class HashMapCollection extends MapCollection { 295 public HashMapCollection() { 296 super(); 297 collect = new HashMap (); 298 } 299 300 public String describeType() { 301 return "HashMap"; 302 } 303 } 304 305 public static class TreeMapCollection extends MapCollection { 306 public TreeMapCollection() { 307 super(); 308 collect = new TreeMap (); 309 } 310 311 public String describeType() { 312 return "TreeMap"; 313 } 314 } 315 316 public static class IdentityHashMapCollection extends MapCollection { 317 public IdentityHashMapCollection() { 318 super(); 319 collect = new IdentityHashMap (); 320 } 321 322 public String describeType() { 323 return "IdentityHashMap"; 324 } 325 } 326 327 public static class HashtableCollection extends MapCollection { 328 public HashtableCollection() { 329 super(); 330 collect = new Hashtable (); 331 } 332 333 public String describeType() { 334 return "Hashtable"; 335 } 336 } 337 338 } 339 | Popular Tags |