diff options
| author | Benjamin Culkin <scorpress@gmail.com> | 2024-05-20 17:58:16 -0400 |
|---|---|---|
| committer | Benjamin Culkin <scorpress@gmail.com> | 2024-05-20 17:58:16 -0400 |
| commit | 40a9d99496e098562f090fb7ffce9e749011b131 (patch) | |
| tree | 437df24d65470582e943e494a52db8ed65a881ae /projects/net.wotonomy.persistence/src/main/java/net/wotonomy/control/EOTemporaryGlobalID.java | |
| parent | ff072dfe782f6f22123cd4ba050828d35c0d0fbd (diff) | |
Formatting pass
Diffstat (limited to 'projects/net.wotonomy.persistence/src/main/java/net/wotonomy/control/EOTemporaryGlobalID.java')
| -rw-r--r-- | projects/net.wotonomy.persistence/src/main/java/net/wotonomy/control/EOTemporaryGlobalID.java | 350 |
1 files changed, 166 insertions, 184 deletions
diff --git a/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/control/EOTemporaryGlobalID.java b/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/control/EOTemporaryGlobalID.java index 44ba855..31351a9 100644 --- a/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/control/EOTemporaryGlobalID.java +++ b/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/control/EOTemporaryGlobalID.java @@ -21,199 +21,181 @@ package net.wotonomy.control; import java.net.InetAddress; /** -* EOTemporaryGlobalID is a network-wide unique key. -* This is used by EOEditingContext to construct temporary -* ids when new objects are created. <br><br> -* -* The specified format of the key is a byte array: -* < Sequence [2], ProcessID [2], Time [4], IP Addr [4] >, -* but because java does not allow access to the process id, -* the timestamp of when this class is first loaded is used -* to simulate a process id. -* -* -* @author michael@mpowers.net -* @author $Author: cgruber $ -* @version $Revision: 893 $ -*/ -public class EOTemporaryGlobalID - extends EOGlobalID -{ - /** - * Holds the length in bytes of the key that is generated. - */ - public static final int UniqueBinaryKeyLength = 12; - - private static int sequence; - private static byte[] processid; // 2 bytes - private static byte[] ipaddr; // 4 bytes - - static // static initializer - { - // init sequence - sequence = 0; - - // init processid - processid = new byte[2]; - long time = System.currentTimeMillis(); - processid[1] = (byte) time; - time = time >> 8; - processid[0] = (byte) time; - - // init ipaddr - ipaddr = new byte[4]; - try - { - ipaddr = InetAddress.getLocalHost().getAddress(); - } - catch ( Exception exc ) - { - // could not obtain ip address - use pid twice - ipaddr[0] = processid[0]; - ipaddr[1] = processid[1]; - ipaddr[2] = processid[0]; - ipaddr[3] = processid[1]; - } - } - - private byte[] key; - private int hashCode; - - /** - * Generates a new id with a unique key. - */ - public EOTemporaryGlobalID() - { - key = new byte[UniqueBinaryKeyLength]; - - // init sequence (important byte first) - key[0] = (byte) ( sequence ); - key[1] = (byte) ( sequence >> 8 ); - sequence++; - - // populate pid (important byte first) - key[2] = processid[1]; - key[3] = processid[0]; - - // init time (important byte first) - long time = System.currentTimeMillis(); - key[4] = (byte) time; - time = time >> 8; - key[5] = (byte) time; - time = time >> 8; - key[6] = (byte) time; - time = time >> 8; - key[7] = (byte) time; - - // populate ipaddr - key[8] = ipaddr[0]; - key[9] = ipaddr[1]; - key[10] = ipaddr[2]; - key[11] = ipaddr[3]; - - // use string's hash code - hashCode = new String( key ).hashCode(); - } - - /** - * Private constructor for cloning. - */ - private EOTemporaryGlobalID( byte[] aKey ) - { + * EOTemporaryGlobalID is a network-wide unique key. This is used by + * EOEditingContext to construct temporary ids when new objects are created. + * <br> + * <br> + * + * The specified format of the key is a byte array: < Sequence [2], ProcessID + * [2], Time [4], IP Addr [4] >, but because java does not allow access to + * the process id, the timestamp of when this class is first loaded is used to + * simulate a process id. + * + * + * @author michael@mpowers.net + * @author $Author: cgruber $ + * @version $Revision: 893 $ + */ +public class EOTemporaryGlobalID extends EOGlobalID { + /** + * Holds the length in bytes of the key that is generated. + */ + public static final int UniqueBinaryKeyLength = 12; + + private static int sequence; + private static byte[] processid; // 2 bytes + private static byte[] ipaddr; // 4 bytes + + static // static initializer + { + // init sequence + sequence = 0; + + // init processid + processid = new byte[2]; + long time = System.currentTimeMillis(); + processid[1] = (byte) time; + time = time >> 8; + processid[0] = (byte) time; + + // init ipaddr + ipaddr = new byte[4]; + try { + ipaddr = InetAddress.getLocalHost().getAddress(); + } catch (Exception exc) { + // could not obtain ip address - use pid twice + ipaddr[0] = processid[0]; + ipaddr[1] = processid[1]; + ipaddr[2] = processid[0]; + ipaddr[3] = processid[1]; + } + } + + private byte[] key; + private int hashCode; + + /** + * Generates a new id with a unique key. + */ + public EOTemporaryGlobalID() { + key = new byte[UniqueBinaryKeyLength]; + + // init sequence (important byte first) + key[0] = (byte) (sequence); + key[1] = (byte) (sequence >> 8); + sequence++; + + // populate pid (important byte first) + key[2] = processid[1]; + key[3] = processid[0]; + + // init time (important byte first) + long time = System.currentTimeMillis(); + key[4] = (byte) time; + time = time >> 8; + key[5] = (byte) time; + time = time >> 8; + key[6] = (byte) time; + time = time >> 8; + key[7] = (byte) time; + + // populate ipaddr + key[8] = ipaddr[0]; + key[9] = ipaddr[1]; + key[10] = ipaddr[2]; + key[11] = ipaddr[3]; + + // use string's hash code + hashCode = new String(key).hashCode(); + } + + /** + * Private constructor for cloning. + */ + private EOTemporaryGlobalID(byte[] aKey) { // key = aKey; // this might be faster - // make copy of key - might be safer - key = new byte[ UniqueBinaryKeyLength ]; - for ( int i = 0; i < UniqueBinaryKeyLength; i++ ) - { - key[i] = aKey[i]; - } - - // use string's hash code - hashCode = new String( aKey ).hashCode(); - } - - /** - * Returns true. - */ - public boolean isTemporary() - { - return true; - } - - /** - * Returns whether the keys are equal. - */ - public boolean equals( Object anObject ) - { - if ( ! ( anObject instanceof EOTemporaryGlobalID ) ) - return false; - - byte[] otherKey = ((EOTemporaryGlobalID)anObject).key; - - for ( int i = 0; i < UniqueBinaryKeyLength; i++ ) - { - if ( key[i] != otherKey[i] ) return false; - } - return true; - } - - /** - * Returns a copy of this object. - */ - public Object clone() - { - // faster than super.clone() - return new EOTemporaryGlobalID( key ); - } - - public int hashCode() - { - return hashCode; - } - - /** - * Returns a string representation of this key. - * This is a 24-character string with each pair - * of characters holding a hexadecimal value that - * is 128 more than the value of the corresponding - * byte (to account for two's complement). - */ - public String toString() - { - String hex; - StringBuffer buffer = new StringBuffer(); - for ( int i = 0; i < key.length; i++ ) - { - // get string: adjust for two's complement - hex = Integer.toHexString( key[i]+128 ); - // pad with zero so we take two characters - if ( hex.length() == 1 ) hex = "0" + hex; - // append hex code - buffer.append( hex ); - } - return buffer.toString(); - } + // make copy of key - might be safer + key = new byte[UniqueBinaryKeyLength]; + for (int i = 0; i < UniqueBinaryKeyLength; i++) { + key[i] = aKey[i]; + } + + // use string's hash code + hashCode = new String(aKey).hashCode(); + } + + /** + * Returns true. + */ + public boolean isTemporary() { + return true; + } + + /** + * Returns whether the keys are equal. + */ + public boolean equals(Object anObject) { + if (!(anObject instanceof EOTemporaryGlobalID)) + return false; + + byte[] otherKey = ((EOTemporaryGlobalID) anObject).key; + + for (int i = 0; i < UniqueBinaryKeyLength; i++) { + if (key[i] != otherKey[i]) + return false; + } + return true; + } + + /** + * Returns a copy of this object. + */ + public Object clone() { + // faster than super.clone() + return new EOTemporaryGlobalID(key); + } + + public int hashCode() { + return hashCode; + } + + /** + * Returns a string representation of this key. This is a 24-character string + * with each pair of characters holding a hexadecimal value that is 128 more + * than the value of the corresponding byte (to account for two's complement). + */ + public String toString() { + String hex; + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < key.length; i++) { + // get string: adjust for two's complement + hex = Integer.toHexString(key[i] + 128); + // pad with zero so we take two characters + if (hex.length() == 1) + hex = "0" + hex; + // append hex code + buffer.append(hex); + } + return buffer.toString(); + } } /* - * $Log$ - * Revision 1.1 2006/02/16 13:19:57 cgruber - * Check in all sources in eclipse-friendly maven-enabled packages. + * $Log$ Revision 1.1 2006/02/16 13:19:57 cgruber Check in all sources in + * eclipse-friendly maven-enabled packages. * - * Revision 1.4 2001/04/29 22:02:45 mpowers - * Work on id transposing between editing contexts. + * Revision 1.4 2001/04/29 22:02:45 mpowers Work on id transposing between + * editing contexts. * - * Revision 1.3 2001/02/15 21:13:30 mpowers - * First draft implementation is complete. Now on to debugging. + * Revision 1.3 2001/02/15 21:13:30 mpowers First draft implementation is + * complete. Now on to debugging. * - * Revision 1.2 2001/02/14 23:03:02 mpowers - * A near-complete first draft of EOEditingContext. + * Revision 1.2 2001/02/14 23:03:02 mpowers A near-complete first draft of + * EOEditingContext. * - * Revision 1.1 2001/02/13 23:24:29 mpowers - * Implementing more of editing context. + * Revision 1.1 2001/02/13 23:24:29 mpowers Implementing more of editing + * context. * * */ - - |
