Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags.
   * See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
   * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA  02110-1301, USA.
  *
  * (C) 2005-2006,
  * @author JBoss Inc.
  */
 /*
  * Copyright (C) 1998, 1999, 2000, 2001,
  *
  * Arjuna Solutions Limited,
  * Newcastle upon Tyne,
  * Tyne and Wear,
  * UK.
  *
  * $Id: Uid.java 2342 2006-03-30 13:06:17Z  $
  */
 
 package com.arjuna.ats.arjuna.common;
 
 
Implements a unique identity class. Since 4.9 each instance is immutable.

Author(s):
Mark Little (mark@arjuna.com)
Version:
$Id: Uid.java 2342 2006-03-30 13:06:17Z $
Since:
1.0.
 
 
 public class Uid implements CloneableSerializable
 {
     private static final long serialVersionUID = 7808395904206530189L;

    
Create a new instance.
 
 
     public Uid()
     {
          = null;
          = 0;
          = 0;
          = 0;
          = -1;
          = false;
          = null;
          = null;
 
         try
         {
              = Utility.hostInetAddr(); /* calculated only once */
              = Utility.getpid();
 
             if (. == 0)
                 . = (int) (System.currentTimeMillis() / 1000);
 
              = .;
 
              = Uid.getValue();
 
              = true;
 
             generateHash();
         }
         catch (UnknownHostException e) {
             ..warn_common_Uid_1();
              = false;
         }
     }

    
Create a copy of the specified identifier.
 
    public Uid(Uid copyFrom)
    {
        copy(copyFrom);
    }
    public Uid (byte[] byteForm)
    {
        if (byteForm == null)
            throw new IllegalArgumentException();
        
         = new long[2];
         = -1;
         = null;
         = null;
        
        try
        {
            ByteArrayInputStream ba = new ByteArrayInputStream(byteForm);
            DataInputStream ds = new DataInputStream(ba);
            
            [0] = ds.readLong();
            [1] = ds.readLong();
             = ds.readInt();
             = ds.readInt();
             = ds.readInt();
            
             = true;
        }
        catch (final Throwable ex) {
            ..warn_common_Uid_bytes(ex);
             = false;
        }
        
        generateHash();
    }
    
    
Create Uid from string representation. If the string does not represent a valid Uid then the instance will be set to nullUid.
    public Uid(String uidString)
    {
        this(uidStringtrue);
    }

    
Create Uid from string representation. boolean arg says whether to give up if an error is detected or to simply replace with nullUid.
    public Uid(String uidStringboolean errsOk)
    {
        if (uidString == null)
            throw new IllegalArgumentException();
        
        char theBreakChar = Uid.getBreakChar(uidString);
         = new long[2];
         = 0;
         = 0;
         = 0;
         = -1;
         = false;
         = null;
         = null;
        
        if (uidString.length() > 0)
        {
            int startIndex = 0;
            int endIndex = 0;
            String s = null;
            try
            {
                while (uidString.charAt(endIndex) != theBreakChar)
                    endIndex++;
                s = uidString.substring(startIndexendIndex);
                [0] = Utility.hexStringToLong(s);
                startIndex = endIndex + 1;
                endIndex++;
                while (uidString.charAt(endIndex) != theBreakChar)
                    endIndex++;
                s = uidString.substring(startIndexendIndex);
                [1] = Utility.hexStringToLong(s);
                startIndex = endIndex + 1;
                endIndex++;
                while (uidString.charAt(endIndex) != theBreakChar)
                    endIndex++;
                s = uidString.substring(startIndexendIndex);
                 = (int) Utility.hexStringToLong(s);
                startIndex = endIndex + 1;
                endIndex++;
                while (uidString.charAt(endIndex) != theBreakChar)
                    endIndex++;
                s = uidString.substring(startIndexendIndex);
                 = (int) Utility.hexStringToLong(s);
                s = uidString.substring(endIndex + 1, uidString.length());
                 = (int) Utility.hexStringToLong(s);
                 = true;
                generateHash();
            }
            catch (NumberFormatException e)
            {
                if (!errsOk) {
                    ..warn_common_Uid_3(uidStringe);
                }
                 = false;
            }
            catch (StringIndexOutOfBoundsException e)
            {
                if (!errsOk) {
                    ..warn_common_Uid_3(uidStringe);
                }
                 = false;
            }
            catch (final Throwable ex) {
                ..warn_common_Uid_npe(uidStringex);
                 = false;
            }
        }
        else
        {
            this.copy(Uid.nullUid());
        }
        if (!)
        {
            if (errsOk)
            {
                try
                {
                    /*
                     * We do this so the instance is printable, but
                     * it shouldn't be usable from here on in!
                     */
                    
                    this.copy(Uid.nullUid());
                    
                     = false;
                }
                catch (Exception e)
                {
                    ..fatal_common_Uid_4(uidString);
                    throw new FatalError(..get_common_Uid_2(), e);
                }
            }
            else
            {
                throw new FatalError(..get_common_Uid_5(uidString));
            }
        }
    }
    public Uid(long[] addrint processIdint timeint incr)
    {
        try
        {
             = new long[2];
            [0] = addr[0];
            [1] = addr[1];
             = processId;
             = time;
             = incr;
             = true;
            generateHash();
        }
        catch (Throwable ex)
        {
             = false;
            throw new FatalError(..get_common_Uid_11()
                    + ex);
        }
    }

    
Override Object.hashCode. We always return a positive value.
    /*
     * generateHash should have been called by now.
     */
    public int hashCode ()
    {
        return ;
    }

    
Print a human-readable form of the Uid.
    public void print (PrintStream strm)
    {
        strm.print("<Uid:" + this.toString() + ">");
    }
    public String stringForm ()
    {
        // no need to synchronize since object is immutable
        if ( == null)
             = Utility.longToHexString([0]) + .
                    + Utility.longToHexString([1]) + .
                    + Utility.intToHexString() + .
                    + Utility.intToHexString() + .
                    + Utility.intToHexString();
        return ;
    }

    

Returns:
a string representation of the Uid with all : replaced by _ so that the name may be used as a file name.
    public String fileStringForm ()
    {
        return Utility.longToHexString([0]) + .
                + Utility.longToHexString([1]) + .
                + Utility.intToHexString() + .
                + Utility.intToHexString() + .
                + Utility.intToHexString();
    }
    
    
Get the byte representation of the Uid. Useful for packing and creating other representations of transaction ids.

Returns:
the byte array. Cached once created.
    
    public byte[] getBytes ()
    {
        /*
         * We should only really be doing this once, so overhead should
         * be negligible.
         */
        
        if ( == null)
        {
            ByteArrayOutputStream ba = new ByteArrayOutputStream();
            DataOutputStream ds = new DataOutputStream(ba);
            
            try
            {
                ds.writeLong([0]);
                ds.writeLong([1]);
                ds.writeInt();
                ds.writeInt();
                ds.writeInt();
                //_byteForm = stringForm().getBytes("UTF-8");
                
                 = ba.toByteArray();
            }
            catch (final Throwable ex) {
                ..warn_common_Uid_getbytes(ex);
                 = null;
            }
        }
        return ;
    }

    
Same as stringForm()
    public String toString ()
    {
        return stringForm();
    }
    // return the process id value in hex form.
    // The internal format is Uids mostly should not be exposed, but some
    // recovery/expiry code need this.
    public String getHexPid ()
    {
        return Utility.intToHexString();
    }

    
Create a copy of this instance.
    public Object clone () throws CloneNotSupportedException
    {
        return new Uid(this);
    }

    
Copy the specified Uid over this instance.
    private void copy (Uid toCopy)
    {
        if (toCopy == this)
            return;
         = toCopy.hostAddr;
         = toCopy.process;
         = toCopy.sec;
         = toCopy.other;
         = toCopy._hashValue;
         = toCopy._valid;
    }

    
Uid comparisons.


    
Override Object.equals
    public boolean equals (Object o)
    {
        if (o instanceof Uid)
            return this.equals((Uido);
        else
            return false;
    }
    public boolean equals (Uid u)
    {
        if (u == null)
            return false;
        if (u == this)
            return true;
        return (( == u.other) && ( == u.sec) && ( == u.process)
                && ([0] == u.hostAddr[0]) && ([1] == u.hostAddr[1]));
    }
    public boolean notEquals (Uid u)
    {
        if (u == null)
            return true;
        if (u == this)
            return false;
        return (( != u.other) || ( != u.sec) || ( != u.process)
                || ([0] != u.hostAddr[0]) || ([1] != u.hostAddr[1]));
    }
    public boolean lessThan (Uid u)
    {
        if (u == null)
            return false;
        if (u == this)
            return false;
        if (this.equals(u))
            return false;
        if (.equals(this))
            return false;
        if (.equals(u))
            return true;
        if (([0] < u.hostAddr[0]) && ([1] < u.hostAddr[1]))
            return true;
        else
        {
            if (([0] == u.hostAddr[0])
                    && ([1] == u.hostAddr[1]))
            {
                if ( < u.process)
                    return true;
                else if ( == u.process)
                {
                    if ( < u.sec)
                        return true;
                    else if (( == u.sec) && ( < u.other))
                        return true;
                }
            }
        }
        return false;
    }
    public boolean greaterThan (Uid u)
    {
        if (u == null)
            return false;
        if (u == this)
            return false;
        if (this.equals(u))
            return false;
        if (.equals(this))
            return true;
        if (.equals(u))
            return false;
        if (([0] > u.hostAddr[0]) && ([1] > u.hostAddr[1]))
            return true;
        else
        {
            if (([0] == u.hostAddr[0])
                    && ([1] == u.hostAddr[0]))
            {
                if ( > u.process)
                    return true;
                else if ( == u.process)
                {
                    if ( > u.sec)
                        return true;
                    else if (( == u.sec) && ( > u.other))
                        return true;
                }
            }
        }
        return false;
    }

    
Is the Uid valid?
    public final boolean valid ()
    {
        return ;
    }

    
Return a null Uid (0:0:0:0:0)
    public static final Uid nullUid ()
    {
        return ;
    }

    
Return a last resource Uid (0:0:0:0:1)
    public static final Uid lastResourceUid ()
    {
        return ;
    }

    
Return the maximum Uid (7fffffff:7fffffff:7fffffff:7fffffff:7fffffff)
    public static final Uid maxUid ()
    {
        return ;
    }

    
Return the minimum Uid (-80000000:-80000000:-80000000:-80000000:-80000000)
    public static final Uid minUid ()
    {
        return ;
    }
    /*
     * Serialization methods. Similar to buffer packing. If the Uid is invalid
     * the an IOException is thrown.
     * @since JTS 2.1.
     */
    private void writeObject (java.io.ObjectOutputStream out)
            throws IOException
    {
        if ()
        {
            out.defaultWriteObject();
            out.writeLong([0]);
            out.writeLong([1]);
            out.writeInt();
            out.writeInt();
            out.writeInt();
        }
        else
            throw new IOException("Invalid Uid object.");
    }
    /*
     * Serialization methods. Similar to buffer unpacking. If the
     * unserialization fails then an IOException is thrown.
     * @since JTS 2.1.
     */
    private void readObject (java.io.ObjectInputStream inthrows IOException,
            ClassNotFoundException
    {
        try
        {
            in.defaultReadObject();
        	
             = new long[2];
            
            [0] = in.readLong();
            [1] = in.readLong();
             = in.readInt();
             = in.readInt();
             = in.readInt();
             = true// should not be able to pack an invalid Uid.
            generateHash();
        }
        catch (IOException e)
        {
             = false;
            throw e;
        }
    }
    private static final int MAX_SEQ_VALUE = 0x40000000; // 2^30, which is a bit
                                                         // conservative.
    private static int getValue ()
    {
        int value = 0;
        do
        {
            value = .getAndIncrement();
            if (value == )
            {
                .set(0);
                 = (int) (System.currentTimeMillis() / 1000);
            }
        }
        while (value >= );
        return value;
    }
    /*
     * Is an idempotent operation, so can be called more than once without
     * adverse effect.
     */
    private final void generateHash ()
    {
        if ()
        {
            if (true)
                 = (int[0] ^ (int[1] ^ 
                        ^  ^ ;
            else
            {
                int g = 0;
                String p = toString();
                int len = p.length();
                int index = 0;
                if (len > 0)
                {
                    while (len-- > 0)
                    {
                         = ( << 4)
                                + (int) (p.charAt(index));
                        g =  & 0xf0000000;
                        if (g > 0)
                        {
                             =  ^ (g >> 24);
                             =  ^ g;
                        }
                        index++;
                    }
                }
            }
            if ( < 0)
                 = -;
        }
        else {
            ..warn_common_Uid_6();
        }
    }
    /*
     * Since we may be given a Uid from the file system (which uses '_' to
     * separate fields, we need to be able to convert.
     */
    private static final char getBreakChar (String uidString)
    {
        if (uidString == null)
            return .;
        if (uidString.indexOf() != -1)
            return .;
        else
            return .;
    }
    protected volatile long[] hostAddr// representation of ipv6 address (and
                                        // ipv4)
    protected volatile int process;
    protected volatile int sec;
    protected volatile int other;
    private volatile int _hashValue;
    private volatile boolean _valid;
    private volatile String _stringForm;
    
    private volatile byte[] _byteForm;
    private static final AtomicInteger uidsCreated = new AtomicInteger();
    private static volatile int initTime;
    private static final char breakChar = ':';
    private static final char fileBreakChar = '_';
    private static final Uid NIL_UID = new Uid("0:0:0:0:0");
    private static final Uid LAST_RESOURCE_UID = new Uid("0:0:0:0:1");
    private static final Uid MAX_UID = new Uid(
            "7fffffff:7fffffff:7fffffff:7fffffff:7fffffff");
    private static final Uid MIN_UID = new Uid(
            "-80000000:-80000000:-80000000:-80000000:-80000000");
    
    public static final int UID_SIZE = 2*8 + 3*4; // in bytes
New to GrepCode? Check out our FAQ X