You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2003/12/30 08:29:18 UTC

cvs commit: jakarta-commons-sandbox/uid/src/test/org/apache/commons/uid IdentifierUtilsTest.java

psteitz     2003/12/29 23:29:18

  Added:       uid/src/java/org/apache/commons/uid
                        AbstractLongIdentifierGenerator.java
                        AbstractStringIdentifierGenerator.java
                        DefaultIdentifierGeneratorFactory.java
                        IdentifierGenerator.java
                        IdentifierGeneratorFactory.java
                        IdentifierUtils.java LongIdentifierGenerator.java
                        StringIdentifierGenerator.java package.html
               uid/src/java/org/apache/commons/uid/random
                        SessionIdGenerator.java package.html
               uid/src/java/org/apache/commons/uid/serial
                        AlphanumericGenerator.java LongGenerator.java
                        NumericGenerator.java package.html
               uid/src/java/org/apache/commons/uid/uuid package.html
               uid/src/test/org/apache/commons/uid IdentifierUtilsTest.java
  Log:
  Added identifier generators from [lang], initial commit of factory classes.
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/AbstractLongIdentifierGenerator.java
  
  Index: AbstractLongIdentifierGenerator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/AbstractLongIdentifierGenerator.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.uid;
  
  /**
   * <p>Abstract superclass for LongIdentifierGenerator implementations.</p>
   *
   * @author Commons-Uid team
   * @version $Id: AbstractLongIdentifierGenerator.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   */
  public abstract class AbstractLongIdentifierGenerator implements LongIdentifierGenerator {
  
      /**
       * Constructor
       */
      protected AbstractLongIdentifierGenerator() {
          super();
      }
  
      /**
       * <p> Returns the maximum value for an identifier from this generator.</p>
       *
       * <p>The default implementation returns Long.MAX_VALUE. Implementations
       * whose identifiers are bounded below Long.MAX_VALUE should override this method to
       * return the maximum value of a generated identifier.</p>
       *
       * @return the maximum identifier value
       */
      public long maxValue() {
          return Long.MAX_VALUE;
      }
  
      /**
       * <p> Returns the minumum value for an identifier from this generator.</p>
       *
       * <p>The default implementation returns Long.MIN_VALUE. Implementations
       * whose identifiers are bounded above Long.MIN_VALUE should override this method to
       * return the minimum value of a generated identifier.</p>
       *
       * @return the minimum identifier value
       */
      public long minValue() {
          return Long.MIN_VALUE;
      }
  
      /**
       * <p>Gets the next identifier in the sequence as an Object.</p>
       *
       * @return the next String identifier in sequence
       */
      public Object nextIdentifier() {
          return this.nextLongIdentifier();
      }
  
      /**
       * <p>Gets the next identifier in the sequence.</p>
       *
       * @return the next Long identifier in sequence
       */
      public abstract Long nextLongIdentifier();
  }
  
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/AbstractStringIdentifierGenerator.java
  
  Index: AbstractStringIdentifierGenerator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/AbstractStringIdentifierGenerator.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.uid;
  
  /**
   * <p>Abstract superclass for StringIdentifierGenerator implementations.</p>
   *
   * @author Commons-Uid team
   * @version $Id: AbstractStringIdentifierGenerator.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   */
  public abstract class AbstractStringIdentifierGenerator implements StringIdentifierGenerator {
  
      /**
       * Constant representing unlimited identifier length, returned by {@link #maxLength()}
       * when there is no upper bound to the length of an identifier in the sequence
       */
      public static final int INFINITE_MAX_LENGTH = -1;
  
      /**
       * Maximum length for a numeric string representing a long value
       */
      protected static final int MAX_LONG_NUMERIC_VALUE_LENGTH =
      Long.toString(Long.MIN_VALUE).length();
  
      /**
       * Number of alphanumeric characters
       */
      protected static final int ALPHA_NUMERIC_CHARSET_SIZE = 36;
  
      /**
       * Maximum length for an alphanumeric string representing a long value
       */
      protected static final int MAX_LONG_ALPHANUMERIC_VALUE_LENGTH =
      Long.toString(Long.MAX_VALUE, ALPHA_NUMERIC_CHARSET_SIZE).length();
  
      /**
       * Maximum length for a numeric string representing an integer value
       */
      protected static final int MAX_INT_NUMERIC_VALUE_LENGTH =
      Integer.toString(Integer.MIN_VALUE).length();
  
      /**
       * Maximum length for an alphanumeric string representing an integer value
       */
      protected static final int MAX_INT_ALPHANUMERIC_VALUE_LENGTH =
      Integer.toString(Integer.MAX_VALUE, ALPHA_NUMERIC_CHARSET_SIZE).length();
  
      /**
       * Default size of an alphanumeric identifier
       */
      protected static final int DEFAULT_ALPHANUMERIC_IDENTIFIER_SIZE = 15;
  
      /**
       * Constructor
       */
      protected AbstractStringIdentifierGenerator() {
          super();
      }
  
      /**
       * <p>Gets the next identifier in the sequence.</p>
       *
       * @return the next String identifier in sequence
       */
      public abstract String nextStringIdentifier();
  
      /**
       * <p>Returns the maximum length (number or characters) for an identifier
       * from this sequence, or INFINITE_MAX_LENGTH if there is no upper bound to the length.</p>
       *
       * <p>The default implementation returns INFINITE_MAX_LENGTH. Implementations
       * with bounded length identifiers should override this method to
       * return the maximum length of a generated identifier.</p>
       *
       * @return the maximum identifier length, or INFINITE_MAX_LENGTH if there is no upper bound
       */
      public long maxLength() {
          return INFINITE_MAX_LENGTH;
      }
  
      /**
       * <p>Returns the minimum length (number of characters) for an identifier
       * from this sequence.</p>
       *
       * <p>The default implementation returns 0. Implementations
       * with identifiers having a postive minimum length should override this
       * method to return the maximum length of a generated identifier.</p>
       *
       * @return the minimum identifier length
       */
      public long minLength() {
          return 0;
      }
  
      /**
       * <p>Gets the next identifier in the sequence as an Object.</p>
       *
       * @return the next identifier in sequence
       */
      public Object nextIdentifier() {
          return nextStringIdentifier();
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/DefaultIdentifierGeneratorFactory.java
  
  Index: DefaultIdentifierGeneratorFactory.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/DefaultIdentifierGeneratorFactory.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.uid;
  
  import java.io.Serializable;
  
  import org.apache.commons.uid.serial.AlphanumericGenerator;
  import org.apache.commons.uid.serial.LongGenerator;
  import org.apache.commons.uid.serial.NumericGenerator;
  import org.apache.commons.uid.random.SessionIdGenerator;
  
  /**
   * A concrete identifier sequence factory.  This is the default factory used by
   * commons-uid.
   *
   * @author Commons-Uid team
   * @version $id$
   */
  public class DefaultIdentifierGeneratorFactory extends IdentifierGeneratorFactory implements Serializable  {
      /**
       * Package scope constructor.
       */
      public DefaultIdentifierGeneratorFactory() {
          super();
      }
  
      /**
       * <p>Gets a new {@link LongIdentifierGenerator} that generates
       * a sequence of Long objects increasing in value.</p>
       *
       * <p>The sequence will wrap when the maximum <code>long</code> value is
       * reached and return negative numbers. It will start from zero.</p>
       *
       * @return a new LongIdentifier with wrapping behavior
       */
      public LongIdentifierGenerator longGenerator() {
          return new LongGenerator(true, 0L);
      }
  
      /**
       * <p>Gets a new {@link LongIdentifierGenerator} that generates
       * a sequence of Long objects increasing in value.</p>
       *
       * @param wrap should the sequence wrap when it reaches the maximum
       *  long value (or throw an {@link IllegalStateException})
       * @param initialValue  the initial long value to start at
       * @return a new LongIdentifier with the specified properties
       */
      public LongIdentifierGenerator longGenerator(boolean wrap, long initialValue) {
          return new LongGenerator(wrap, initialValue);
      }
  
      /**
       * <p>Gets a new {@link StringIdentifierGenerator} that generates a sequence of
       * String objects representing numbers increasing in size in base-36.</p>
       *
       * <p>The sequence will wrap when the maximum size (15) is reached.</p>
       *
       * @return a new StringIdentifierGenerator
       */
      public StringIdentifierGenerator alphanumericGenerator() {
          return new AlphanumericGenerator
              (true, AbstractStringIdentifierGenerator.DEFAULT_ALPHANUMERIC_IDENTIFIER_SIZE);
      }
  
      /**
       * <p>Gets a new {@link StringIdentifierGenerator} that generates a sequence
       * of String objects representing numbers increasing in size in base-36.</p>
       *
       * @param wrap should the factory wrap when it reaches the maximum
       *  size (or throw an IllegalStateException)
       * @param size  the number of characters the id should fill
       *
       * @return a new StringIdentifierGenerator
       */
      public StringIdentifierGenerator alphanumericGenerator(boolean wrap, int size) {
          return new AlphanumericGenerator(wrap, size);
      }
  
      /**
       * <p>Gets a new {@link StringIdentifierGenerator}
       * that generates a sequence of String objects representing numbers increasing in size.</p>
       *
       * <p>The sequence will wrap when the maximum <code>long</code> value is
       * reached and return negative numbers. It will start from zero.</p>
       *
       * @return a new StringIdentifierGenerator
       */
      public StringIdentifierGenerator numericGenerator() {
          return new NumericGenerator(true, 0L);
      }
  
      /**
       * <p>Gets a new {@link StringIdentifierGenerator} that generates a sequence
       * of String objects representing numbers increasing in size.</p>
       *
       * @param wrap should the sequence wrap when it reaches the maximum
       *  long value (or throw an IllegalStateException)
       * @param initialValue  the initial long value to start at
       * @return a new StringIdentifierGenerator
       */
      public StringIdentifierGenerator numericGenerator(boolean wrap, long initialValue) {
          return new NumericGenerator(wrap, initialValue);
      }
  
      /**
       * <p>Gets a new {@link StringIdentifierGenerator} that generates a sequence
       * of String objects that appear to be random and are suitable for use as
       * session identifiers.</p>
       *
       *
       * @return a new StringIdentifierGenerator
       */
      public StringIdentifierGenerator sessionIdGenerator() {
          return new SessionIdGenerator();
      }
  
      /**
       * <p>Gets a new {@link StringIdentifierGenerator} that generates a sequence
       * of String objects that conform to the
       * <a href="http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-01.txt">
       * IETF Draft UUID specification</a>.</p>
       *
       * <p>NOT IMPLEMENTED YET -- throws UnsupportedOperationExcption</p>
       *
       * @return a new StringIdentifierGenerator
       */
      public StringIdentifierGenerator uuidGenerator() {
          // not implemented yet
          throw new UnsupportedOperationException();
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/IdentifierGenerator.java
  
  Index: IdentifierGenerator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/IdentifierGenerator.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.uid;
  
  /**
   * <p><code>IdentifierGenerator</code> defines a simple interface for
   * identifier generation.</p>
   *
   * @author Commons-Uid team
   * @version $Id: IdentifierGenerator.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   */
  public interface IdentifierGenerator {
  
      /**
       * <p>Gets the next identifier in the sequence.</p>
       *
       * @return the next identifier in sequence
       */
      Object nextIdentifier();
  }
  
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/IdentifierGeneratorFactory.java
  
  Index: IdentifierGeneratorFactory.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/IdentifierGeneratorFactory.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.uid;
  
  import org.apache.commons.discovery.tools.DiscoverClass;
  
  /**
   * <p>Abstract factory for identifier instances.</p>
   *
   * <p>Use newInstance() to get a concrete factory instance. </p>
   *
   * <p>Commons Discovery is used to select the concrete factory.  The default
   * factory is {@link DefaultIdentifierGeneratorFactory}, which can be subclassed
   * (or replaced) to plug in alternative identifier implementations.  See
   * <a href="http://jakarta.apache.org/commons/discovery/">Commons Discovery
   * Documentation</a> for instructions on how to set up discovery for user-supplied
   * factories.</p>
   *
   * @author Commons-Uid team
   *
   * @version $Id: IdentifierGeneratorFactory.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   */
  public abstract class IdentifierGeneratorFactory {
      /**
       * Default constructor.
       */
      protected IdentifierGeneratorFactory() {
          super();
      }
  
      /**
       * Create an instance of an <code>IdentifierGeneratorFactory</code>.
       *
       * @return a new factory
       */
      public static IdentifierGeneratorFactory newInstance() {
          IdentifierGeneratorFactory factory = null;
          try {
              DiscoverClass dc = new DiscoverClass();
              factory = (IdentifierGeneratorFactory) dc.newInstance(
              IdentifierGeneratorFactory.class,
              "org.apache.commons.uid.DefaultIdentifierGeneratorFactory");
          } catch (Exception ex) {
              // ignore as default implementation will be used.
          }
          return factory;
      }
  
      /**
       * <p>Gets a new {@link LongIdentifierGenerator} that generates
       * a sequence of Long objects increasing in value.</p>
       *
       * <p>The sequence will wrap when the maximum <code>long</code> value is
       * reached and return negative numbers. It will start from zero.</p>
       *
       * @return a new LongIdentifier with wrapping behavior
       */
      public abstract LongIdentifierGenerator longGenerator();
  
      /**
       * <p>Gets a new {@link LongIdentifierGenerator} that generates
       * a sequence of Long objects increasing in value.</p>
       *
       * @param wrap should the sequence wrap when it reaches the maximum
       *  long value (or throw an {@link IllegalStateException})
       * @param initialValue  the initial long value to start at
       * @return a new LongIdentifier with the specified properties
       */
      public abstract LongIdentifierGenerator longGenerator(boolean wrap, long initialValue);
  
      /**
       * <p>Gets a new {@link StringIdentifierGenerator}
       * that generates a sequence of String objects representing numbers increasing in size.</p>
       *
       * <p>The sequence will wrap when the maximum <code>long</code> value is
       * reached and return negative numbers. It will start from zero.</p>
       *
       * @return a new StringIdentifierGenerator
       */
      public abstract StringIdentifierGenerator numericGenerator();
  
      /**
       * <p>Gets a new {@link StringIdentifierGenerator} that generates a sequence
       * of String objects representing numbers increasing in size.</p>
       *
       * @param wrap should the sequence wrap when it reaches the maximum
       *  long value (or throw an IllegalStateException)
       * @param initialValue  the initial long value to start at
       * @return a new StringIdentifierGenerator
       */
      public abstract StringIdentifierGenerator numericGenerator(boolean wrap, long initialValue);
  
      /**
       * <p>Gets a new {@link StringIdentifierGenerator} that generates a sequence of String objects
       * representing numbers increasing in size in base-36.</p>
       *
       * <p>The sequence will wrap when the maximum size (15) is reached.</p>
       *
       * @return a new StringIdentifierGenerator
       */
      public abstract StringIdentifierGenerator alphanumericGenerator();
  
      /**
       * <p>Gets a new {@link StringIdentifierGenerator} that generates a sequence of String objects
       * representing numbers increasing in size in base-36.</p>
       *
       * @param wrap should the factory wrap when it reaches the maximum
       *  size (or throw an IllegalStateException)
       * @param size  the number of characters the id should fill
       *
       * @return a new StringIdentifierGenerator
       */
      public abstract StringIdentifierGenerator alphanumericGenerator(boolean wrap, int size);
  
      /**
       * <p>Gets a new {@link StringIdentifierGenerator} that generates a sequence of String objects
       * that appear to be random and are suitable for use as session identifiers.</p>
       *
       *
       * @return a new StringIdentifierGenerator
       */
      public abstract StringIdentifierGenerator sessionIdGenerator();
  
      /**
       * <p>Gets a new {@link StringIdentifierGenerator} that generates a sequence of String objects
       * that conform to the <a href="http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-01.txt">
       * IETF Draft UUID specification</a>.</p>
       *
       * <p>NOT IMPLEMENTED YET -- throws UnsupportedOperationExcption</p>
       *
       * @return a new StringIdentifierGenerator
       */
      public abstract StringIdentifierGenerator uuidGenerator();
  }
  
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/IdentifierUtils.java
  
  Index: IdentifierUtils.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/IdentifierUtils.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.uid;
  
  /**
   * <p><code>IdentifierUtils</code> provides convenience methods for generating
   * identifiers using the identifier generators provided by commons-uid.</p>
   *
   * <p>This class maintains a static instance of each generator and uses this instance
   * to generate identifiers in response to its corresponding nextXxx() method.</p>
   *
   * @author Commons-Uid team
   * @version $Id: IdentifierUtils.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   */
  public class IdentifierUtils {
  
      /**
       * Static {@link IdentifierGeneratorFactory} used to create generator instances
       */
      private static IdentifierGeneratorFactory factory =
      IdentifierGeneratorFactory.newInstance();
  
      /**
       * <p>Singleton instance of the
       * <code>LongIdentifierFactory</code>.</p>
       *
       * <p>The singleton instance will wrap, so in a long-lived server, the id
       * may be duplicated.</p>
       *
       * <p>The objects returned are:</p>
       * <ul>
       * <li>new Long(0L)</li>
       * <li>new Long(1L)</li>
       * <li>new Long(2L)</li>
       * <li>...</li>
       * </ul>
       */
      public static final LongIdentifierGenerator LONG_IDENTIFIER_GENERATOR =
      factory.longGenerator();
      /**
       * <p>Singleton instance of the <code>StringNumericIdentifierFactory</code>.
       * </p>
       *
       * <p>The singleton instance will wrap, so in a long-lived server, the id
       * may be duplicated.</p>
       *
       * <p>The objects returned are:</p>
       * <ul>
       * <li>"0"</li>
       * <li>"1"</li>
       * <li>"2"</li>
       * <li>...</li>
       * </ul>
       */
  
      public static final StringIdentifierGenerator STRING_NUMERIC_IDENTIFIER_GENERATOR =
      factory.numericGenerator();
      /**
       * <p>Singleton instance of the
       * <code>StringAlphanumericIdentifierFactory</code>.</p>
       *
       * <p>The singleton instance will wrap, so in a long-lived server, the id
       * may be duplicated. However, the length is 15 in base-36, so thats a
       * lot of identifiers.</p>
       *
       * <p>The objects returned are:</p>
       * <ul>
       * <li>"000000000000001"</li>
       * <li>"000000000000002"</li>
       * <li>"000000000000003"</li>
       * <li>...
       * <li>"00000000000000y"</li>
       * <li>"00000000000000z"</li>
       * <li>"000000000000010"</li>
       * <li>"000000000000011"</li>
       * <li>...
       * <li>"00000000000001z"</li>
       * <li>"000000000000020"</li>
       * <li>...</li>
       * </ul>
       */
  
      public static final StringIdentifierGenerator STRING_ALPHANUMERIC_IDENTIFIER_GENERATOR =
      factory.alphanumericGenerator();
      /**
       * <p>Singleton instance of the
       * <code>StringSessionIdentifierFactory</code>.</p>
       *
       * <p>The singleton instance may produce duplicates in a long-lived server,
       * but its unlikely.</p>
       *
       * <p>The objects returned are 10 or more base-36 digits.</p>
       */
  
      public static final StringIdentifierGenerator STRING_SESSION_IDENTIFIER_GENERATOR =
      factory.sessionIdGenerator();
  
      //---------------------------------------------------------------------------------
  
      /**
       * <p>Constructor.</p>
       *
       */
      protected IdentifierUtils() {
          super();
      }
  
      //---------------------------------------------------------------------------------
  
      /**
       * <p>Gets the next identifier using the singleton instance of the
       * Long factory.</p>
       *
       * <p>The singleton instance will wrap, so in a long-lived server, the id
       * may be duplicated.</p>
       *
       * @return a new identifier
       */
      public static Long nextLongIdentifier() {
          return LONG_IDENTIFIER_GENERATOR.nextLongIdentifier();
      }
  
      //---------------------------------------------------------------------------------
  
      /**
       * <p>Gets the next identifier using the singleton instance of the
       * String Alphanumeric factory.</p>
       *
       * <p>The singleton instance will wrap, so in a long-lived server, the id
       * may be duplicated.</p>
       *
       * @return a new identifier
       */
      public static String nextStringAlphanumericIdentifier() {
          return STRING_ALPHANUMERIC_IDENTIFIER_GENERATOR.nextStringIdentifier();
      }
  
  
      //---------------------------------------------------------------------------------
  
      /**
       * <p>Gets the next identifier using the singleton instance of the
       * String Session factory.</p>
       *
       * <p>The generation routine is based on a random number and a counter
       * within a 2 second time interval.</p>
       *
       * @return a new identifier
       */
      public static String nextStringSessionIdentifier() {
          return STRING_SESSION_IDENTIFIER_GENERATOR.nextStringIdentifier();
      }
  
      /**
       * <p>Gets the next identifier using the singleton instance of the
       * String Numeric factory.</p>
       *
       * <p>The singleton instance will wrap, so in a long-lived server, the id
       * may be duplicated.</p>
       *
       * @return a new identifier
       */
      public static String nextStringNumericIdentifier() {
          return STRING_NUMERIC_IDENTIFIER_GENERATOR.nextStringIdentifier();
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/LongIdentifierGenerator.java
  
  Index: LongIdentifierGenerator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/LongIdentifierGenerator.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.uid;
  
  /**
   * <p><code>LongIdentifier</code> defines a simple interface for
   * Long based identifier generation.</p>
   *
   * @author Commons-Uid team
   * @version $Id: LongIdentifierGenerator.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   */
  public interface LongIdentifierGenerator extends IdentifierGenerator {
  
      /**
       * <p>Gets the next identifier in the sequence.</p>
       *
       * @return the next Long identifier in sequence
       */
      Long nextLongIdentifier();
  
      /**
       * <p>Returns the maximum value of an identifier from this generator.</p>
       *
       * @return the maximum identifier value
       */
      long maxValue();
  
      /**
       * <p>Returns the minimum value of an identifier from this generator.</p>
       *
       * @return the minimum identifier value
       */
      long minValue();
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/StringIdentifierGenerator.java
  
  Index: StringIdentifierGenerator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/StringIdentifierGenerator.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.uid;
  
  /**
   * <p><code>StringIdentifierGenerator</code> defines a simple interface for
   * String based identifier generation.</p>
   *
   * @author Commons-Uid team
   * @since 2.0
   * @version $Id: StringIdentifierGenerator.java,v 1.1 2003/12/30 07:29:17 psteitz Exp $
   */
  public interface StringIdentifierGenerator extends IdentifierGenerator {
  
      /**
       * <p>Gets the next identifier in the sequence.</p>
       *
       * @return the next String identifier in sequence
       */
      String nextStringIdentifier();
  
      /**
       * <p>Returns the maximum length (number or characters) for an identifier
       * from this sequence, or INFINITE_MAX_LENGTH if there is no upper bound to the length.</p>
       *
       * <p>The default implementation returns INFINITE_MAX_LENGTH. Implementations
       * with bounded length identifiers should override this method to
       * return the maximum length of a generated identifier.</p>
       *
       * @return the maximum identifier length, or INFINITE_MAX_LENGTH if there is no upper bound
       */
      long maxLength();
  
      /**
       * <p>Returns the minimum length (number of characters) for an identifier
       * from this sequence.</p>
       *
       *<p>The default implementation returns 0. Implementations
       * with bounded length identifiers should override this method to
       * return the maximum length of a generated identifier.</p>
       *
       * @return the minimum identifier length
       */
      long minLength();
  }
  
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/package.html
  
  Index: package.html
  ===================================================================
  <body> 
    <p>
       This package contains factories and implementation classes to generate unique
       identifiers.</p>
    <p>
       The implementation is divided into the following subpackages.
       <ul>
       <li>The main package contains uid interface definitions, abstract base
       classes for identifier generators, generator factories and an
       IdentifierUtils convenience class that maintains singleton generators and 
       exposes static methods for generating identifiers of the various kinds.</li>
       <li>The <code>random</code> subpackage contains implementations of
       generators that return identifiers that appear to be random.</li>
  
       <li>The <code>serial</code> subpackage contains generators that yield
       sequences of identifiers that follow a regular sequence (e.g., increasing
       numeric values).</li>
       <li>The <code>uuid</code> subpackage contains implementations of the 
       <a href="http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-01.txt">
       IETF Draft UUID specification</a></li>
       </ul>
     </p>
  </body>
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/random/SessionIdGenerator.java
  
  Index: SessionIdGenerator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/random/SessionIdGenerator.java,v 1.1 2003/12/30 07:29:18 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.uid.random;
  
  import org.apache.commons.uid.AbstractStringIdentifierGenerator;
  
  import java.util.Random;
  
  /**
   * <p><code>SessionIdGenerator</code> is an identifier generator
   * that generates an alphanumeric 10+ character identifier. The
   * exact length depends on the number of ids requested per time period.</p>
   *
   * <p>Originally designed for JServ sessions. Uses synchronized count and
   * time to ensure uniqueness. Not guaranteed unique across JVMs, but
   * fairly safe nonetheless.</p>
  
   * @author Commons-uid team
   * @version $Id: SessionIdGenerator.java,v 1.1 2003/12/30 07:29:18 psteitz Exp $
   */
  public class SessionIdGenerator extends AbstractStringIdentifierGenerator {
  
      /**
       * We want to have a random string with a length of 6 characters.
       * Since we encode it base-36, we modulo the random number with
       * this value.
       */
      private static final long MAX_RANDOM_LEN = 2176782336L; // 36 ** 6
      /**
       * <p>The identifier must be unique within the typical lifespan of a
       * session; the value can roll over after that.</p>3 characters:
       * (this means a roll over after over a day, which is much larger
       * than a typical lifespan).
       */
      private static final long MAX_TIME_SECTION_LEN = 46656L; // 36 ** 3
      /**
       * Milliseconds between different tics.  The 3-character time
       * string has a new value every 2 seconds.
       */
      private static final long TIC_DIFFERENCE = 2000;
      /**
       * Length of random segment
       */
      private static final int RANDOM_LENGTH = 6;
      /**
       * Length of time segment
       */
      private static final int TIME_LENGTH = 3;
  
      /** The incrementing counter. */
      private int counter = 0;
      /** The last time. */
      private long lastTimeValue = 0;
      /** The randmonizer. */
      private Random randomizer = new Random();
  
      /**
       * <p>Constructor.</p>
       */
      public SessionIdGenerator() {
          super();
      }
  
      /**
       * <p>Returns the maximum length (number or characters) for an identifier
       * from this generator.</p>
       *
       * @return the maximum identifier length
       */
      public long maxLength() {
          return RANDOM_LENGTH + TIME_LENGTH
              + AbstractStringIdentifierGenerator.MAX_INT_ALPHANUMERIC_VALUE_LENGTH;
      }
  
      /**
       * <p>Returns the minimum length (number of characters) for an identifier
       * from this generator.</p>
       *
       * @return the minimum identifier length
       */
      public long minLength() {
          return RANDOM_LENGTH + TIME_LENGTH + 1;
      }
  
      /**
       * <p>Gets the next new identifier.</p>
       *
       * <p>Only guaranteed unique within this JVM, but fairly safe
       * for cross JVM usage as well.</p>
       *
       * <p>Format of identifier is
       * <code>[6 chars random][3 chars time][1+ chars count]</code></p>
       *
       * @return the next 10 char String identifier
       */
      public String nextStringIdentifier() {
  
          // Random value
          //--------------
          long currentRandom = randomizer.nextLong();
          if (currentRandom < 0) {
              currentRandom = -currentRandom;
          }
          // force value into 6 char range, and add to pad with zeros
          // this gives a length of 7, when converted to base 36, and
          // the first character (always 1 from the add) is dropped
          currentRandom %= MAX_RANDOM_LEN;
          currentRandom += MAX_RANDOM_LEN;
  
          long currentTimeValue = 0;
          int currentCount = 0;
  
          synchronized (this) {
              // Time
              //--------------
              currentTimeValue = (System.currentTimeMillis() / TIC_DIFFERENCE);
  
              // force value into 3 char range, and add to pad with zeros
              // this gives a length of 4, when converted to base 36, and
              // the first character (always 1 from the add) is dropped
              currentTimeValue %= MAX_TIME_SECTION_LEN;
              currentTimeValue += MAX_TIME_SECTION_LEN;
  
              // Count
              //--------------
              // Make the string unique by appending the count since last
              // time flip.
  
              // Count sessions only within tics (so the 'real' counter
              // isn't exposed to the public).
              if (lastTimeValue != currentTimeValue) {
                  lastTimeValue = currentTimeValue;
                  counter = 0;
              }
              currentCount = counter++;
          }
  
          // build string
          //--------------
          StringBuffer id = new StringBuffer
              (AbstractStringIdentifierGenerator.DEFAULT_ALPHANUMERIC_IDENTIFIER_SIZE);
          id.append(Long.toString(currentRandom,
              AbstractStringIdentifierGenerator.ALPHA_NUMERIC_CHARSET_SIZE).substring(1));  // 6 chars
          id.append(Long.toString(currentTimeValue,
              AbstractStringIdentifierGenerator.ALPHA_NUMERIC_CHARSET_SIZE).substring(1));  // 3 chars
          id.append(Long.toString(currentCount,
              AbstractStringIdentifierGenerator.ALPHA_NUMERIC_CHARSET_SIZE));  // 1+ chars
          return id.toString();
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/random/package.html
  
  Index: package.html
  ===================================================================
  <body>
    <p>
      This package contains identifier generators that return sequences of
      identifiers that appear to be random.
    </p>
  </body>
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/serial/AlphanumericGenerator.java
  
  Index: AlphanumericGenerator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/serial/AlphanumericGenerator.java,v 1.1 2003/12/30 07:29:18 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2004 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.uid.serial;
  
  import org.apache.commons.uid.AbstractStringIdentifierGenerator;
  
  /**
   * <p><code>AlphanumericGenerator</code> is an Identifier Generator
   * that generates an incrementing number in base 36 as a String
   * object.</p>
   *
   * <p>All generated ids have the same length (padding with 0's on the left),
   * which is determined by the <code>size</code> parameter passed to the constructor.<p>
   *
   * <p>The <code>wrap</code> property determines whether or not the sequence wraps
   * when it reaches the largest value that can be represented in <code>size</code>
   * base 36 digits. If <code>wrap</code> is false and the the maximum representable
   * value is exceeded, an IllegalStateException is thrown</p>
   *
   * @author Commons-Uid team
   * @version $Id: AlphanumericGenerator.java,v 1.1 2003/12/30 07:29:18 psteitz Exp $
   */
  public class AlphanumericGenerator extends AbstractStringIdentifierGenerator {
  
      /**
       * Should the counter wrap.
       */
      private boolean wrap = true;
  
      /**
       * The counter.
       */
      private char[] count = null;
  
      /**
       * 'z' char
       */
      private static final char Z_CHAR = 'z';
  
      /**
       * '9' char
       */
      private static final char NINE_CHAR = '9';
  
      /**
       * <p>Constructor.</p>
       *
       * @param wrap should the factory wrap when it reaches the maximum
       *  long value (or throw an exception)
       * @param size  the size of the identifier
       */
      public AlphanumericGenerator(boolean wrap, int size) {
          super();
          this.wrap = wrap;
          if (size < 1) {
              throw new IllegalArgumentException("The size must be at least one");
          }
          this.count = new char[size];
          for (int i = 0; i < size; i++) {
              count[i] = '0';  // zero
          }
      }
  
      /**
       * <p>Returns the maximum length (number or characters) for an identifier
       * from this sequence.</p>
       *
       * @return the maximum identifier length
       */
      public long maxLength() {
          return this.count.length;
      }
  
      /**
       * <p>Returns the minimum length (number of characters) for an identifier
       * from this sequence.</p>
       *
       * @return the minimum identifier length
       */
      public long minLength() {
          return this.count.length;
      }
  
      /**
       * Getter for property wrap.
       *
       * @return <code>true</code> iff this generator is set up to wrap.
       *
       */
      public boolean isWrap() {
          return wrap;
      }
  
      /**
       * Sets the wrap property.
       *
       * @param wrap value for the wrap property
       *
       */
      public void setWrap(boolean wrap) {
          this.wrap = wrap;
      }
  
      /**
       * Returns the (constant) size of the strings generated by this generator.
       *
       * @return the size of generated identifiers
       */
      public int getSize() {
          return this.count.length;
      }
  
      /**
       * <p>Gets the next new identifier.</p>
       *
       * @return a new identifier as a String
       */
      public synchronized String nextStringIdentifier() {
          for (int i = count.length - 1; i >= 0; i--) {
              switch (count[i]) {
                  case Z_CHAR:  // z
                      count[i] = '0';
                      if (i == 0 && !wrap) {
                          throw new IllegalStateException
                          ("The maximum number of identifiers has been reached");
                      }
                      break;
  
                  case NINE_CHAR:  // 9
                      count[i] = 'a';
                      i = -1;
                      break;
  
                  default:
                      count[i]++;
                      i = -1;
                      break;
              }
          }
          return new String(count);
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/serial/LongGenerator.java
  
  Index: LongGenerator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/serial/LongGenerator.java,v 1.1 2003/12/30 07:29:18 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.uid.serial;
  
  import org.apache.commons.uid.AbstractLongIdentifierGenerator;
  
  /**
   * <p><code>LongGenerator</code> is an Identifier Generator
   * that generates an incrementing number as a Long object.</p>
   *
   * <p>If the <code>wrap</code> argument passed to the constructor is set to
   * <code>true</code>, the sequence will wrap, returning negative values when
   * <code>Long.MAX_VALUE</code> reached; otherwise an <code>IllegalStateException</code>
   * will be thrown.</p>
   *
   * @author Commons-Uid team
   * @version $Id: LongGenerator.java,v 1.1 2003/12/30 07:29:18 psteitz Exp $
   */
  public class LongGenerator extends AbstractLongIdentifierGenerator {
  
      /** Should the counter wrap. */
      private boolean wrap;
      /** The counter. */
      private long count = 0;
      /**
       * <p>Constructor.</p>
       *
       * @param wrap should the factory wrap when it reaches the maximum
       *  long value (or throw an exception)
       * @param initialValue  the initial long value to start at
       */
      public LongGenerator(boolean wrap, long initialValue) {
          super();
          this.wrap = wrap;
          this.count = initialValue;
      }
  
      /**
       * <p> Returns the maximum value for an identifier from this generator.</p>
       *
       * @return the maximum idendifier value
       */
      public long maxValue() {
          return Long.MAX_VALUE;
      }
  
      /**
       * <p> Returns the minimum value for an identifier from this generator.</p>
       *
       * @return the minimum idendifier value
       */
      public long minValue() {
          return Long.MIN_VALUE;
      }
  
      /**
       * Getter for property wrap.
       *
       * @return <code>true</code> iff this generator is set up to wrap.
       *
       */
      public boolean isWrap() {
          return wrap;
      }
  
      /**
       * Sets the wrap property.
       *
       * @param wrap value for the wrap property
       *
       */
      public void setWrap(boolean wrap) {
          this.wrap = wrap;
      }
      /**
       * <p>Gets the next identifier in the sequence.</p>
       *
       * @return the next Long identifier in sequence
       */
      public Long nextLongIdentifier() {
          long value = 0;
          if (wrap) {
              synchronized (this) {
                  value = count++;
              }
          } else {
              synchronized (this) {
                  if (count == Long.MAX_VALUE) {
                      throw new IllegalStateException
                      ("The maximum number of identifiers has been reached");
                  }
                  value = count++;
              }
          }
          return new Long(value);
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/serial/NumericGenerator.java
  
  Index: NumericGenerator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/serial/NumericGenerator.java,v 1.1 2003/12/30 07:29:18 psteitz Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.uid.serial;
  
  import org.apache.commons.uid.AbstractStringIdentifierGenerator;
  
  /**
   * <p><code>NumericIdentifierGenerator</code> is an Identifier Generator
   * that generates an incrementing number as a String object.</p>
   *
   * <p>If the <code>wrap</code> argument passed to the constructor is set to
   * <code>true</code>, the sequence will wrap, returning negative values when
   * <code>Long.MAX_VALUE</code> reached; otherwise an <code>IllegalStateException</code>
   * will be thrown.</p>
   *
   * @author Commons-Uid team
   * @version $Id: NumericGenerator.java,v 1.1 2003/12/30 07:29:18 psteitz Exp $
   */
  public class NumericGenerator extends AbstractStringIdentifierGenerator {
  
      /** Should the counter wrap. */
      private boolean wrap;
      /** The counter. */
      private long count = 0;
  
      /**
       * <p>Constructor.</p>
       *
       * @param wrap should the factory wrap when it reaches the maximum
       *  long value (or throw an exception)
       * @param initialValue  the initial long value to start at
       */
      public NumericGenerator(boolean wrap, long initialValue) {
          super();
          this.wrap = wrap;
          this.count = initialValue;
      }
  
      /**
       * <p>Returns the maximum length (number or characters) for an identifier
       * from this sequence.</p>
       *
       * @return the maximum identifier length
       */
      public long maxLength() {
          return AbstractStringIdentifierGenerator.MAX_LONG_NUMERIC_VALUE_LENGTH;
      }
  
      /**
       * <p>Returns the minimum length (number of characters) for an identifier
       * from this sequence.</p>
       *
       * @return the minimum identifier length
       */
      public long minLength() {
          return 1;
      }
  
      /**
       * Getter for property wrap.
       *
       * @return <code>true</code> iff this generator is set up to wrap.
       *
       */
      public boolean isWrap() {
          return wrap;
      }
  
      /**
       * Sets the wrap property.
       *
       * @param wrap value for the wrap property
       *
       */
      public void setWrap(boolean wrap) {
          this.wrap = wrap;
      }
  
      /**
       * <p>Gets the next new identifier.</p>
       *
       * @return a new identifier as a String
       */
      public String nextStringIdentifier() {
          long value = 0;
          if (wrap) {
              synchronized (this) {
                  value = count++;
              }
          } else {
              synchronized (this) {
                  if (count == Long.MAX_VALUE) {
                      throw new IllegalStateException
                      ("The maximum number of identifiers has been reached");
                  }
                  value = count++;
              }
          }
          return Long.toString(value);
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/serial/package.html
  
  Index: package.html
  ===================================================================
  <body>
    <p>
      This package contains identifier generators that yield sequences of
      identifiers that follow a prescribed order.
    </p>
  </body>
  
  
  1.1                  jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/uuid/package.html
  
  Index: package.html
  ===================================================================
  <body>
    <p>
      This package contains contains implementations of the 
       <a href="http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-01.txt">
       IETF Draft UUID specification</a>. 
    </p>
  </body>
  
  
  1.1                  jakarta-commons-sandbox/uid/src/test/org/apache/commons/uid/IdentifierUtilsTest.java
  
  Index: IdentifierUtilsTest.java
  ===================================================================
  /*
   *===================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.uid;
  
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Modifier;
  
  import junit.framework.AssertionFailedError;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  /**
   * Tests the org.apache.commons.uid.IdentifierUtils class.
   *
   * @author Commons-Uid team
   * @version $Id: IdentifierUtilsTest.java,v 1.1 2003/12/30 07:29:18 psteitz Exp $
   */
  public class IdentifierUtilsTest extends junit.framework.TestCase {
  
      /**
       * Construct
       */
      public IdentifierUtilsTest(String name) {
          super(name);
      }
  
      /**
       * Return class aa a test suite.
       */
      public static Test suite() {
          TestSuite suite = new TestSuite(IdentifierUtilsTest.class);
          suite.setName("IdentifierUtils Tests");
          return suite;
      }
      
      protected static IdentifierGeneratorFactory factory = IdentifierGeneratorFactory.newInstance();
  
      //-----------------------------------------------------------------------
      public void testConstructor() {
          assertNotNull(new IdentifierUtils());
          Constructor[] cons = IdentifierUtils.class.getDeclaredConstructors();
          assertEquals(1, cons.length);
          assertEquals(false, Modifier.isPublic(cons[0].getModifiers()));
          assertEquals(true, Modifier.isPublic(IdentifierUtils.class.getModifiers()));
          assertEquals(false, Modifier.isFinal(IdentifierUtils.class.getModifiers()));
      }
      
      //--------------------------------------------------------------------------
  
      public void testLongIncrementing() {
          LongIdentifierGenerator f = IdentifierUtils.LONG_IDENTIFIER_GENERATOR;
          assertEquals(new Long(0), f.nextLongIdentifier());
          assertEquals(new Long(1), f.nextLongIdentifier());
          assertEquals(new Long(2), f.nextIdentifier());
          assertEquals(new Long(3), f.nextLongIdentifier());
          assertEquals(new Long(4), IdentifierUtils.nextLongIdentifier());
          assertEquals(new Long(5), f.nextLongIdentifier());
          assertEquals(new Long(6), IdentifierUtils.nextLongIdentifier());
          assertEquals(new Long(7), IdentifierUtils.nextLongIdentifier());
      }
  
      public void testLongIncrementingNoArgs() {
          LongIdentifierGenerator f = factory.longGenerator();
          assertEquals(new Long(0), f.nextLongIdentifier());
          assertEquals(new Long(1), f.nextLongIdentifier());
          assertTrue(f != IdentifierUtils.LONG_IDENTIFIER_GENERATOR);
      }
  
      public void testLongIncrementingInit() {
          LongIdentifierGenerator f = factory.longGenerator(true, 100);
          assertEquals(new Long(100), f.nextLongIdentifier());
          assertEquals(new Long(101), f.nextLongIdentifier());
      }
  
      public void testLongIncrementingWrap() {
          LongIdentifierGenerator f = factory.longGenerator(true, Long.MAX_VALUE - 1);
          assertEquals(new Long(Long.MAX_VALUE - 1), f.nextLongIdentifier());
          assertEquals(new Long(Long.MAX_VALUE), f.nextLongIdentifier());
          assertEquals(new Long(Long.MIN_VALUE), f.nextLongIdentifier());
      }
  
      public void testLongIncrementingNoWrap() {
          LongIdentifierGenerator f = factory.longGenerator(false, Long.MAX_VALUE);
          try {
              f.nextLongIdentifier();
              fail();
          } catch (IllegalStateException ex) {}
      }
  
      //--------------------------------------------------------------------------
  
      public void testStringNumericLong() {
          StringIdentifierGenerator f = IdentifierUtils.STRING_NUMERIC_IDENTIFIER_GENERATOR;
          assertEquals("0", f.nextStringIdentifier());
          assertEquals("1", f.nextStringIdentifier());
          assertEquals("2", f.nextIdentifier());
          assertEquals("3", f.nextStringIdentifier());
          assertEquals("4", IdentifierUtils.nextStringNumericIdentifier());
          assertEquals("5", f.nextStringIdentifier());
          assertEquals("6", IdentifierUtils.nextStringNumericIdentifier());
          assertEquals("7", IdentifierUtils.nextStringNumericIdentifier());
      }
  
      public void testStringNumericNoArgs() {
          StringIdentifierGenerator f = factory.numericGenerator();
          assertEquals("0", f.nextStringIdentifier());
          assertEquals("1", f.nextStringIdentifier());
          assertTrue(f != IdentifierUtils.STRING_NUMERIC_IDENTIFIER_GENERATOR);
      }
  
      public void testStringNumericInit() {
          StringIdentifierGenerator f = factory.numericGenerator(true, 100);
          assertEquals("100", f.nextStringIdentifier());
          assertEquals("101", f.nextStringIdentifier());
      }
  
      public void testStringNumericWrap() {
          StringIdentifierGenerator f = factory.numericGenerator(true, Long.MAX_VALUE - 1);
          assertEquals(Long.toString(Long.MAX_VALUE - 1), f.nextStringIdentifier());
          assertEquals(Long.toString(Long.MAX_VALUE), f.nextStringIdentifier());
          assertEquals(Long.toString(Long.MIN_VALUE), f.nextStringIdentifier());
      }
  
      public void testStringNumericNoWrap() {
          StringIdentifierGenerator f = factory.numericGenerator(false, Long.MAX_VALUE);
          try {
              f.nextStringIdentifier();
              fail();
          } catch (IllegalStateException ex) { }
      }
  
      //--------------------------------------------------------------------------
  
      public void testStringAlphanumeric() {
          StringIdentifierGenerator f = IdentifierUtils.STRING_ALPHANUMERIC_IDENTIFIER_GENERATOR;
          assertEquals("000000000000001", f.nextStringIdentifier());
          assertEquals("000000000000002", f.nextIdentifier());
          assertEquals("000000000000003", f.nextStringIdentifier());
          assertEquals("000000000000004", f.nextStringIdentifier());
          assertEquals("000000000000005", f.nextStringIdentifier());
          assertEquals("000000000000006", f.nextStringIdentifier());
          assertEquals("000000000000007", f.nextStringIdentifier());
          assertEquals("000000000000008", f.nextStringIdentifier());
          assertEquals("000000000000009", f.nextStringIdentifier());
          assertEquals("00000000000000a", f.nextStringIdentifier());
          assertEquals("00000000000000b", f.nextStringIdentifier());
          assertEquals("00000000000000c", f.nextStringIdentifier());
          assertEquals("00000000000000d", IdentifierUtils.nextStringAlphanumericIdentifier());
          assertEquals("00000000000000e", f.nextStringIdentifier());
          assertEquals("00000000000000f", f.nextStringIdentifier());
          assertEquals("00000000000000g", f.nextStringIdentifier());
          assertEquals("00000000000000h", f.nextStringIdentifier());
          assertEquals("00000000000000i", f.nextStringIdentifier());
          assertEquals("00000000000000j", f.nextStringIdentifier());
          assertEquals("00000000000000k", f.nextStringIdentifier());
          assertEquals("00000000000000l", f.nextStringIdentifier());
          assertEquals("00000000000000m", f.nextStringIdentifier());
          assertEquals("00000000000000n", f.nextStringIdentifier());
          assertEquals("00000000000000o", f.nextStringIdentifier());
          assertEquals("00000000000000p", f.nextStringIdentifier());
          assertEquals("00000000000000q", f.nextStringIdentifier());
          assertEquals("00000000000000r", f.nextStringIdentifier());
          assertEquals("00000000000000s", f.nextStringIdentifier());
          assertEquals("00000000000000t", f.nextStringIdentifier());
          assertEquals("00000000000000u", f.nextStringIdentifier());
          assertEquals("00000000000000v", f.nextStringIdentifier());
          assertEquals("00000000000000w", f.nextStringIdentifier());
          assertEquals("00000000000000x", f.nextStringIdentifier());
          assertEquals("00000000000000y", f.nextStringIdentifier());
          assertEquals("00000000000000z", f.nextStringIdentifier());
          assertEquals("000000000000010", f.nextStringIdentifier());
          assertEquals("000000000000011", f.nextStringIdentifier());
          assertEquals("000000000000012", f.nextStringIdentifier());
          assertEquals("000000000000013", f.nextStringIdentifier());
      }
  
      public void testLongAlphanumericNoArgs() {
          StringIdentifierGenerator f = factory.alphanumericGenerator();
          assertEquals("000000000000001", f.nextStringIdentifier());
          assertEquals("000000000000002", f.nextStringIdentifier());
          assertTrue(f != IdentifierUtils.STRING_ALPHANUMERIC_IDENTIFIER_GENERATOR);
      }
  
      public void testStringAlphanumericWrap() {
          try {
              StringIdentifierGenerator f = factory.alphanumericGenerator(true, -1);
              fail();
          } catch (IllegalArgumentException ex) {}
          
          StringIdentifierGenerator f = factory.alphanumericGenerator(true, 1);
          assertEquals("1", f.nextStringIdentifier());
          assertEquals("2", f.nextStringIdentifier());
          assertEquals("3", f.nextStringIdentifier());
          assertEquals("4", f.nextStringIdentifier());
          assertEquals("5", f.nextStringIdentifier());
          assertEquals("6", f.nextStringIdentifier());
          assertEquals("7", f.nextStringIdentifier());
          assertEquals("8", f.nextStringIdentifier());
          assertEquals("9", f.nextStringIdentifier());
          assertEquals("a", f.nextStringIdentifier());
          assertEquals("b", f.nextStringIdentifier());
          assertEquals("c", f.nextStringIdentifier());
          assertEquals("d", f.nextStringIdentifier());
          assertEquals("e", f.nextStringIdentifier());
          assertEquals("f", f.nextStringIdentifier());
          assertEquals("g", f.nextStringIdentifier());
          assertEquals("h", f.nextStringIdentifier());
          assertEquals("i", f.nextStringIdentifier());
          assertEquals("j", f.nextStringIdentifier());
          assertEquals("k", f.nextStringIdentifier());
          assertEquals("l", f.nextStringIdentifier());
          assertEquals("m", f.nextStringIdentifier());
          assertEquals("n", f.nextStringIdentifier());
          assertEquals("o", f.nextStringIdentifier());
          assertEquals("p", f.nextStringIdentifier());
          assertEquals("q", f.nextStringIdentifier());
          assertEquals("r", f.nextStringIdentifier());
          assertEquals("s", f.nextStringIdentifier());
          assertEquals("t", f.nextStringIdentifier());
          assertEquals("u", f.nextStringIdentifier());
          assertEquals("v", f.nextStringIdentifier());
          assertEquals("w", f.nextStringIdentifier());
          assertEquals("x", f.nextStringIdentifier());
          assertEquals("y", f.nextStringIdentifier());
          assertEquals("z", f.nextStringIdentifier());
          assertEquals("0", f.nextStringIdentifier());
      }
  
      public void testStringAlphanumericNoWrap() {
          try {
              StringIdentifierGenerator f = factory.alphanumericGenerator(false, -1);
              fail();
          } catch (IllegalArgumentException ex) {}
          
          StringIdentifierGenerator f = factory.alphanumericGenerator(false, 1);
          assertEquals("1", f.nextStringIdentifier());
          assertEquals("2", f.nextStringIdentifier());
          assertEquals("3", f.nextStringIdentifier());
          assertEquals("4", f.nextStringIdentifier());
          assertEquals("5", f.nextStringIdentifier());
          assertEquals("6", f.nextStringIdentifier());
          assertEquals("7", f.nextStringIdentifier());
          assertEquals("8", f.nextStringIdentifier());
          assertEquals("9", f.nextStringIdentifier());
          assertEquals("a", f.nextStringIdentifier());
          assertEquals("b", f.nextStringIdentifier());
          assertEquals("c", f.nextStringIdentifier());
          assertEquals("d", f.nextStringIdentifier());
          assertEquals("e", f.nextStringIdentifier());
          assertEquals("f", f.nextStringIdentifier());
          assertEquals("g", f.nextStringIdentifier());
          assertEquals("h", f.nextStringIdentifier());
          assertEquals("i", f.nextStringIdentifier());
          assertEquals("j", f.nextStringIdentifier());
          assertEquals("k", f.nextStringIdentifier());
          assertEquals("l", f.nextStringIdentifier());
          assertEquals("m", f.nextStringIdentifier());
          assertEquals("n", f.nextStringIdentifier());
          assertEquals("o", f.nextStringIdentifier());
          assertEquals("p", f.nextStringIdentifier());
          assertEquals("q", f.nextStringIdentifier());
          assertEquals("r", f.nextStringIdentifier());
          assertEquals("s", f.nextStringIdentifier());
          assertEquals("t", f.nextStringIdentifier());
          assertEquals("u", f.nextStringIdentifier());
          assertEquals("v", f.nextStringIdentifier());
          assertEquals("w", f.nextStringIdentifier());
          assertEquals("x", f.nextStringIdentifier());
          assertEquals("y", f.nextStringIdentifier());
          assertEquals("z", f.nextStringIdentifier());
          try {
              f.nextStringIdentifier();
              fail();
          } catch (IllegalStateException ex) {}
      }
  
      //--------------------------------------------------------------------------
  
      public void testStringSession() {
          StringIdentifierGenerator f = factory.sessionIdGenerator();
          assertTrue(f != IdentifierUtils.STRING_SESSION_IDENTIFIER_GENERATOR);
          
          String a = (String) f.nextStringIdentifier();
          String b = (String) IdentifierUtils.nextStringSessionIdentifier();
          String c = (String) f.nextIdentifier();
          assertTrue(a.length() >= 10);
          assertTrue(b.length() >= 10);
          assertTrue(c.length() >= 10);
          try {
              // could fail, but unlikely
              assertTrue(a.substring(6, 9).equals(b.substring(6, 9)));
              assertTrue(a.substring(6, 9).equals(c.substring(6, 9)));
          } catch (AssertionFailedError ex) {
              // try again to make test more robust
              a = (String) f.nextStringIdentifier();
              b = (String) IdentifierUtils.nextStringSessionIdentifier();
              c = (String) f.nextIdentifier();
              assertTrue(a.substring(6, 9).equals(b.substring(6, 9)));
              assertTrue(a.substring(6, 9).equals(c.substring(6, 9)));
          }
          assertEquals("0", a.substring(9));
          assertEquals("0", b.substring(9)); //Different from [lang], factory is independent
          assertEquals("1", c.substring(9)); // "" "" 
      }
  
      //--------------------------------------------------------------------------
  
  }
  
  
  

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