001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest.perf.collections;
005:
006: import com.tctest.perf.collections.ElementType.Factory;
007:
008: import java.util.ArrayList;
009: import java.util.Collection;
010: import java.util.Collections;
011: import java.util.HashMap;
012: import java.util.HashSet;
013: import java.util.Hashtable;
014: import java.util.IdentityHashMap;
015: import java.util.Iterator;
016: import java.util.LinkedList;
017: import java.util.List;
018: import java.util.Map;
019: import java.util.Random;
020: import java.util.Set;
021: import java.util.TreeMap;
022: import java.util.TreeSet;
023: import java.util.Vector;
024:
025: public interface CollectionType {
026: void add(int count, ElementType.Factory typeFactory);
027:
028: void remove(int count);
029:
030: void sort();
031:
032: void iterate();
033:
034: Vector getRandom(int count);
035:
036: Iterator getValues();
037:
038: String describeType();
039:
040: int size();
041:
042: void clear();
043:
044: boolean isSorted();
045:
046: void setSorted(boolean yesno);
047:
048: abstract public static class CollectionImpl implements
049: CollectionType {
050:
051: protected Object collect;
052: private boolean sorted = false;
053:
054: public CollectionImpl() {
055: // whatever, Eclipse
056: }
057:
058: public boolean isSorted() {
059: return sorted;
060: }
061:
062: public void setSorted(boolean yesno) {
063: sorted = yesno;
064: }
065:
066: public CollectionImpl(Collection c) {
067: collect = c;
068: }
069:
070: public Collection asCollection() {
071: return (Collection) collect;
072: }
073:
074: public void clear() {
075: asCollection().clear();
076: }
077:
078: public int size() {
079: int sz = 0;
080: synchronized (collect) {
081: sz = asCollection().size();
082: }
083: return sz;
084: }
085:
086: public void add(int count, Factory typeFactory) {
087: Collection c = asCollection();
088: setSorted(false);
089: for (int i = 0; i < count; i++)
090: c.add(typeFactory.create());
091: }
092:
093: public void remove(int count) {
094: setSorted(false);
095: Vector toRemove = getRandom(count);
096: asCollection().removeAll(toRemove);
097: }
098:
099: public void sort() {
100: setSorted(true);
101: // default implementation does nothing
102: }
103:
104: public Vector getRandom(int count) {
105: Iterator it = getValues();
106: Vector ret = new Vector(count);
107: for (int i = 0; it.hasNext() && (i < count); i++)
108: ret.add(it.next());
109: return ret;
110: }
111:
112: public Iterator getValues() {
113: return asCollection().iterator();
114: }
115:
116: public void iterate() {
117: for (Iterator it = getValues(); it.hasNext();) {
118: ((ElementType) it.next()).traverse();
119: }
120: }
121: }
122:
123: abstract public static class ListCollection extends CollectionImpl {
124: public ListCollection() {
125: super ();
126: }
127:
128: public void sort() {
129: setSorted(true);
130: Collections.sort((List) collect);
131: }
132:
133: public void clear() {
134: asList().clear();
135: }
136:
137: protected List asList() {
138: return (List) collect;
139: }
140:
141: public Vector getRandom(int count) {
142: int[] indices = new int[count];
143: Random rand = new Random();
144: for (int i = 0; i < count; i++)
145: indices[i] = rand.nextInt(count);
146: Vector ret = new Vector(count);
147: List l = asList();
148: for (int i = 0; i < count; i++)
149: ret.add(l.get(indices[i]));
150:
151: return ret;
152: }
153: }
154:
155: public static class VectorCollection extends ListCollection {
156: public VectorCollection() {
157: super ();
158: collect = new Vector();
159: }
160:
161: public String describeType() {
162: return "Vector";
163: }
164: }
165:
166: public static class ArrayListCollection extends ListCollection {
167: public ArrayListCollection() {
168: super ();
169: collect = new ArrayList();
170: }
171:
172: public String describeType() {
173: return "ArrayList";
174: }
175: }
176:
177: public static class LinkedListCollection extends ListCollection {
178: public LinkedListCollection() {
179: super ();
180: collect = new LinkedList();
181: }
182:
183: public String describeType() {
184: return "LinkedList";
185: }
186: }
187:
188: abstract static public class SetCollection extends ListCollection {
189: public Vector getRandom(int cnt) {
190: Iterator it = getValues();
191: Vector res = new Vector(cnt);
192: for (int i = 0; (i < cnt) && it.hasNext(); i++)
193: res.add(it.next());
194: return res;
195: }
196:
197: public Iterator getValues() {
198: return asSet().iterator();
199: }
200:
201: public void sort() {
202: // default implementation does nothing
203: }
204:
205: Set asSet() {
206: return (Set) collect;
207: }
208:
209: public void clear() {
210: asSet().clear();
211: }
212:
213: public boolean isSorted() {
214: return true; // lie to prevent sorting
215: }
216: }
217:
218: public static class HashSetCollection extends SetCollection {
219: public HashSetCollection() {
220: super ();
221: collect = new HashSet();
222: }
223:
224: public String describeType() {
225: return "HashSet";
226: }
227: }
228:
229: public static class TreeSetCollection extends SetCollection {
230: public TreeSetCollection() {
231: super ();
232: collect = new TreeSet();
233: }
234:
235: public String describeType() {
236: return "TreeSet";
237: }
238: }
239:
240: abstract public static class MapCollection extends CollectionImpl {
241:
242: protected ElementType.Factory keyFactory = new ElementType.LongFactory();
243:
244: public void add(int count, ElementType.Factory valueFactory) {
245: Map me = asMap();
246: for (int i = 0; i < count; i++)
247: me.put(keyFactory.create(), valueFactory.create());
248: }
249:
250: public MapCollection() {
251: super ();
252: }
253:
254: public boolean isSorted() {
255: return true; // lie to prevent sorting
256: }
257:
258: protected Map asMap() {
259: return (Map) collect;
260: }
261:
262: public void clear() {
263: asMap().clear();
264: }
265:
266: public int size() {
267:
268: int sz = 0;
269: synchronized (collect) {
270: sz = asMap().values().size();
271: }
272: return sz;
273: }
274:
275: // we want keys here as the usage is to remove some keyed things
276: public Vector getRandom(int count) {
277: Vector ret = new Vector(count);
278: Iterator keys = asMap().keySet().iterator();
279: for (int i = 0; keys.hasNext() && (i < count); i++)
280: ret.add(keys.next());
281: return ret;
282: }
283:
284: public void remove(int count) {
285: Iterator removeKeys = getRandom(count).iterator();
286: while (removeKeys.hasNext())
287: asMap().remove(removeKeys.next());
288: }
289:
290: public Iterator getValues() {
291: return asMap().values().iterator();
292: }
293: }
294:
295: public static class HashMapCollection extends MapCollection {
296: public HashMapCollection() {
297: super ();
298: collect = new HashMap();
299: }
300:
301: public String describeType() {
302: return "HashMap";
303: }
304: }
305:
306: public static class TreeMapCollection extends MapCollection {
307: public TreeMapCollection() {
308: super ();
309: collect = new TreeMap();
310: }
311:
312: public String describeType() {
313: return "TreeMap";
314: }
315: }
316:
317: public static class IdentityHashMapCollection extends MapCollection {
318: public IdentityHashMapCollection() {
319: super ();
320: collect = new IdentityHashMap();
321: }
322:
323: public String describeType() {
324: return "IdentityHashMap";
325: }
326: }
327:
328: public static class HashtableCollection extends MapCollection {
329: public HashtableCollection() {
330: super ();
331: collect = new Hashtable();
332: }
333:
334: public String describeType() {
335: return "Hashtable";
336: }
337: }
338:
339: }
|