View Javadoc

1   package net.wotonomy.test;
2   
3   import java.io.Serializable;
4   import java.util.Date;
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.control.EOEditingContext;
11  import net.wotonomy.control.EOKeyValueCodingSupport;
12  import net.wotonomy.datastore.DataSoup;
13  import net.wotonomy.datastore.SerializedFileSoup;
14  import net.wotonomy.datastore.XMLFileSoup;
15  import net.wotonomy.foundation.internal.ValueConverter;
16  
17  public class TestObject implements Serializable // , EOKeyValueCoding
18  {
19      static final long serialVersionUID = -5482454640042392838L;
20      
21  // for testing manual array faulting
22  public EOEditingContext editingContext;
23  public EOEditingContext getEditingContext() { return editingContext; };
24      
25      public TestObject()
26      {
27      	date = new Date();
28      	firstName = randomParse(
29              "Bert|Ernie|Elmo|Zoe|Arthur|Emily|DJ|Grover|Oscar|Max|Big|Twinkle");
30          middleName = new StringBuffer( randomParse(
31              "Rufus|Remy|Martin|Josephus|Ulysses|Homer|Bart|Tip|Onegin|Meredith|Jay") );
32          lastName = randomParse(
33              "Alejandro|Alexander|Bird|Gosling|Joy|Van Hoff|Pedia|Marr|McNealy|Ping");
34          address = randomParse( "1|2|3|4" ) + randomParse( "0|1|00|10|5|50" ) +
35              randomParse( "0|00|1|01|5|05|9|09||000" ) +	" " + randomParse(
36              "Merry|Berry|Perry|Jerry|Meadow|Falls|Elm|Raspberry|Strawberry") + " "
37              + randomParse( "Road|Lane|Court|Drive|Parkway|Terrace" );
38          city = randomParse(
39              "Springfield|Sterling|Cascades|Vienna|Reston|Paris|London|Runnymeade");
40          state = randomParse(
41              "TX|NJ|NY|VA|DC|MD|NC|SC|WV|AR|FL|CA|TN" );
42          zip = ValueConverter.getIntValue(
43              randomParse( "1|2|3|4" ) + "0" + randomParse( "0|1|2|3|5" ) +
44              randomParse( "6|7|8|9" ) +	randomParse( "6|7|8|9" ) );
45          age = (short) ( new Random().nextDouble() * 40 + 18 );
46  		childCount = -1;
47  //	    children = null;
48          childList = null;
49      }
50      
51      protected Date date;
52      public Date getCreateDate() { return date; }
53      public void setCreateDate( Date aDate ) { date = aDate; }
54      
55      protected String firstName;
56      public String getFirstName() { return firstName; }
57      public void setFirstName( String aName ) { firstName = aName; }
58      
59      protected String lastName;
60      public String getLastName() { return lastName; }
61      public void setLastName( String aName ) { 
62          if ( "Jones".equals( aName ) ) throw new RuntimeException( "Jones not allowed" ) ;
63          lastName = aName; 
64      }
65      
66      protected StringBuffer middleName;
67      public StringBuffer getMiddleName() { return middleName; }
68      public void setMiddleName( StringBuffer aName ) { middleName = aName; }
69      
70      protected String address;
71      public String getAddress() { return address; }
72      public void setAddress( String anAddress ) { address = anAddress; }
73      
74      protected String city;
75      public String getCity() { return city; }
76      public void setCity( String aCity ) { city = aCity; }
77      
78      protected String state;
79      public String getState() { return state; }
80      public void setState( String aState ) { state = aState; }
81          
82      protected int zip;
83      public int getZipCode() { return zip; }
84      public void setZipCode( int aZipCode ) { zip = aZipCode; }
85          
86      protected short age;
87      public short getAge() { return age; }
88      public void setAge( short anAge ) { age = anAge; }
89      
90      protected boolean special;
91      public Boolean isSpecial() { return new Boolean( special ); }
92      public void setSpecial( Boolean isSpecial ) { special = isSpecial.booleanValue(); }
93  	
94  /*	
95  	protected Object[] children;
96      
97  	private Object[] getChildren()
98  	{
99  		if ( children == null )
100 		{
101 			int n = getChildCount();
102 			children = new Object[ n ];
103 			for ( int i = 0; i < n; i++ )
104 			{
105 				children[i] = new TestObject();
106 			}
107             //System.out.println( "TestObject.getChildren: " + toString() + " : " + getChildCount() );        
108 		}
109 		return children;
110 	}
111 	private void setChildren( Object[] aChildArray )
112 	{
113 		children = aChildArray;
114         childCount = aChildArray.length;
115 	}
116 
117     // following child list implementation wraps child array
118     
119 	public List getChildList()
120 	{
121 		List result = new LinkedList();
122 		Object[] childArray = getChildren();
123 		for ( int i = 0; i < childArray.length; i++ )
124 		{
125 			result.add( childArray[i] );	
126 		}
127 		return result;
128 	}
129 	public void setChildList( List aChildList )
130 	{
131 		children = new Object[ aChildList.size() ];
132 		for ( int i = 0; i < children.length; i++ )
133 		{
134 			children[i] = (TestObject) aChildList.get( i );	
135 		}
136         childCount = children.length;
137 	}
138 */    
139 	protected int childCount;
140 	public int getChildCount() 
141 	{
142 		if ( childCount == -1 )
143 		{            
144 // uncomment this to enable random children
145 //			childCount = (int) ( random.nextDouble() * 6 ) - 3; // + 100; // tree scalability test
146             if ( childCount < 0 ) childCount = 0;
147 		}
148         
149 // this tests internal count        
150 //		return childCount; 
151 // this tests deferred count    
152         if ( childList != null )
153         {
154             return childList.size();
155         }
156         else
157         {
158             return 0;
159         }
160 	};
161 
162     // following child list implementation stands alone
163 
164     protected List childList;
165 	public List getChildList()
166 	{
167 /* 
168         // this tests random child population        
169 		if ( childList == null )
170 		{
171 			int n = getChildCount();
172 			childList = new LinkedList();
173 			for ( int i = 0; i < n; i++ )
174 			{
175 				childList.add( new TestObject() );
176 			}
177 		}
178 */
179         // this tests manual loading        
180 		if ( childList == null )
181 		{
182             childList = new LinkedList();
183 		}
184 		return childList;
185 	}
186 	public void setChildList( List aChildList )
187 	{
188         childList = aChildList;
189 	}
190     
191     protected TestObject parent;
192     public TestObject getParent() { return parent; }
193     public void setParent( TestObject anObject ) { parent = anObject; }
194     
195     public String getHash() { return Integer.toHexString( System.identityHashCode( this ) ); }
196     
197     public String getFullName() 
198     {
199 //    	return getHash() + ": " + firstName + " " + middleName + " " + lastName;
200     	return firstName + " " + middleName + " " + lastName;
201     }
202     
203     public boolean equals( Object anObject )
204     {
205     	return anObject == this;
206     }
207 	
208 	public String toString()
209 	{
210 		return "[" + getClass().getName() 
211             + "@" + Integer.toHexString( System.identityHashCode(this) )
212             + ":" + getFullName() + "]";	
213 	}
214 	    
215     // statics
216     
217     private static Random random = new Random();
218     private static String randomParse( String aString )
219     {
220     	String result = "";
221     	StringTokenizer tokens = new StringTokenizer( aString, "|" );
222         int n = (int) ( random.nextDouble() * tokens.countTokens() );
223         for ( int i = 0; i <= n; i++ )
224         {
225             result = tokens.nextToken();
226         }
227         return result;
228     }
229 
230     
231     // interface EOKeyValueCoding:
232     // disable this interface by commenting out the "implements" declaration
233     
234     /***
235     * Returns the value for the specified property.
236     * If the property does not exist, this method should
237     * call handleQueryWithUnboundKey.
238     */
239     public Object valueForKey( String aKey )
240     {
241         System.out.println( "valueForKey: " + aKey );
242         return EOKeyValueCodingSupport.valueForKey( this, aKey );
243     }
244 
245     /***
246     * Sets the property to the specified value.
247     * If the property does not exist, this method should
248     * call handleTakeValueForUnboundKey.
249     * If the property is of a type that cannot allow
250     * null (e.g. primitive types) and aValue is null,
251     * this method should call unableToSetNullForKey.
252     */
253     public void takeValueForKey( Object aValue, String aKey )
254     {
255         System.out.println( "takeValueForKey: " + aValue + " : " + aKey );
256         EOKeyValueCodingSupport.takeValueForKey( this, aValue, aKey );
257     }
258 
259     /***
260     * Returns the value for the private field that 
261     * corresponds to the specified property.
262     */
263     public Object storedValueForKey( String aKey )
264     {
265         System.out.println( "storedValueForKey: " + aKey );
266         return EOKeyValueCodingSupport.storedValueForKey( this, aKey );
267     }
268 
269     /***
270     * Sets the the private field that corresponds to the 
271     * specified property to the specified value.
272     */
273     public void takeStoredValueForKey( Object aValue, String aKey )
274     {
275         System.out.println( "takeStoredValueForKey: " + aValue + " : " + aKey );
276         EOKeyValueCodingSupport.takeStoredValueForKey( this, aValue, aKey );
277     }
278 
279     /***
280     * Called by valueForKey when the specified key is
281     * not found on this object.  Implementing classes 
282     * should handle the specified value or otherwise 
283     * throw an exception.
284     */
285     public Object handleQueryWithUnboundKey( String aKey )
286     {
287         System.out.println( "handleQueryWithUnboundKey: " + aKey );
288         return EOKeyValueCodingSupport.handleQueryWithUnboundKey( this, aKey );
289     }
290 
291     /***
292     * Called by takeValueForKey when the specified key
293     * is not found on this object.  Implementing classes
294     * should handle the specified value or otherwise 
295     * throw an exception.
296     */
297     public void handleTakeValueForUnboundKey( Object aValue, String aKey )
298     {
299         System.out.println( "handleTakeValueForUnboundKey: " + aValue + " : " + aKey );
300         EOKeyValueCodingSupport.handleTakeValueForUnboundKey( this, aValue, aKey );
301     }
302 
303     /***
304     * Called by takeValueForKey when the type of the
305     * specified key is not allowed to be null, as is
306     * the case with primitive types.  Implementing 
307     * classes should handle this case appropriately
308     * or otherwise throw an exception.
309     */
310     public void unableToSetNullForKey( String aKey )
311     {
312         System.out.println( "unableToSetNullForKey: " + aKey );
313         EOKeyValueCodingSupport.unableToSetNullForKey( this, aKey );
314     }
315 
316     
317     // main entry point    
318     
319     public static void main( String[] argv ) 
320     {
321         int count = 100;
322         boolean xmlMode = false;
323         if ( argv.length > 0 )
324         {
325             Integer parsed = ValueConverter.getInteger( argv[0] );
326             if ( parsed != null ) count = parsed.intValue();
327             
328             if ( argv.length > 1 )
329             {
330                 if ( argv[1].indexOf( "xml" ) > -1 )
331                 {
332                     xmlMode = true;
333                 }
334             }
335         }
336 
337 long millis = System.currentTimeMillis();
338         
339     	DataSoup store = null;
340         if ( xmlMode )
341         {
342             store = new XMLFileSoup( "testObjects-xml" );
343         }
344         else
345         {
346             store = new SerializedFileSoup( "testObjects-java" );
347         }
348 		
349         Object o;
350         for ( int i = 0; i < count; i++ )
351         {
352             store.addObject( new TestObject() );
353         }
354  /*
355         store.addIndex( "age", "age" );
356         store.addIndex( "zipCode", "zipCode" );
357         store.addIndex( "firstName", "firstName" );
358         store.addIndex( "lastName", "lastName" );
359 */ 
360 System.out.println( System.currentTimeMillis() - millis + " milliseconds" );
361     }
362 }