image
 
image
GUID.java


/*::.
==================================================================================================================================
================================================= Copyright  2001 Allen Baker =================================================
                                                 +------------------------------+
File:       GUID.java
Originator: Allen Baker (2001.09.02)
LayoutRev:  5
================================================================================================================================== */



/*.
==========================================================================================
Package
------------------------------------------------------------------------------------------ */
package cosmicabyss.com.lib;



/*.
==========================================================================================
Imports
------------------------------------------------------------------------------------------ */
import java.io.*;
import java.util.*;
import java.net.*;
import java.rmi.server.*;



/*::
======================================================================================================================== *//**
Instances of this class create a pure identifier that is guaranteed to be unique in the world under the following
conditions:<P>
   <BLOCKQUOTE>
      => the machine on which it is generated takes more than one second to reboot,<P>
      => the machine's clock is never set backward, and<P>
      => the machine does not have the same Internet address as any other machine in the world that generates a GUID at
         The Exact same internal clock time as this machine.<P>
   </BLOCKQUOTE>

A GUID is generated by pairing a UID with the host machine's InetAddress.

<P>
   <DL>
      <DT>
         <B>
            Example usage:
         </B>
         <DD>
            <BLOCKQUOTE>
               <PRE id="unindent">
                  =========================================================================================
                  generate 10 globally unique ids
                  -----------------------------------------------------------------------------------------
                  for (int i=0; i<10; i++)
                     {
                     GUID id = new GUID();
                     System.out.println(id);
                     }
               </PRE>
            </BLOCKQUOTE>
         </DD>
      </DT>
      <DT>
         <B>
            View Source:
         </B>
         <DD>
            <A href="GUID.java.html">
               GUID.java
            </A>
         </DD>
      </DT>
      <DT>
         <B>
            Author:
         </B>
         <DD>
            <A href="mailto:sourcecode.v01@cosmicabyss.com">
               Allen Baker
            </A>
         </DD>
      </DT>
   </DL>
*//*
======================================================================================================================== */
public class GUID
   {



   /*:                                    :METHOD:000:BOOKMARK:
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method creates a pure identifier that is guaranteed to be unique in the world under the
   following conditions:

      <LI> the machine on which it is generated takes more than one second to reboot, </LI>

      <LI> the machine's clock is never set backward, and </LI>

      <LI> the machine does not have the same Internet address as any other machine in the world that
      generates a GUID at the exact same internal clock time as this machine. </LI>

   A G1UID is generated by pairing a UID with the host machine's InetAddress.

   <P><B>Implementation: </B><A HREF="GUID.java.html#000">View source</A>

   *//*
   ---------------------------------------------------------------------------------------------------- */
   public GUID() throws Exception
      {
      InetAddress  ip  = InetAddress.getLocalHost();
      UID          uid = new UID();

      iId = new XString(ip.getHostAddress() + uid.toString());
      ArrayList<XString> a = iId.tokenizedString(".:-");
      iId = new XString("");
      for (XString str : a)
         {
         iId = iId.concat(str);
         }
      }



   /*:                                    :METHOD:001:BOOKMARK:
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method returns the String representation of the GUID.

   <P><B>Implementation: </B><A HREF="GUID.java.html#001">View source</A>

   @return
      The String representation of this GUID.
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public String string()
      {
      return iId.string();
      }



   /*:                                    :METHOD:002:BOOKMARK:
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method returns the String representation of the GUID. It is equivalent to the String() method
   and is here to override the toString() method defined by the Object class for use in String
   expressions.

   <P><B>Implementation: </B><A HREF="GUID.java.html#002">View source</A>

   @return
      The String representation of this GUID.
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public String toString()
      {
      return this.string();
      }



   /*:                                    :METHOD:003:BOOKMARK:
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method returns the xstring representation of the GUID.

   <P><B>Implementation: </B><A HREF="GUID.java.html#003">View source</A>

   @return
      The XString representation of this GUID.
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public XString xstring()
      {
      return iId;
      }



   /*:                                    :METHOD:004:BOOKMARK:
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method sets the ConsoleStream for this object.

   <P><B>Implementation: </B><A HREF="GUID.java.html#004">View source</A>

   @return
      A reference to this object

   @param
      pConsole is a ConsoleStream through which this objects sends its output.
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public GUID setOut(ConsoleStream pConsole)
      {
      cOut = pConsole;
      return this;
      }



   /*:                                    :METHOD:005:BOOKMARK:
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This boilerplate method is used for testing an instantiated object of this class and may include any
   code the developer chooses.

   <P><B>Implementation: </B><A HREF="GUID.java.html#005">View source</A>

   *//*
   ---------------------------------------------------------------------------------------------------- */
   public GUID test() throws Exception
      {
      cOut.titledPrintf
         (
         "\"HELLO WORLD!\"",
         "%s  %s  %s",
         "I'm an object of the", CLASS_NAME, "class, and I approved this message."
         );
      return this;
      }



   /*:.
   ==============================================================================================================
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[  Protected  ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   ============================================================================================================== */



   /*:.
   ==============================================================================================================
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[  Private  ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   ============================================================================================================== */



   /*.
   ==========================================================================================
   Class Constants
      CLASS_NAME : the name of this class DFLT_LINE_LEN : the default line length for word
      wrapping
   ------------------------------------------------------------------------------------------ */
   private static final XString  CLASS_NAME    = new XString(GUID.class.getName());
   private static final int      DFLT_LINE_LEN = ConsoleMessage.defaultLineLength();
   /*.
   ==========================================================================================
   Class variables
      cOut : console output.
   ------------------------------------------------------------------------------------------ */
   private static ConsoleStream  cOut = ConsoleStream.getSingleton();
   /*.
   ==========================================================================================
   Instance variables
      iId : where the generated ID is stored
   ------------------------------------------------------------------------------------------ */
   private XString  iId;



   /*:.
   ==============================================================================================================
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[  Inner Classes  ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   ============================================================================================================== */



   /*:.
   ==============================================================================================================
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[  Public Static Methods  ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   ============================================================================================================== */



   /*:                                    :METHOD:006:BOOKMARK:
   ====================================================================================================
   [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
   ==================================================================================================== *//**
   This method allows this class file to be unit tested as a standalone application. It's the method
   that's called when the class is invoked from the command line by using the java application launcher
   - "java". Main() is not a required method, but the practice of putting one in each class and
   wrapping class test code within it allows easy unit testing of the class; and main does not need to
   be removed when testing is complete.

   <P>
      <DL>
         <DT>
            <B>
               Command line usage:
            </B>
            <DD>
               Java cosmicabyss.com.lib.GUID
            </DD>
         </DT>
      </DL>

   <P><B>Implementation: </B><A HREF="GUID.java.html#006">View source</A>

   @param
      pArgs contains the command line arguments with which this class was invoked as an application.
   *//*
   ---------------------------------------------------------------------------------------------------- */
   public static void main(String[] pArgs) throws Exception
      {
      /*.
      ==========================================================================================
      Greetings !
      ------------------------------------------------------------------------------------------ */
      cOut.banner(CLASS_NAME);
      /*.
      ==========================================================================================
      Create an object and send its output to the ConsoleStream
      ------------------------------------------------------------------------------------------ */
      GUID  obj = new GUID();
      /*.
      ==========================================================================================
      Test code
      ------------------------------------------------------------------------------------------ */
      obj.test();
      /*.
      ==========================================================================================
      ------------------------------------------------------------------------------------------ */
      for (int i=0; i<10; i++)
         {
         GUID id = new GUID();
         cOut.println(id);
         }
      }



   }  // class GUID



   /*:.
   ==============================================================================================================
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[  Notes  ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   ============================================================================================================== */