View Javadoc

1   package net.wotonomy.test;
2   
3   import java.util.Date;
4   import java.util.HashMap;
5   import java.util.LinkedList;
6   import java.util.List;
7   import java.util.Random;
8   import java.util.StringTokenizer;
9   
10  import net.wotonomy.datastore.DataSoup;
11  import net.wotonomy.datastore.SerializedFileSoup;
12  import net.wotonomy.datastore.XMLFileSoup;
13  import net.wotonomy.foundation.internal.ValueConverter;
14  
15  public class TestMap extends HashMap
16  {
17      public TestMap()
18      {
19      	put( "date", new Date() );
20      	put( "firstName", randomParse(
21              "Bert|Ernie|Elmo|Zoe|Arthur|Emily|DJ|Grover|Oscar|Max|Big|Twinkle") );
22          put( "middleName", new StringBuffer( randomParse(
23              "Rufus|Remy|Martin|Josephus|Ulysses|Homer|Bart|Tip|Onegin|Meredith|Jay") ) );
24          put( "lastName", randomParse(
25              "Alejandro|Alexander|Bird|Gosling|Joy|Van Hoff|Pedia|Marr|McNealy|Ping") );
26          put( "address", randomParse( "1|2|3|4" ) + randomParse( "0|1|00|10|5|50" ) +
27              randomParse( "0|00|1|01|5|05|9|09||000" ) +	" " + randomParse(
28              "Merry|Berry|Perry|Jerry|Meadow|Falls|Elm|Raspberry|Strawberry") + " "
29              + randomParse( "Road|Lane|Court|Drive|Parkway|Terrace" ) );
30          put( "city", randomParse(
31              "Springfield|Sterling|Cascades|Vienna|Reston|Paris|London|Runnymeade") );
32          put( "state", randomParse(
33              "TX|NJ|NY|VA|DC|MD|NC|SC|WV|AR|FL|CA|TN" ) );
34          put( "zip", ValueConverter.getInteger(
35              randomParse( "1|2|3|4" ) + "0" + randomParse( "0|1|2|3|5" ) +
36              randomParse( "6|7|8|9" ) +	randomParse( "6|7|8|9" ) ) );
37          put( "age", new Short( (short) ( new Random().nextDouble() * 40 + 18 ) ) );
38  		childCount = -1;
39      }
40      
41  	protected int childCount;
42  	public int getChildCount() 
43  	{
44  		if ( childCount == -1 )
45  		{            
46  			//childCount = (int) ( random.nextDouble() * 6 ) - 3; // + 100; // tree scalability test
47              if ( childCount < 0 ) childCount = 0;
48  		}
49  		return childCount; 
50  	};
51  	
52  	protected TestMap[] children;
53  	public TestMap[] getChildren()
54  	{
55  		if ( get( "children" ) == null )
56  		{
57  			int n = getChildCount();
58  			TestMap[] children = new TestMap[ n ];
59  			for ( int i = 0; i < n; i++ )
60  			{
61  				children[i] = new TestMap();
62  			}
63              put( "children", children );
64  		}
65  		return (TestMap[]) get( "children" );
66  	}
67  	public void setChildren( TestMap[] aChildArray )
68  	{
69  		put( "children", aChildArray );
70  	}
71  	public List getChildList()
72  	{
73  		List result = new LinkedList();
74  		TestMap[] childArray = getChildren();
75  		for ( int i = 0; i < childArray.length; i++ )
76  		{
77  			result.add( childArray[i] );	
78  		}
79  		return result;
80  	}
81  	public void setChildList( List aChildList )
82  	{
83  		TestMap[] children = new TestMap[ aChildList.size() ];
84  		for ( int i = 0; i < children.length; i++ )
85  		{
86  			children[i] = (TestMap) aChildList.get( i );	
87  		}
88          setChildren( children );
89  	}
90      
91      public String getFullName()
92      {
93          return get( "firstName" ) + " " + get( "middleName" ) + " " + get( "lastName" );   
94      }
95      
96      public boolean equals( Object anObject )
97      {
98      	return anObject == this;
99      }
100 	
101 	public String toString()
102 	{
103 		return "[" + getClass().getName() + ":" + getFullName() + "]";	
104 	}
105 	    
106     // statics
107     
108     private static Random random = new Random();
109     private static String randomParse( String aString )
110     {
111     	String result = "";
112     	StringTokenizer tokens = new StringTokenizer( aString, "|" );
113         int n = (int) ( random.nextDouble() * tokens.countTokens() );
114         for ( int i = 0; i <= n; i++ )
115         {
116             result = tokens.nextToken();
117         }
118         return result;
119     }
120     
121     public static void main( String[] argv ) 
122     {
123         int count = 100;
124         boolean xmlMode = false;
125         if ( argv.length > 0 )
126         {
127             Integer parsed = ValueConverter.getInteger( argv[0] );
128             if ( parsed != null ) count = parsed.intValue();
129             
130             if ( argv.length > 1 )
131             {
132                 if ( argv[1].indexOf( "xml" ) > -1 )
133                 {
134                     xmlMode = true;
135                 }
136             }
137         }
138 
139 long millis = System.currentTimeMillis();
140         
141     	DataSoup store = null;
142         if ( xmlMode )
143         {
144             store = new XMLFileSoup( "testMaps-xml" );
145         }
146         else
147         {
148             store = new SerializedFileSoup( "testMaps-java" );
149         }
150 		
151         Object o;
152         for ( int i = 0; i < count; i++ )
153         {
154             store.addObject( new TestMap() );
155         }
156  /*
157         store.addIndex( "age", "age" );
158         store.addIndex( "zipCode", "zipCode" );
159         store.addIndex( "firstName", "firstName" );
160         store.addIndex( "lastName", "lastName" );
161 */ 
162 System.out.println( System.currentTimeMillis() - millis + " milliseconds" );
163     }
164 }