commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trei...@apache.org
Subject cvs commit: jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid VersionOneGenerator.java
Date Mon, 31 May 2004 07:04:46 GMT
treilly     2004/05/31 00:04:46

  Modified:    id/src/java/org/apache/commons/id/uuid
                        VersionOneGenerator.java
  Log:
  Fixes error in generator mangled time values. Current users should apply this version. Net
affect is minimal due to clock sequence
  Moved shared constants Contants interface
  
  Revision  Changes    Path
  1.4       +20 -41    jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/VersionOneGenerator.java
  
  Index: VersionOneGenerator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/VersionOneGenerator.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- VersionOneGenerator.java	28 Apr 2004 03:12:01 -0000	1.3
  +++ VersionOneGenerator.java	31 May 2004 07:04:46 -0000	1.4
  @@ -16,7 +16,6 @@
   package org.apache.commons.id.uuid;
   
   import java.lang.reflect.InvocationTargetException;
  -import java.math.BigInteger;
   
   import org.apache.commons.discovery.tools.DiscoverSingleton;
   import org.apache.commons.id.IdentifierGenerator;
  @@ -36,32 +35,15 @@
    *
    */
   
  -public final class VersionOneGenerator implements IdentifierGenerator {
  +public final class VersionOneGenerator implements IdentifierGenerator, Constants {
   
       /** Positions 10-16: Length of node bytes */
       protected static final int NODE_ID_BYTE_LENGTH = 6;
   
  -    // ** Array positions of UUID fields ** //
  -    /** Position 0: First TIME_LOW byte */
  -    private static final int TIME_LOW_BYTE0 = 0;
  -    /** Position 1: Next TIME_LOW byte */
  -    private static final int TIME_LOW_BYTE1 = 1;
  -    /** Position 2: Next TIME_LOW byte */
  -    private static final int TIME_LOW_BYTE2 = 2;
  -    /** Position 3: Last TIME_LOW byte */
  -    private static final int TIME_LOW_BYTE3 = 3;
  -    /** Position 4: First TIME_MID byte */
  -    private static final int TIME_MID_BYTE4 = 4;
  -    /** Position 5: Last TIME_MID byte */
  -    private static final int TIME_MID_BYTE5 = 5;
  -    /** Position 6: First TIME_HI and VERSION byte */
  -    private static final int TIME_HI_VERSION_BYTE6 = 6;
  -    /** Position 7: Last TIME_HI and VERSION byte */
  -    private static final int TIME_HI_VERSION_BYTE7 = 7;
       /** Position 8: CLOCK_SEQ_LOW byte */
  -    private static final int CLOCK_LOW_BYTE8 = 8;
  +    private static final int CLOCK_HI_VARIANT_BYTE8 = 8;
       /** Position 9: CLOCK_SEQ_HIGH and VARIANT byte */
  -    private static final int CLOCK_HI_VARIANT_BYTE9 = 9;
  +    private static final int CLOCK_LOW_BYTE9 = 9;
       /** Positions 10-16: Start of node bytes */
       private static final int NODE_ID_BYTE10 = 10;
   
  @@ -120,10 +102,10 @@
        * @return Returns a new version 1 UUID.
        */
       public synchronized UUID nextUUID() {
  -        byte[] rawUUID = new byte[UUID.BYTE_LENGTH];
  -        Node node = manager.currentNode();
  +        byte[] rawUUID = new byte[UUID_BYTE_LENGTH];
           long time = 0;
           short clockSq = 0;
  +        Node node = manager.currentNode();
           while (time < 1) {
               try {
                   manager.lockNode(node);
  @@ -137,23 +119,20 @@
                   manager.releaseNode(node);
               }
           }
  +        byte[] timeBytes = Bytes.toBytes(time);
  +        //Copy time low
  +        System.arraycopy(timeBytes, TIME_LOW_TS_POS, rawUUID, TIME_LOW_START_POS, TIME_LOW_BYTE_LEN);
  +        //Copy time mid
  +        System.arraycopy(timeBytes, TIME_MID_TS_POS, rawUUID, TIME_MID_START_POS, TIME_MID_BYTE_LEN);
  +        //Copy time hi
  +        System.arraycopy(timeBytes, TIME_HI_TS_POS, rawUUID, TIME_HI_START_POS, TIME_HI_BYTE_LEN);
  +        //Set version
  +        rawUUID[6] |= 0x10; // 0001 0000
  +        //Set clock sequence
  +        rawUUID[CLOCK_HI_VARIANT_BYTE8] = (byte) ((clockSq & 0x3F00) >>> 8);
  +        rawUUID[CLOCK_HI_VARIANT_BYTE8] |= 0x80;
  +        rawUUID[CLOCK_LOW_BYTE9] = (byte) (clockSq & 0xFF);
   
  -        rawUUID[TIME_LOW_BYTE0] = (byte) (time & 0xFF);
  -        rawUUID[TIME_LOW_BYTE1] = (byte) ((time >> 8) & 0xFF);
  -        rawUUID[TIME_LOW_BYTE2] = (byte) ((time >> 16) & 0xFF);
  -        rawUUID[TIME_LOW_BYTE3] = (byte) ((time >> 24) & 0xFF);
  -
  -        rawUUID[TIME_MID_BYTE4] = (byte) ((time >> 32) & 0xFF);
  -        rawUUID[TIME_MID_BYTE5] = (byte) ((time >> 40) & 0xFF);
  -
  -        rawUUID[TIME_HI_VERSION_BYTE6] = (byte) ((time >> 48) & 0x0F);
  -        rawUUID[TIME_HI_VERSION_BYTE6] |= (1 << 4);
  -        rawUUID[TIME_HI_VERSION_BYTE7] = (byte) ((time >> 56) & 0xFF);
  -
  -        rawUUID[CLOCK_LOW_BYTE8] = (byte) (clockSq & 0xFF);
  -        rawUUID[CLOCK_HI_VARIANT_BYTE9] = (byte) ((clockSq & 0x3F00) >> 8);
  -        rawUUID[CLOCK_HI_VARIANT_BYTE9] |= 0x80;
  -
  -        return new UUID(new BigInteger(1, rawUUID));
  +        return new UUID(rawUUID);
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message