View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Michael Powers
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.datastore;
20  
21  import java.io.Serializable;
22  
23  import net.wotonomy.foundation.internal.ValueConverter;
24  
25  public class DataKey implements Comparable, Serializable, Cloneable
26  {
27  	static final long serialVersionUID = 8421127539579065705L;
28  
29      Long key;
30  
31      public DataKey()
32      {
33          key = new Long( 0 );
34      }
35  
36      /***
37      * Converts string representation to new object.
38      */
39      public DataKey( String aString )
40      {
41          this();
42          setKeyString( aString );
43      }
44      
45      public int hashCode()
46      {
47          return key.intValue();    
48      }
49      
50      public void increment()
51      {
52      	key = new Long( key.longValue() + 1 );
53      }
54      
55      public Object clone()
56      {
57      	return new DataKey( this.toString() );
58      }
59      
60      public String toString() 
61      {
62      	return key.toString();
63      }
64      
65      public String getKeyString() 
66      {
67          return key.toString();
68      }
69      
70      public void setKeyString( String aString )
71      {
72      	Long parsed = ValueConverter.getLong( aString );
73  		if ( parsed != null ) key = parsed;
74      }
75      
76      public boolean equals( Object anObject )
77      {
78      	if ( anObject instanceof String )
79  	    {
80  	    	if ( toString().equals( anObject ) )
81  		    {
82  		    	return true;
83  		    }
84  	    }
85      	if ( ! ( anObject instanceof DataKey ) ) return false;
86  		return key.equals( ((DataKey)anObject).key );
87      }
88      
89      public int compareTo( Object anObject )
90      {
91      	if ( anObject instanceof String )
92  	    {
93  	    	if ( toString().equals( anObject ) )
94  		    {
95  		    	return 0;
96  		    }
97  	    }
98      	if ( ! ( anObject instanceof DataKey ) )
99          {
100             Long converted = (Long) ValueConverter.getLong( anObject );
101             if ( converted != null )
102             {
103                 return (int) ( key.longValue() - converted.longValue() );
104             }
105             return 0;
106         };
107         return (int) ( key.longValue() - ((DataKey)anObject).key.longValue() );
108     }
109 }
110 
111 /*
112  * $Log$
113  * Revision 1.2  2006/02/19 16:26:19  cgruber
114  * Move non-unit-test code to tests project
115  * Fix up code to work with proper imports
116  * Fix maven dependencies.
117  *
118  * Revision 1.1  2006/02/16 13:18:56  cgruber
119  * Check in all sources in eclipse-friendly maven-enabled packages.
120  *
121  * Revision 1.4  2003/08/14 19:29:38  chochos
122  * minor cleanup (imports, static method calls, etc)
123  *
124  * Revision 1.3  2001/02/23 23:44:44  mpowers
125  * Fixes for hashcode to ensure proper key comparison.
126  *
127  * Revision 1.2  2001/02/15 21:12:41  mpowers
128  * Added accessors for key throughout the api.  This breaks compatibility.
129  * insertObject now returns the permanent key for the newly created object.
130  * The old way returned a copy of the object which was an additional read
131  * that was often ignored.  Now you can read it only if you need it.
132  * Furthermore, there was not other way of getting the permanent key.
133  *
134  * Revision 1.1.1.1  2000/12/21 15:47:04  mpowers
135  * Contributing wotonomy.
136  *
137  * Revision 1.2  2000/12/20 16:25:36  michael
138  * Added log to all files.
139  *
140  *
141  */
142