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" ); } }