KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > perf > collections > CollectionType


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.perf.collections;
5
6 import com.tctest.perf.collections.ElementType.Factory;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Hashtable JavaDoc;
14 import java.util.IdentityHashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.LinkedList JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Random JavaDoc;
20 import java.util.Set JavaDoc;
21 import java.util.TreeMap JavaDoc;
22 import java.util.TreeSet JavaDoc;
23 import java.util.Vector JavaDoc;
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 JavaDoc getRandom(int count);
35
36   Iterator JavaDoc getValues();
37
38   String JavaDoc 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 JavaDoc collect;
51     private boolean sorted = false;
52
53     public CollectionImpl() {
54       // whatever, Eclipse
55
}
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 JavaDoc c) {
66       collect = c;
67     }
68
69     public Collection JavaDoc asCollection() {
70       return (Collection JavaDoc) 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 JavaDoc 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 JavaDoc toRemove = getRandom(count);
95       asCollection().removeAll(toRemove);
96     }
97
98     public void sort() {
99       setSorted(true);
100       // default implementation does nothing
101
}
102
103     public Vector JavaDoc getRandom(int count) {
104       Iterator JavaDoc it = getValues();
105       Vector JavaDoc ret = new Vector JavaDoc(count);
106       for (int i = 0; it.hasNext() && (i < count); i++)
107         ret.add(it.next());
108       return ret;
109     }
110
111     public Iterator JavaDoc getValues() {
112       return asCollection().iterator();
113     }
114
115     public void iterate() {
116       for (Iterator JavaDoc 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 JavaDoc) collect);
130     }
131
132     public void clear() {
133       asList().clear();
134     }
135
136     protected List JavaDoc asList() {
137       return (List JavaDoc) collect;
138     }
139
140     public Vector JavaDoc getRandom(int count) {
141       int[] indices = new int[count];
142       Random JavaDoc rand = new Random JavaDoc();
143       for (int i = 0; i < count; i++)
144         indices[i] = rand.nextInt(count);
145       Vector JavaDoc ret = new Vector JavaDoc(count);
146       List JavaDoc 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 JavaDoc();
158     }
159
160     public String JavaDoc describeType() {
161       return "Vector";
162     }
163   }
164
165   public static class ArrayListCollection extends ListCollection {
166     public ArrayListCollection() {
167       super();
168       collect = new ArrayList JavaDoc();
169     }
170
171     public String JavaDoc describeType() {
172       return "ArrayList";
173     }
174   }
175
176   public static class LinkedListCollection extends ListCollection {
177     public LinkedListCollection() {
178       super();
179       collect = new LinkedList JavaDoc();
180     }
181
182     public String JavaDoc describeType() {
183       return "LinkedList";
184     }
185   }
186
187   abstract static public class SetCollection extends ListCollection {
188     public Vector JavaDoc getRandom(int cnt) {
189       Iterator JavaDoc it = getValues();
190       Vector JavaDoc res = new Vector JavaDoc(cnt);
191       for (int i = 0; (i < cnt) && it.hasNext(); i++)
192         res.add(it.next());
193       return res;
194     }
195
196     public Iterator JavaDoc getValues() {
197       return asSet().iterator();
198     }
199
200     public void sort() {
201       // default implementation does nothing
202
}
203
204     Set JavaDoc asSet() {
205       return (Set JavaDoc) collect;
206     }
207
208     public void clear() {
209       asSet().clear();
210     }
211
212     public boolean isSorted() {
213       return true; // lie to prevent sorting
214
}
215   }
216
217   public static class HashSetCollection extends SetCollection {
218     public HashSetCollection() {
219       super();
220       collect = new HashSet JavaDoc();
221     }
222
223     public String JavaDoc describeType() {
224       return "HashSet";
225     }
226   }
227
228   public static class TreeSetCollection extends SetCollection {
229     public TreeSetCollection() {
230       super();
231       collect = new TreeSet JavaDoc();
232     }
233
234     public String JavaDoc 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 JavaDoc 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; // lie to prevent sorting
255
}
256
257     protected Map JavaDoc asMap() {
258       return (Map JavaDoc) 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     // we want keys here as the usage is to remove some keyed things
275
public Vector JavaDoc getRandom(int count) {
276       Vector JavaDoc ret = new Vector JavaDoc(count);
277       Iterator JavaDoc 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 JavaDoc removeKeys = getRandom(count).iterator();
285       while (removeKeys.hasNext())
286         asMap().remove(removeKeys.next());
287     }
288
289     public Iterator JavaDoc 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 JavaDoc();
298     }
299
300     public String JavaDoc describeType() {
301       return "HashMap";
302     }
303   }
304
305   public static class TreeMapCollection extends MapCollection {
306     public TreeMapCollection() {
307       super();
308       collect = new TreeMap JavaDoc();
309     }
310
311     public String JavaDoc describeType() {
312       return "TreeMap";
313     }
314   }
315
316   public static class IdentityHashMapCollection extends MapCollection {
317     public IdentityHashMapCollection() {
318       super();
319       collect = new IdentityHashMap JavaDoc();
320     }
321
322     public String JavaDoc describeType() {
323       return "IdentityHashMap";
324     }
325   }
326
327   public static class HashtableCollection extends MapCollection {
328     public HashtableCollection() {
329       super();
330       collect = new Hashtable JavaDoc();
331     }
332
333     public String JavaDoc describeType() {
334       return "Hashtable";
335     }
336   }
337
338 }
339
Popular Tags