KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Arrays JavaDoc;
7 import java.util.Vector JavaDoc;
8
9 public interface ElementType extends Comparable JavaDoc {
10   public void traverse();
11
12   public interface Factory {
13     ElementType create();
14
15     String JavaDoc describeType();
16   }
17
18   public static class LongFactory implements Factory {
19     long next = 0;
20
21     public ElementType create() {
22       Long JavaDoc l = new Long JavaDoc(next++);
23       return new WrappedComparable(l);
24     }
25
26     public String JavaDoc describeType() {
27       return "Long";
28     }
29
30   }
31
32   public static class StringFactory implements Factory {
33     long next = 0;
34     String JavaDoc prefix = "standard_";
35
36     public void setPrefix(String JavaDoc newPrefix) {
37       prefix = newPrefix;
38     }
39
40     public String JavaDoc describeType() {
41       return "String";
42     }
43
44     public ElementType create() {
45       return new WrappedComparable(prefix + next++);
46     }
47   }
48
49   public static class GraphFactory implements Factory {
50     ElementType.Factory contentFactory = null;
51     int graphSize = 10;
52
53     public GraphFactory(int size, ElementType.Factory valueFactory) {
54       graphSize = size;
55       contentFactory = valueFactory;
56     }
57
58     public ElementType create() {
59       return new GraphType(graphSize, contentFactory);
60     }
61
62     public String JavaDoc describeType() {
63       StringBuffer JavaDoc buf = new StringBuffer JavaDoc("BinaryTree(");
64       buf.append(contentFactory.describeType());
65       buf.append("[");
66       buf.append(graphSize);
67       buf.append("])");
68       return buf.toString();
69     }
70
71   }
72
73   public class GraphType implements ElementType { // simple lazy balanced binary tree minus insert, remove, etc.
74
class Node {
75       Node right = null, left = null;
76       ElementType value;
77
78       Node(ElementType e) {
79         value = e;
80       }
81
82       // in-order contents
83
void addContents(Vector JavaDoc result) {
84         if (left != null) left.addContents(result);
85         result.add(value);
86         if (right != null) right.addContents(result);
87       }
88     }
89
90     // in order contents
91
Vector JavaDoc getContents() {
92       Vector JavaDoc ret = new Vector JavaDoc();
93       if (top != null) top.addContents(ret);
94       return ret;
95     }
96
97     static int atMostCompare = 100;
98
99     public int compareTo(Object JavaDoc other) {
100       if (!(other instanceof GraphType)) return 0;
101
102       Vector JavaDoc contents1 = getContents();
103       Vector JavaDoc contents2 = ((GraphType) other).getContents();
104       int cnt1 = contents1.size(), cnt2 = contents2.size();
105       int cnt = Math.min(cnt1, cnt2);
106       if (cnt == 0) return (cnt1 > 0) ? 1 : ((cnt2 > 0) ? -1 : 0);
107       else {
108         int cmp = 0;
109         for (int i = 0; (cmp == 0) && (i < cnt); i++) {
110
111           cmp = ((Comparable JavaDoc) contents1.get(i)).compareTo(contents2.get(i));
112
113         }
114         return cmp;
115       }
116     }
117
118     // utility method
119
ElementType[] subArray(int start, int end, ElementType[] original) {
120       int size = end - start + 1;
121       ElementType[] ret = new ElementType[size];
122       for (int i = 0; i < size; i++)
123         ret[i] = original[i + start];
124       return ret;
125     }
126
127     // simple construction from array of values
128
Node nodeFromCollection(ElementType[] collection) {
129       int size = collection.length;
130       if (size == 0) return null;
131       int split = size / 2;
132       Node ret = new Node(collection[split]);
133       ret.left = nodeFromCollection(subArray(0, split - 1, collection));
134       ret.right = nodeFromCollection(subArray(split + 1, size - 1, collection));
135       return ret;
136     }
137
138     Node top = null;
139     ElementType.Factory elementFactory;
140
141     public void addNode(ElementType value) {
142       // TBD: not used yet
143
// Node newNode = new Node(value);
144
}
145
146     public GraphType(int size, ElementType.Factory factory) {
147       elementFactory = factory;
148       ElementType[] contents = new ElementType[size];
149       for (int i = 0; i < size; i++)
150         contents[i] = elementFactory.create();
151       Arrays.sort(contents);
152       top = nodeFromCollection(contents);
153     }
154
155     void traverse(Node node) {
156       if (node != null) {
157         traverse(node.left);
158         node.value.traverse();
159         traverse(node.right);
160       }
161     }
162
163     public void traverse() {
164       traverse(top);
165     }
166   }
167
168   // useful for Numeric, String, Date, etc. types
169
public class WrappedComparable implements ElementType {
170     Comparable JavaDoc wrapped;
171
172     public Comparable JavaDoc getWrapped() {
173       return wrapped;
174     }
175
176     public WrappedComparable(Comparable JavaDoc c) {
177       wrapped = c;
178     }
179
180     public void traverse() {
181       // nothing yet
182
}
183
184     public int compareTo(Object JavaDoc other) {
185       return (other instanceof WrappedComparable) ? ((WrappedComparable) other).getWrapped().compareTo(wrapped) : 0;
186     }
187   }
188
189 }
190
Popular Tags