commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/identifier/factory StringIncrementingVLongIdentifierFactory.java StringRMIIdentifierFactory.java StringAlphanumeric15IdentifierFactory.java StringIncrementingLongIdentifierFactory.java StringAlphanumeric10IdentifierFactory.java LongRandomIdentifierFactory.java LongIncrementingIdentifierFactory.java
Date Sat, 29 Jun 2002 21:42:09 GMT
scolebourne    2002/06/29 14:42:09

  Added:       pattern/src/java/org/apache/commons/pattern/identifier/factory
                        StringIncrementingVLongIdentifierFactory.java
                        StringRMIIdentifierFactory.java
                        StringAlphanumeric15IdentifierFactory.java
                        StringIncrementingLongIdentifierFactory.java
                        StringAlphanumeric10IdentifierFactory.java
                        LongRandomIdentifierFactory.java
                        LongIncrementingIdentifierFactory.java
  Log:
  Initial checkin of Identifier factories
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/identifier/factory/StringIncrementingVLongIdentifierFactory.java
  
  Index: StringIncrementingVLongIdentifierFactory.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements 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.pattern.identifier.factory;
  
  import java.io.Serializable;
  
  import org.apache.commons.lang.Strings;
  import org.apache.commons.pattern.factory.Factory;
  import org.apache.commons.pattern.identifier.IdentifierFactoryException;
  /**
   * <code>IncrementingBigStringIdentifierFactory</code> is an Identifier Factory
   * that generates an incrementing number as a two Long objects.
   *
   * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
   * @version $Id: StringIncrementingVLongIdentifierFactory.java,v 1.1 2002/06/29 21:42:09
scolebourne Exp $
   */
  public class StringIncrementingVLongIdentifierFactory implements Factory, Serializable {
      
      /**
       * StringIncrementingVLongIdentifierFactory identifier factory
       */
      public static final Factory INSTANCE = new StringIncrementingVLongIdentifierFactory();
  
      /**
       * Should the counter wrap
       */
      private final boolean iWrap;
      /**
       * Should the counter be padded, and to what length
       */
      private final int iPadSize;
      /**
       * The counters
       */
      private long iCount1 = 0;
      private long iCount2 = 0;
          
      /**
       * Constructor that defaults to non-wrapped, non-padded
       * 
       * @param wrap  should the factory wrap when it reaches the maximum 
       *  long value (or throw an exception)
       */
      public StringIncrementingVLongIdentifierFactory() {
          this(false, -1);
      }
  
      /**
       * Constructor that defaults to non-padded
       * 
       * @param wrap  should the factory wrap when it reaches the maximum 
       *  value (or throw an exception)
       */
      public StringIncrementingVLongIdentifierFactory(boolean wrap) {
          this(wrap, -1);
      }
  
      /**
       * Constructor
       * 
       * @param wrap  should the factory wrap when it reaches the maximum 
       *  value (or throw an exception)
       * @param padLength  the length to pad to with leading zeros
       */
      public StringIncrementingVLongIdentifierFactory(boolean wrap, int padLength) {
          super();
          
          iWrap = wrap;
          if (padLength > 0) {
              iPadSize = padLength;
          } else {
              iPadSize = -1;
          }
      }
  
      /**
       * Create a new identifier
       * 
       * @return a new identifier as a Long
       */
      public Object create() {
          long value1 = 0;
          long value2 = 0;
          synchronized (this) {
              if (iCount1 == Long.MAX_VALUE) {
                  if (iCount2 == Long.MAX_VALUE) {
                      if (iWrap) {
                          iCount2 = 0;
                      } else {
                          throw new IdentifierFactoryException("The maximum number of identifiers
has been reached");
                      }
                  } else {
                      iCount2++;
                  }
                  iCount1 = 0;
              }
              value1 = iCount1++;
              value2 = iCount2;
          }
          StringBuffer buf = new StringBuffer(50);
          buf.append(Long.toString(value2));
          buf.append(Long.toString(value1));
          if (iPadSize > 0) {
              try {
                  char[] array = new char[iPadSize - buf.length()];
                  for (int i = 0; i < array.length; i++) {
                      array[i] = '0';
                  }
                  buf.insert(0, array);
                  return buf.toString();
                  
              } catch (NegativeArraySizeException ex) {
                   throw new IdentifierFactoryException("The padding size for the identifier
has been exceeded");
              }
          } else {
              return buf.toString();
          }
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/identifier/factory/StringRMIIdentifierFactory.java
  
  Index: StringRMIIdentifierFactory.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements 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.pattern.identifier.factory;
  
  import java.io.Serializable;
  import java.rmi.server.UID;
  
  import org.apache.commons.pattern.factory.Factory;
  /**
   * <code>StringRMIIdentifierFactory</code> is an Identifier Factory
   * that generates an identifier from the RMI UID class.
   *
   * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
   * @version $Id: StringRMIIdentifierFactory.java,v 1.1 2002/06/29 21:42:09 scolebourne Exp
$
   */
  public class StringRMIIdentifierFactory implements Factory, Serializable {
      
      /**
       * StringIncrementingVLongIdentifierFactory identifier factory
       */
      public static final Factory INSTANCE = new StringRMIIdentifierFactory();
  
      /**
       * Constructor
       */
      public StringRMIIdentifierFactory() {
          super();
      }
  
      /**
       * Create a new identifier
       * 
       * @return a new identifier as a Long
       */
      public Object create() {
          return new UID().toString();
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/identifier/factory/StringAlphanumeric15IdentifierFactory.java
  
  Index: StringAlphanumeric15IdentifierFactory.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements 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.pattern.identifier.factory;
  
  import java.io.Serializable;
  import java.util.Random;
  
  import org.apache.commons.pattern.factory.Factory;
  /**
   * <code>StringAlphanumeric15IdentifierFactory</code> is an Identifier Factory
   * that generates an alphanumeric 12 character identifier.
   * <p>
   * Based on StringAlphanumeric10IdentifierFactory, with the difference being
   * control of the length of the string, and thus a limitation on the number
   * of ids that can be generated in 2 seconds (2,176,782,336 ids).
   *
   * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
   * @author <a href="mailto:neeme@one.lv">Neeme Praks</a>
   * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
   * @version $Id: StringAlphanumeric15IdentifierFactory.java,v 1.1 2002/06/29 21:42:09 scolebourne
Exp $
   */
  public class StringAlphanumeric15IdentifierFactory implements Factory, Serializable {
  
      /**
       * StringAlphanumeric15IdentifierFactory identifier factory
       */
      public static final Factory INSTANCE = new StringAlphanumeric15IdentifierFactory();
  
      /**
       * 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 final static long MAX_RANDOM_LEN = 2176782336L; // 36 ** 6
  
      /**
       * The identifier must be unique within the typical lifespan of a
       * session; the value can roll over after that.  3 characters:
       * (this means a roll over after over a day, which is much larger
       * than a typical lifespan)
       */
      private final static long MAX_TIME_SECTION_LEN = 46656L; // 36 ** 3
  
      /**
       * The counter will take up 6 characters. This is for the base 36 calc.
       * The limitation on the counter is Integer.MAX_VALUE before rollover,
       * but there shouldn't be 2 billion ids every 2 secs.
       */
      private final static long MAX_COUNTER_LEN = 2176782336L; // 36 ** 6
  
      /**
       * Milliseconds between different tics.  The 3-character time
       * string has a new value every 2 seconds.
       */
      private final static long TIC_DIFFERENCE = 2000;
  
      /**
       * The incrementing counter
       */
      private static long iCounter = 0;
      /**
       * The last time
       */
      private static long iLastTimeVal = 0;
      /**
       * The randmonizer
       */
      private static Random iRandom = new Random();
  
      /**
       * Constructor
       */
      public StringAlphanumeric15IdentifierFactory() {
          super();
      }
  
      /**
       * Create a new identifier. Only guaranteed unique within
       * this JVM, but fairly safe for cross JVM usage as well.
       * 
       * <p>Format of identifier is
       * [3 chars random][2 chars count][3 chars time][2 chars count][3 chars random][2 chars
count]</p>
       * 
       * @return a new identifier as a Long
       */
      public Object create() {
          // Random value
          //--------------
          long random = iRandom.nextLong();
          if (random < 0) {
              random = -random;
          }
          // 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
          random %= MAX_RANDOM_LEN;
          random += MAX_RANDOM_LEN;
  
          long timeVal = 0;
          long counter = 0;
          
          synchronized (this) {
              // Time
              //--------------
              timeVal = (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
              timeVal %= MAX_TIME_SECTION_LEN;
              timeVal += 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 (iLastTimeVal != timeVal) {
                  iLastTimeVal = timeVal;
                  iCounter = 0;
              }
              counter = iCounter++;
              if (iCounter < 0) {
                  iCounter = -iCounter;
              }
          }
          // 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
          counter %= MAX_COUNTER_LEN;
          counter += MAX_COUNTER_LEN;
  
          // build string        
          //--------------
          StringBuffer id = new StringBuffer(15);
          id.append(Long.toString(random, 36).substring(1));   // 6 chars
          id.append(Long.toString(timeVal, 36).substring(1));  // 3 chars
          id.append(Long.toString(counter, 36).substring(1));  // 6 chars
          return id.toString();
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/identifier/factory/StringIncrementingLongIdentifierFactory.java
  
  Index: StringIncrementingLongIdentifierFactory.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements 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.pattern.identifier.factory;
  
  import java.io.Serializable;
  
  import org.apache.commons.pattern.factory.Factory;
  import org.apache.commons.pattern.identifier.IdentifierFactoryException;
  /**
   * <code>StringIncrementingLongIdentifierFactory</code> is an Identifier Factory
   * that generates an incrementing number as a String object.
   *
   * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
   * @version $Id: StringIncrementingLongIdentifierFactory.java,v 1.1 2002/06/29 21:42:09
scolebourne Exp $
   */
  public class StringIncrementingLongIdentifierFactory implements Factory, Serializable {
      
      /**
       * StringIncrementingLongIdentifierFactory identifier factory
       */
      public static final Factory INSTANCE = new StringIncrementingLongIdentifierFactory();
  
      /**
       * Should the counter wrap
       */
      private final boolean iWrap;
      /**
       * Should the counter be padded, and to what length
       */
      private final long iPadScale;
      /**
       * The counter
       */
      private long iCount = 0;
          
      /**
       * Constructor that defaults to non-wrapped, non-padded starting from zero
       * 
       * @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 StringIncrementingLongIdentifierFactory() {
          this(false, 0, -1);
      }
  
      /**
       * Constructor that defaults to non-padded, starting from zero
       * 
       * @param wrap  should the factory wrap when it reaches the maximum 
       *  long value (or throw an exception)
       */
      public StringIncrementingLongIdentifierFactory(boolean wrap) {
          this(wrap, 0, -1);
      }
  
      /**
       * Constructor that defaults to non-padded
       * 
       * @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 StringIncrementingLongIdentifierFactory(boolean wrap, long initialValue) {
          this(wrap, initialValue, -1);
      }
  
      /**
       * Constructor that defaults to non-padded
       * 
       * @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
       * @param padLength  the length to pad to with leading zeros
       */
      public StringIncrementingLongIdentifierFactory(boolean wrap, long initialValue, int
padLength) {
          super();
          
          iWrap = wrap;
          iCount = initialValue;
          if (padLength > 0) {
              iPadScale = (long) Math.pow(10, padLength);
          } else {
              iPadScale = 0;
          }
      }
  
      /**
       * Create a new identifier
       * 
       * @return a new identifier as a String
       */
      public Object create() {
          long value = 0;
          if (iWrap) {
              synchronized (this) {
                  value = iCount++;
              }
          } else {
              synchronized (this) {
                  if (iCount == Long.MAX_VALUE) {
                      throw new IdentifierFactoryException("The maximum number of identifiers
has been reached");
                  }
                  value = iCount++;
              }
          }
          if (iPadScale > 0) {
              value += iPadScale;
              return Long.toString(value).substring(1);
          } else {
              return Long.toString(value);
          }
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/identifier/factory/StringAlphanumeric10IdentifierFactory.java
  
  Index: StringAlphanumeric10IdentifierFactory.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements 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.pattern.identifier.factory;
  
  import java.io.Serializable;
  import java.util.Random;
  
  import org.apache.commons.pattern.factory.Factory;
  /**
   * <code>StringAlphanumeric10IdentifierFactory</code> is an Identifier Factory
   * that generates an alphanumeric 10+ character identifier. The exact 
   * length depends on the number of ids requested per time period.
   * <p>
   * Originally designed for JServ sessions. Uses synchronized count and
   * time to ensure uniqueness. Not guaranteed unique across JVMs, but
   * fairly safe none the less.
   *
   * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
   * @author <a href="mailto:neeme@one.lv">Neeme Praks</a>
   * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
   * @version $Id: StringAlphanumeric10IdentifierFactory.java,v 1.1 2002/06/29 21:42:09 scolebourne
Exp $
   */
  public class StringAlphanumeric10IdentifierFactory implements Factory, Serializable {
  
      /**
       * StringAlphanumeric10IdentifierFactory identifier factory
       */
      public static final Factory INSTANCE = new StringAlphanumeric10IdentifierFactory();
  
      /**
       * 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 final static long MAX_RANDOM_LEN = 2176782336L; // 36 ** 6
  
      /**
       * The identifier must be unique within the typical lifespan of a
       * session; the value can roll over after that.  3 characters:
       * (this means a roll over after over a day, which is much larger
       * than a typical lifespan)
       */
      private final static 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 final static long TIC_DIFFERENCE = 2000;
  
      /**
       * The incrementing counter
       */
      private static int iCounter = 0;
      /**
       * The last time
       */
      private static long iLastTimeVal = 0;
      /**
       * The randmonizer
       */
      private static Random iRandom = new Random();
  
      /**
       * Constructor
       */
      public StringAlphanumeric10IdentifierFactory() {
          super();
      }
  
      /**
       * Create a new identifier. Only guaranteed unique within
       * this JVM, but fairly safe for cross JVM usage as well.
       * 
       * <p>Format of identifier is
       * [6 chars random][3 chars time][1+ chars count]</p>
       * 
       * @return a new identifier as a Long
       */
      public Object create() {
          // Random value
          //--------------
          long random = iRandom.nextLong();
          if (random < 0) {
              random = -random;
          }
          // 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
          random %= MAX_RANDOM_LEN;
          random += MAX_RANDOM_LEN;
  
          long timeVal = 0;
          int counter = 0;
          
          synchronized (this) {
              // Time
              //--------------
              timeVal = (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
              timeVal %= MAX_TIME_SECTION_LEN;
              timeVal += 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 (iLastTimeVal != timeVal) {
                  iLastTimeVal = timeVal;
                  iCounter = 0;
              }
              counter = iCounter++;
          }
  
          // build string        
          //--------------
          StringBuffer id = new StringBuffer(15);
          id.append(Long.toString(random, 36).substring(1));  // 6 chars
          id.append(Long.toString(timeVal, 36).substring(1));  // 3 chars
          id.append(Long.toString(counter, 36));  // 1+ chars
          return id.toString();
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/identifier/factory/LongRandomIdentifierFactory.java
  
  Index: LongRandomIdentifierFactory.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements 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.pattern.identifier.factory;
  
  import java.io.Serializable;
  import java.util.Random;
  
  import org.apache.commons.pattern.factory.Factory;
  /**
   * <code>LongRandomIdentifierFactory</code> is an Identifier Factory
   * that generates an random number as a Long object. The identifier is
   * NOT guaranteed to be unique. (TODO: Find an algorithm that will allow
   * that guarantee)
   *
   * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
   * @version $Id: LongRandomIdentifierFactory.java,v 1.1 2002/06/29 21:42:09 scolebourne
Exp $
   */
  public class LongRandomIdentifierFactory implements Factory, Serializable {
      
      /**
       * LongRandomIdentifierFactory identifier factory
       */
      public static final Factory INSTANCE = new LongRandomIdentifierFactory();
  
      /**
       * Should the id always be positive
       */
      private final boolean iPositiveOnly;
      /**
       * The randomizer
       */
      private Random iRandom = new Random();
          
      /**
       * Constructor that allows only positive random longs
       */
      public LongRandomIdentifierFactory() {
          this(true);
      }
  
      /**
       * Constructor
       * 
       * @param positiveOnly  allow positive numbers only
       */
      public LongRandomIdentifierFactory(boolean positiveOnly) {
          super();
          
          iPositiveOnly = positiveOnly;
      }
  
      /**
       * Create a new identifier
       * 
       * @return a new identifier as a Long
       */
      public Object create() {
          long value = 0;
          if (iPositiveOnly) {
              synchronized (this) {
                  do {
                      value = iRandom.nextLong();
                  } while (value < 0);
              }
          } else {
              synchronized (this) {
                  value = iRandom.nextLong();
              }
          }
          return new Long(value);
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/identifier/factory/LongIncrementingIdentifierFactory.java
  
  Index: LongIncrementingIdentifierFactory.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements 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.pattern.identifier.factory;
  
  import java.io.Serializable;
  
  import org.apache.commons.pattern.factory.Factory;
  import org.apache.commons.pattern.identifier.IdentifierFactoryException;
  /**
   * <code>LongIncrementingIdentifierFactory</code> is an Identifier Factory
   * that generates an incrementing number as a Long object.
   *
   * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
   * @version $Id: LongIncrementingIdentifierFactory.java,v 1.1 2002/06/29 21:42:09 scolebourne
Exp $
   */
  public class LongIncrementingIdentifierFactory implements Factory, Serializable {
      
      /**
       * LongIncrementingIdentifierFactory identifier factory
       */
      public static final Factory INSTANCE = new LongIncrementingIdentifierFactory();
  
      /**
       * Should the counter wrap
       */
      private final boolean iWrap;
      /**
       * The counter
       */
      private long iCount = 0;
          
      /**
       * Constructor that defaults to non-wrapped starting from zero
       * 
       * @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 LongIncrementingIdentifierFactory() {
          this(false, 0);
      }
  
      /**
       * Constructor that defaults to starting from zero
       * 
       * @param wrap  should the factory wrap when it reaches the maximum 
       *  long value (or throw an exception)
       */
      public LongIncrementingIdentifierFactory(boolean wrap) {
          this(wrap, 0);
      }
  
      /**
       * Constructor
       * 
       * @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 LongIncrementingIdentifierFactory(boolean wrap, long initialValue) {
          super();
          
          iWrap = wrap;
          iCount = initialValue;
      }
  
      /**
       * Create a new identifier
       * 
       * @return a new identifier as a Long
       */
      public Object create() {
          long value = 0;
          if (iWrap) {
              synchronized (this) {
                  value = iCount++;
              }
          } else {
              synchronized (this) {
                  if (iCount == Long.MAX_VALUE) {
                      throw new IdentifierFactoryException("The maximum number of identifiers
has been reached");
                  }
                  value = iCount++;
              }
          }
          return new Long(value);
      }
  
  }
  
  
  

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


Mime
View raw message