On Sunday, September 8, 2002, at 11:52 , Dmitri Colebatch wrote:

> unjustifyable desire to have a relatively easy way of identifying rows.

Right. Why do it the easy way, when you can make it complicate :-)

> UUIDs are definately quicker from a performance pov....  I'll eventually
> come around (o:

Attached is some lame code to get you started.

Cheers,

PA.


//
//      ===========================================================================
//
//      Title:          SZUUID.java
//      Description:    [Description]
//      Author:         Raphael Szwarc <[EMAIL PROTECTED]>
//      Creation Date:  Sat Sep 08 2001
//      Legal:          Copyright (C) 2001 Raphael Szwarc. All Rights Reserved.
//
//      This file contains Original Code and/or Modifications of Original Code
//      as defined in and that are subject to the Rapha�l Szwarc Public Source 
//      License Version 1.0 (the 'License'). You may not use this file except in 
//      compliance with the License.
//
//      Please obtain a copy of the License at <[EMAIL PROTECTED]> and read it 
//      before using this file.
//      
//      The Original Code and all software distributed under the License are 
//      distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
//      EXPRESS OR IMPLIED, AND Rapha�l Szwarc HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
//      INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 
//      FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
//      
//      Please see the License for the specific language governing rights and 
//      limitations under the License.
//
//      ---------------------------------------------------------------------------
//

package alt.dev.szfoundation;

import java.net.InetAddress;

import java.util.zip.Checksum;
import java.util.zip.CRC32;

import java.util.Arrays;

public final class SZUUID extends Object
{

//      ===========================================================================
//      Constant(s)
//      ---------------------------------------------------------------------------

        public static final int         Length = 16;          // 
LoTime-HiTime-Unique-Sequence-Node
        
        private static final int        LoTimeIndex = 0;      // int -> 4 bytes
        private static final int        HiTimeIndex = 4;      // short -> 2 bytes
        private static final int        UniqueIndex = 6;      // int -> 4 bytes
        private static final int        SequenceIndex = 10;   // short -> 2 bytes
        private static final int        NodeIndex = 12;               // int -> 4 bytes

//      ===========================================================================
//      Class variable(s)
//      ---------------------------------------------------------------------------

        private static final int        _unique = new Object().hashCode();    // new 
Object().hashCode()
        private static short            _sequence = Short.MIN_VALUE;          // 
_sequence++
        private static int              _node = 0;                            // 
InetAddress.getLocalHost().getAddress().hashCode()
        
        private static final Checksum   _checksum = new CRC32();
        
//      ===========================================================================
//      Instance variable(s)
//      ---------------------------------------------------------------------------

//      ===========================================================================
//      Constructor method(s)
//      ---------------------------------------------------------------------------

        private SZUUID()
        {
                super();
        }

//      ===========================================================================
//      Class initialization method(s)
//      ---------------------------------------------------------------------------

        static 
        {
                try
                {
                        _node = InetAddress.getLocalHost().getAddress().hashCode();
                }
                catch (Exception anException)
                {
                        SZLog.error( anException );
                }
        }


//      ===========================================================================
//      Class method(s)
//      ---------------------------------------------------------------------------

        public static byte[] uuid()
        {
                return SZUUID.uuidWithTime( System.currentTimeMillis() );
        }

        public static byte[] uuidWithTime(long aTime)
        {
                byte[]  anUUID = new byte[ SZUUID.Length ];
                int     loTime = (int) aTime;
                short   hiTime = (short) ( aTime >>> 32 );
                
                synchronized ( SZUUID.class ) 
                {
                        if ( _sequence == Short.MAX_VALUE )
                        {
                                _sequence = Short.MIN_VALUE;
                        }
                        
                        _sequence += 1;
                }

                SZByte.writeIntToBytes( anUUID, LoTimeIndex, loTime );
                SZByte.writeShortToBytes( anUUID, HiTimeIndex, hiTime );
                SZByte.writeIntToBytes( anUUID, UniqueIndex, _unique );
                SZByte.writeShortToBytes( anUUID, SequenceIndex, _sequence );
                SZByte.writeIntToBytes( anUUID, NodeIndex, _node );
                
                return anUUID;
        }

        public static long timeWithUUID(byte[] anUUID)
        {
                if ( ( anUUID != null ) && ( anUUID.length == SZUUID.Length ) )
                {
                        int     loTime = SZByte.readIntFromBytes( anUUID, LoTimeIndex 
);
                        short   hiTime = SZByte.readShortFromBytes( anUUID, 
HiTimeIndex );
                        long    aTime = ( ( long ) hiTime ) << 32 | ( ( ( long ) 
loTime ) & 0xFFFFFFFFL );
                        
                        return aTime;
                } 
                
                throw new IllegalArgumentException( "SZUUID.timeWithUUID: invalid 
uuid." );
        }
        
        public static int hashCode(byte[] anUUID)
        {
                if ( ( anUUID != null ) && ( anUUID.length == SZUUID.Length ) )
                {
                        _checksum.reset();
                        _checksum.update( anUUID, 0, SZUUID.Length );
                        
                        return (int) _checksum.getValue();
                } 
                
                throw new IllegalArgumentException( "SZUUID.hashCode: invalid uuid." );
        }
        
        public static boolean equals(byte[] anUUID, byte[] anotherUUID)
        {
                if ( ( anUUID != null ) && ( anUUID.length == SZUUID.Length ) )
                {
                        return Arrays.equals( anUUID, anotherUUID );
                } 
                
                throw new IllegalArgumentException( "SZUUID.equals: invalid uuid." );
        }
        
//      ===========================================================================
//      Instance method(s)
//      ---------------------------------------------------------------------------

}

Reply via email to