package net.wotonomy.test; import java.io.Serializable; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Random; import java.util.StringTokenizer; import net.wotonomy.control.EOEditingContext; import net.wotonomy.control.EOKeyValueCodingSupport; import net.wotonomy.datastore.DataSoup; import net.wotonomy.datastore.SerializedFileSoup; import net.wotonomy.datastore.XMLFileSoup; import net.wotonomy.foundation.internal.ValueConverter; public class TestObject implements Serializable // , EOKeyValueCoding { static final long serialVersionUID = -5482454640042392838L; // for testing manual array faulting public EOEditingContext editingContext; public EOEditingContext getEditingContext() { return editingContext; }; public TestObject() { date = new Date(); firstName = randomParse( "Bert|Ernie|Elmo|Zoe|Arthur|Emily|DJ|Grover|Oscar|Max|Big|Twinkle"); middleName = new StringBuffer( randomParse( "Rufus|Remy|Martin|Josephus|Ulysses|Homer|Bart|Tip|Onegin|Meredith|Jay") ); lastName = randomParse( "Alejandro|Alexander|Bird|Gosling|Joy|Van Hoff|Pedia|Marr|McNealy|Ping"); 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" ); city = randomParse( "Springfield|Sterling|Cascades|Vienna|Reston|Paris|London|Runnymeade"); state = randomParse( "TX|NJ|NY|VA|DC|MD|NC|SC|WV|AR|FL|CA|TN" ); zip = ValueConverter.getIntValue( randomParse( "1|2|3|4" ) + "0" + randomParse( "0|1|2|3|5" ) + randomParse( "6|7|8|9" ) + randomParse( "6|7|8|9" ) ); age = (short) ( new Random().nextDouble() * 40 + 18 ); childCount = -1; // children = null; childList = null; } protected Date date; public Date getCreateDate() { return date; } public void setCreateDate( Date aDate ) { date = aDate; } protected String firstName; public String getFirstName() { return firstName; } public void setFirstName( String aName ) { firstName = aName; } protected String lastName; public String getLastName() { return lastName; } public void setLastName( String aName ) { if ( "Jones".equals( aName ) ) throw new RuntimeException( "Jones not allowed" ) ; lastName = aName; } protected StringBuffer middleName; public StringBuffer getMiddleName() { return middleName; } public void setMiddleName( StringBuffer aName ) { middleName = aName; } protected String address; public String getAddress() { return address; } public void setAddress( String anAddress ) { address = anAddress; } protected String city; public String getCity() { return city; } public void setCity( String aCity ) { city = aCity; } protected String state; public String getState() { return state; } public void setState( String aState ) { state = aState; } protected int zip; public int getZipCode() { return zip; } public void setZipCode( int aZipCode ) { zip = aZipCode; } protected short age; public short getAge() { return age; } public void setAge( short anAge ) { age = anAge; } protected boolean special; public Boolean isSpecial() { return new Boolean( special ); } public void setSpecial( Boolean isSpecial ) { special = isSpecial.booleanValue(); } /* protected Object[] children; private Object[] getChildren() { if ( children == null ) { int n = getChildCount(); children = new Object[ n ]; for ( int i = 0; i < n; i++ ) { children[i] = new TestObject(); } //System.out.println( "TestObject.getChildren: " + toString() + " : " + getChildCount() ); } return children; } private void setChildren( Object[] aChildArray ) { children = aChildArray; childCount = aChildArray.length; } // following child list implementation wraps child array public List getChildList() { List result = new LinkedList(); Object[] childArray = getChildren(); for ( int i = 0; i < childArray.length; i++ ) { result.add( childArray[i] ); } return result; } public void setChildList( List aChildList ) { children = new Object[ aChildList.size() ]; for ( int i = 0; i < children.length; i++ ) { children[i] = (TestObject) aChildList.get( i ); } childCount = children.length; } */ protected int childCount; public int getChildCount() { if ( childCount == -1 ) { // uncomment this to enable random children // childCount = (int) ( random.nextDouble() * 6 ) - 3; // + 100; // tree scalability test if ( childCount < 0 ) childCount = 0; } // this tests internal count // return childCount; // this tests deferred count if ( childList != null ) { return childList.size(); } else { return 0; } }; // following child list implementation stands alone protected List childList; public List getChildList() { /* // this tests random child population if ( childList == null ) { int n = getChildCount(); childList = new LinkedList(); for ( int i = 0; i < n; i++ ) { childList.add( new TestObject() ); } } */ // this tests manual loading if ( childList == null ) { childList = new LinkedList(); } return childList; } public void setChildList( List aChildList ) { childList = aChildList; } protected TestObject parent; public TestObject getParent() { return parent; } public void setParent( TestObject anObject ) { parent = anObject; } public String getHash() { return Integer.toHexString( System.identityHashCode( this ) ); } public String getFullName() { // return getHash() + ": " + firstName + " " + middleName + " " + lastName; return firstName + " " + middleName + " " + lastName; } public boolean equals( Object anObject ) { return anObject == this; } public String toString() { return "[" + getClass().getName() + "@" + Integer.toHexString( System.identityHashCode(this) ) + ":" + 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; } // interface EOKeyValueCoding: // disable this interface by commenting out the "implements" declaration /** * Returns the value for the specified property. * If the property does not exist, this method should * call handleQueryWithUnboundKey. */ public Object valueForKey( String aKey ) { System.out.println( "valueForKey: " + aKey ); return EOKeyValueCodingSupport.valueForKey( this, aKey ); } /** * Sets the property to the specified value. * If the property does not exist, this method should * call handleTakeValueForUnboundKey. * If the property is of a type that cannot allow * null (e.g. primitive types) and aValue is null, * this method should call unableToSetNullForKey. */ public void takeValueForKey( Object aValue, String aKey ) { System.out.println( "takeValueForKey: " + aValue + " : " + aKey ); EOKeyValueCodingSupport.takeValueForKey( this, aValue, aKey ); } /** * Returns the value for the private field that * corresponds to the specified property. */ public Object storedValueForKey( String aKey ) { System.out.println( "storedValueForKey: " + aKey ); return EOKeyValueCodingSupport.storedValueForKey( this, aKey ); } /** * Sets the the private field that corresponds to the * specified property to the specified value. */ public void takeStoredValueForKey( Object aValue, String aKey ) { System.out.println( "takeStoredValueForKey: " + aValue + " : " + aKey ); EOKeyValueCodingSupport.takeStoredValueForKey( this, aValue, aKey ); } /** * Called by valueForKey when the specified key is * not found on this object. Implementing classes * should handle the specified value or otherwise * throw an exception. */ public Object handleQueryWithUnboundKey( String aKey ) { System.out.println( "handleQueryWithUnboundKey: " + aKey ); return EOKeyValueCodingSupport.handleQueryWithUnboundKey( this, aKey ); } /** * Called by takeValueForKey when the specified key * is not found on this object. Implementing classes * should handle the specified value or otherwise * throw an exception. */ public void handleTakeValueForUnboundKey( Object aValue, String aKey ) { System.out.println( "handleTakeValueForUnboundKey: " + aValue + " : " + aKey ); EOKeyValueCodingSupport.handleTakeValueForUnboundKey( this, aValue, aKey ); } /** * Called by takeValueForKey when the type of the * specified key is not allowed to be null, as is * the case with primitive types. Implementing * classes should handle this case appropriately * or otherwise throw an exception. */ public void unableToSetNullForKey( String aKey ) { System.out.println( "unableToSetNullForKey: " + aKey ); EOKeyValueCodingSupport.unableToSetNullForKey( this, aKey ); } // main entry point 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( "testObjects-xml" ); } else { store = new SerializedFileSoup( "testObjects-java" ); } Object o; for ( int i = 0; i < count; i++ ) { store.addObject( new TestObject() ); } /* store.addIndex( "age", "age" ); store.addIndex( "zipCode", "zipCode" ); store.addIndex( "firstName", "firstName" ); store.addIndex( "lastName", "lastName" ); */ System.out.println( System.currentTimeMillis() - millis + " milliseconds" ); } }