summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.test/src/main/java/net/wotonomy/test/TestMap.java
blob: a7f4e06df3a46ac1a116a9f718094110dc28dbb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package net.wotonomy.test;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;

import net.wotonomy.datastore.DataSoup;
import net.wotonomy.datastore.SerializedFileSoup;
import net.wotonomy.datastore.XMLFileSoup;
import net.wotonomy.foundation.internal.ValueConverter;

public class TestMap extends HashMap {
	public TestMap() {
		put("date", new Date());
		put("firstName", randomParse("Bert|Ernie|Elmo|Zoe|Arthur|Emily|DJ|Grover|Oscar|Max|Big|Twinkle"));
		put("middleName",
				new StringBuffer(randomParse("Rufus|Remy|Martin|Josephus|Ulysses|Homer|Bart|Tip|Onegin|Meredith|Jay")));
		put("lastName", randomParse("Alejandro|Alexander|Bird|Gosling|Joy|Van Hoff|Pedia|Marr|McNealy|Ping"));
		put("address",
				randomParse("1|2|3|4") + randomParse("0|1|00|10|5|50") + randomParse("0|00|1|01|5|05|9|09||000") + " "
						+ randomParse("Merry|Berry|Perry|Jerry|Meadow|Falls|Elm|Raspberry|Strawberry") + " "
						+ randomParse("Road|Lane|Court|Drive|Parkway|Terrace"));
		put("city", randomParse("Springfield|Sterling|Cascades|Vienna|Reston|Paris|London|Runnymeade"));
		put("state", randomParse("TX|NJ|NY|VA|DC|MD|NC|SC|WV|AR|FL|CA|TN"));
		put("zip", ValueConverter.getInteger(randomParse("1|2|3|4") + "0" + randomParse("0|1|2|3|5")
				+ randomParse("6|7|8|9") + randomParse("6|7|8|9")));
		put("age", new Short((short) (new Random().nextDouble() * 40 + 18)));
		childCount = -1;
	}

	protected int childCount;

	public int getChildCount() {
		if (childCount == -1) {
			// childCount = (int) ( random.nextDouble() * 6 ) - 3; // + 100; // tree
			// scalability test
			if (childCount < 0)
				childCount = 0;
		}
		return childCount;
	};

	protected TestMap[] children;

	public TestMap[] getChildren() {
		if (get("children") == null) {
			int n = getChildCount();
			TestMap[] children = new TestMap[n];
			for (int i = 0; i < n; i++) {
				children[i] = new TestMap();
			}
			put("children", children);
		}
		return (TestMap[]) get("children");
	}

	public void setChildren(TestMap[] aChildArray) {
		put("children", aChildArray);
	}

	public List getChildList() {
		List result = new LinkedList();
		TestMap[] childArray = getChildren();
		for (int i = 0; i < childArray.length; i++) {
			result.add(childArray[i]);
		}
		return result;
	}

	public void setChildList(List aChildList) {
		TestMap[] children = new TestMap[aChildList.size()];
		for (int i = 0; i < children.length; i++) {
			children[i] = (TestMap) aChildList.get(i);
		}
		setChildren(children);
	}

	public String getFullName() {
		return get("firstName") + " " + get("middleName") + " " + get("lastName");
	}

	public boolean equals(Object anObject) {
		return anObject == this;
	}

	public String toString() {
		return "[" + getClass().getName() + ":" + getFullName() + "]";
	}

	// statics

	private static Random random = new Random();

	private static String randomParse(String aString) {
		String result = "";
		StringTokenizer tokens = new StringTokenizer(aString, "|");
		int n = (int) (random.nextDouble() * tokens.countTokens());
		for (int i = 0; i <= n; i++) {
			result = tokens.nextToken();
		}
		return result;
	}

	public static void main(String[] argv) {
		int count = 100;
		boolean xmlMode = false;
		if (argv.length > 0) {
			Integer parsed = ValueConverter.getInteger(argv[0]);
			if (parsed != null)
				count = parsed.intValue();

			if (argv.length > 1) {
				if (argv[1].indexOf("xml") > -1) {
					xmlMode = true;
				}
			}
		}

		long millis = System.currentTimeMillis();

		DataSoup store = null;
		if (xmlMode) {
			store = new XMLFileSoup("testMaps-xml");
		} else {
			store = new SerializedFileSoup("testMaps-java");
		}

		Object o;
		for (int i = 0; i < count; i++) {
			store.addObject(new TestMap());
		}
		/*
		 * store.addIndex( "age", "age" ); store.addIndex( "zipCode", "zipCode" );
		 * store.addIndex( "firstName", "firstName" ); store.addIndex( "lastName",
		 * "lastName" );
		 */
		System.out.println(System.currentTimeMillis() - millis + " milliseconds");
	}
}