You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2002/11/23 02:05:48 UTC
cvs commit: jakarta-commons-sandbox/util/src/java/org/apache/commons/util/identifier StringIncrementingVLongIdentifierFactory.java StringRMIIdentifierFactory.java IdentifierUtils.java StringAlphanumeric15IdentifierFactory.java StringIncrementingLongIdentifierFactory.java StringAlphanumeric10IdentifierFactory.java LongRandomIdentifierFactory.java LongIncrementingIdentifierFactory.java IdentifierFactoryException.java
scolebourne 2002/11/22 17:05:48
Added: util/src/java/org/apache/commons/util/identifier
StringIncrementingVLongIdentifierFactory.java
StringRMIIdentifierFactory.java
IdentifierUtils.java
StringAlphanumeric15IdentifierFactory.java
StringIncrementingLongIdentifierFactory.java
StringAlphanumeric10IdentifierFactory.java
LongRandomIdentifierFactory.java
LongIncrementingIdentifierFactory.java
IdentifierFactoryException.java
Log:
Added identifier classes, originally in [pattern].
Subsumes the GenerateUniqueId class previously in [util]
Revision Changes Path
1.1 jakarta-commons-sandbox/util/src/java/org/apache/commons/util/identifier/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.util.identifier;
import java.io.Serializable;
import org.apache.commons.lang.functor.Factory;
/**
* <code>StringIncrementingVLongIdentifierFactory</code> is an Identifier Factory
* that generates an incrementing number as a two Long objects.
*
* @author Stephen Colebourne
* @version $Id: StringIncrementingVLongIdentifierFactory.java,v 1.1 2002/11/23 01:05:48 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;
/**
* Get the next identifier from the public INSTANCE factory.
*
* @return the next identifier
* @throws IdentifierFactoryException if the maximum identifiers is reached
*/
public static String nextIdentifier() {
return (String) INSTANCE.create();
}
/**
* 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/util/src/java/org/apache/commons/util/identifier/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.util.identifier;
import java.io.Serializable;
import java.rmi.server.UID;
import org.apache.commons.lang.functor.Factory;
/**
* <code>StringRMIIdentifierFactory</code> is an Identifier Factory
* that generates an identifier from the RMI UID class.
*
* @author Stephen Colebourne
* @version $Id: StringRMIIdentifierFactory.java,v 1.1 2002/11/23 01:05:48 scolebourne Exp $
*/
public class StringRMIIdentifierFactory implements Factory, Serializable {
/**
* StringIncrementingVLongIdentifierFactory identifier factory.
*/
public static final Factory INSTANCE = new StringRMIIdentifierFactory();
/**
* Get the next identifier from the public INSTANCE factory.
*
* @return the next identifier
*/
public static String nextIdentifier() {
return (String) INSTANCE.create();
}
/**
* 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/util/src/java/org/apache/commons/util/identifier/IdentifierUtils.java
Index: IdentifierUtils.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.util.identifier;
import org.apache.commons.lang.functor.Factory;
/**
* <code>IdentifierUtils</code> provides reference implementations and utilities
* for the Identifier factories.
*
* @author Stephen Colebourne
* @version $Id: IdentifierUtils.java,v 1.1 2002/11/23 01:05:48 scolebourne Exp $
*/
public class IdentifierUtils {
/**
* JVM shared default identifier factory
*/
public static Factory cDefault = new LongIncrementingIdentifierFactory(false);
/**
* Constructor. This class should not be instantiated.
*/
public IdentifierUtils() {
super();
}
/**
* Gets the default Identifier Factory.
*
* @return the default Identifier Factory
*/
public static Factory getDefaultIdentifierFactory() {
return cDefault;
}
/**
* Sets the default Identifier Factory. This method is shared across the
* JVM, thus should usually only be called during initialisation.
*
* @param defaultFactory the new fectory to set as the default
* @throws IllegalArgumentException if the factory specified is null
*/
public static void setDefaultIdentifierFactory(Factory defaultFactory) {
if (defaultFactory == null) {
throw new IllegalArgumentException("The Identifier Factory must not be null");
}
cDefault = defaultFactory;
}
/**
* Gets the next identifier using the default Identifier Factory.
* This is suitable for small programs where you can guarantee control
* of the default identifier.
*
* @return a new identifier
*/
public static Object nextIdentifier() {
return cDefault.create();
}
/**
* Gets the next identifier using the specified Identifier Factory.
* There is no requirement to use this method to get an identifier, as it
* simply forwards the call to the create method on the factory specified.
*
* @return a new identifier
* @throws IllegalArgumentException if the factory specified is null
*/
public static Object nextIdentifier(Factory identifierFactory) {
if (identifierFactory == null) {
throw new IllegalArgumentException("The Identifier Factory must not be null");
}
return identifierFactory.create();
}
}
1.1 jakarta-commons-sandbox/util/src/java/org/apache/commons/util/identifier/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.util.identifier;
import java.io.Serializable;
import java.util.Random;
import org.apache.commons.lang.functor.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 Stephen Colebourne
* @version $Id: StringAlphanumeric15IdentifierFactory.java,v 1.1 2002/11/23 01:05:48 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();
/**
* Get the next identifier from the public INSTANCE factory.
*
* @return the next identifier
*/
public static String nextIdentifier() {
return (String) INSTANCE.create();
}
/**
* 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/util/src/java/org/apache/commons/util/identifier/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.util.identifier;
import java.io.Serializable;
import org.apache.commons.lang.functor.Factory;
/**
* <code>StringIncrementingLongIdentifierFactory</code> is an Identifier Factory
* that generates an incrementing number as a String object.
*
* @author Stephen Colebourne
* @version $Id: StringIncrementingLongIdentifierFactory.java,v 1.1 2002/11/23 01:05:48 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;
/**
* Get the next identifier from the public INSTANCE factory.
*
* @return the next identifier
* @throws IdentifierFactoryException if the maximum identifiers is reached
*/
public static String nextIdentifier() {
return (String) INSTANCE.create();
}
/**
* 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/util/src/java/org/apache/commons/util/identifier/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.util.identifier;
import java.io.Serializable;
import java.util.Random;
import org.apache.commons.lang.functor.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 Stephen Colebourne
* @version $Id: StringAlphanumeric10IdentifierFactory.java,v 1.1 2002/11/23 01:05:48 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();
/**
* Get the next identifier from the public INSTANCE factory.
*
* @return the next identifier
*/
public static String nextIdentifier() {
return (String) INSTANCE.create();
}
/**
* 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/util/src/java/org/apache/commons/util/identifier/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.util.identifier;
import java.io.Serializable;
import java.util.Random;
import org.apache.commons.lang.functor.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 Stephen Colebourne
* @version $Id: LongRandomIdentifierFactory.java,v 1.1 2002/11/23 01:05:48 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();
/**
* Get the next identifier from the public INSTANCE factory.
*
* @return the next identifier
*/
public static Long nextIdentifier() {
return (Long) INSTANCE.create();
}
/**
* 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/util/src/java/org/apache/commons/util/identifier/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.util.identifier;
import java.io.Serializable;
import org.apache.commons.lang.functor.Factory;
/**
* <code>LongIncrementingIdentifierFactory</code> is an Identifier Factory
* that generates an incrementing number as a Long object.
*
* @author Stephen Colebourne
* @version $Id: LongIncrementingIdentifierFactory.java,v 1.1 2002/11/23 01:05:48 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;
/**
* Get the next identifier from the public INSTANCE factory.
*
* @return the next identifier
* @throws IdentifierFactoryException if the maximum identifiers is reached
*/
public static Long nextIdentifier() {
return (Long) INSTANCE.create();
}
/**
* 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);
}
}
1.1 jakarta-commons-sandbox/util/src/java/org/apache/commons/util/identifier/IdentifierFactoryException.java
Index: IdentifierFactoryException.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.util.identifier;
import org.apache.commons.lang.functor.FactoryException;
/**
* Exception thrown when the a method encounters a problem when creating
* an Identifier in a Factory. If required, a root cause error can be
* wrapped within this one.
*
* @author Stephen Colebourne
* @version $Id: IdentifierFactoryException.java,v 1.1 2002/11/23 01:05:48 scolebourne Exp $
*/
public class IdentifierFactoryException extends FactoryException {
/**
* Constructs a new <code>IdentifierException</code> without specified
* detail message.
*/
public IdentifierFactoryException() {
super();
}
/**
* Constructs a new <code>IdentifierException</code> with specified
* detail message.
*
* @param msg the error message.
*/
public IdentifierFactoryException(String msg) {
super(msg);
}
/**
* Constructs a new <code>IdentifierException</code> with specified
* nested <code>Throwable</code> root cause.
*
* @param rootCause the exception or error that caused this exception
* to be thrown.
*/
public IdentifierFactoryException(Throwable rootCause) {
super(rootCause);
}
/**
* Constructs a new <code>IdentifierException</code> with specified
* detail message and nested <code>Throwable</code> root cause.
*
* @param msg the error message.
* @param rootCause the exception or error that caused this exception
* to be thrown.
*/
public IdentifierFactoryException(String msg, Throwable rootCause) {
super(msg, rootCause);
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>