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 2003/05/17 00:06:45 UTC
cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/util StringIdentifierFactory.java IdentifierFactory.java LongIdentifierFactory.java IdentifierUtils.java
scolebourne 2003/05/16 15:06:44
Modified: lang/src/java/org/apache/commons/lang/util
IdentifierUtils.java
Added: lang/src/java/org/apache/commons/lang/util
StringIdentifierFactory.java IdentifierFactory.java
LongIdentifierFactory.java
Log:
Change Identifier generation code to be independent of functors
Revision Changes Path
1.5 +80 -46 jakarta-commons/lang/src/java/org/apache/commons/lang/util/IdentifierUtils.java
Index: IdentifierUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/util/IdentifierUtils.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- IdentifierUtils.java 9 Apr 2003 01:04:48 -0000 1.4
+++ IdentifierUtils.java 16 May 2003 22:06:43 -0000 1.5
@@ -56,15 +56,14 @@
import java.io.Serializable;
import java.util.Random;
-import org.apache.commons.lang.functor.Factory;
-import org.apache.commons.lang.functor.FactoryException;
/**
* <p><code>IdentifierUtils</code> provides a number of different identifier
* reference implementations.</p>
*
- * <p>All the identifer factories are serializable and synchronized. The
- * factories all implement the <i>functor</i>
- * {@link org.apache.commons.lang.functor.Factory Factory} interface</p>
+ * <p>All the identifer factories are serializable and synchronized.
+ * The factories all implement one of the factory interfaces defined in this
+ * package. This allows you to obtain and use multiple factories for
+ * different reasons.</p>
*
* @author Stephen Colebourne
* @since 2.0
@@ -87,7 +86,7 @@
* <li>...
* </ul>
*/
- public static final Factory LONG_IDENTIFIER_FACTORY = new LongIdentifierFactory(true, 0L);
+ public static final LongIdentifierFactory LONG_IDENTIFIER_FACTORY = new LongNumericIdentifierFactory(true, 0L);
/**
* <p>Singleton instance of the <code>StringNumericIdentifierFactory</code>.
* </p>
@@ -103,7 +102,7 @@
* <li>...
* </ul>
*/
- public static final Factory STRING_NUMERIC_IDENTIFIER_FACTORY = new StringNumericIdentifierFactory(true, 0L);
+ public static final StringIdentifierFactory STRING_NUMERIC_IDENTIFIER_FACTORY = new StringNumericIdentifierFactory(true, 0L);
/**
* <p>Singleton instance of the
* <code>StringAlphanumericIdentifierFactory</code>.</p>
@@ -128,7 +127,7 @@
* <li>...
* </ul>
*/
- public static final Factory STRING_ALPHANUMERIC_IDENTIFIER_FACTORY = new StringAlphanumericIdentifierFactory(true, 15);
+ public static final StringIdentifierFactory STRING_ALPHANUMERIC_IDENTIFIER_FACTORY = new StringAlphanumericIdentifierFactory(true, 15);
/**
* <p>Singleton instance of the
* <code>StringSessionIdentifierFactory</code>.</p>
@@ -138,7 +137,7 @@
*
* <p>The objects returned are 10 or more base-36 digits.</p>
*/
- public static final Factory STRING_SESSION_IDENTIFIER_FACTORY = new StringSessionIdentifierFactory();
+ public static final StringIdentifierFactory STRING_SESSION_IDENTIFIER_FACTORY = new StringSessionIdentifierFactory();
//---------------------------------------------------------------------------------
@@ -166,7 +165,7 @@
* @return a new identifier
*/
public static Long nextLongIdentifier() {
- return (Long) LONG_IDENTIFIER_FACTORY.create();
+ return LONG_IDENTIFIER_FACTORY.nextLongIdentifier();
}
/**
@@ -178,8 +177,8 @@
*
* @return a new identifier factory
*/
- public static Factory longIdentifierFactory() {
- return new LongIdentifierFactory(true, 0L);
+ public static LongIdentifierFactory longIdentifierFactory() {
+ return new LongNumericIdentifierFactory(true, 0L);
}
/**
@@ -187,12 +186,12 @@
* increasing in size.</p>
*
* @param wrap should the factory wrap when it reaches the maximum
- * long value (or throw an exception)
+ * long value (or throw an IllegalStateException)
* @param initialValue the initial long value to start at
* @return a new identifier factory
*/
- public static Factory longIdentifierFactory(boolean wrap, long initialValue) {
- return new LongIdentifierFactory(wrap, initialValue);
+ public static LongIdentifierFactory longIdentifierFactory(boolean wrap, long initialValue) {
+ return new LongNumericIdentifierFactory(wrap, initialValue);
}
//---------------------------------------------------------------------------------
@@ -208,7 +207,7 @@
* @return a new identifier
*/
public static String nextStringNumericIdentifier() {
- return (String) STRING_NUMERIC_IDENTIFIER_FACTORY.create();
+ return STRING_NUMERIC_IDENTIFIER_FACTORY.nextStringIdentifier();
}
/**
@@ -220,7 +219,7 @@
*
* @return a new identifier factory
*/
- public static Factory stringNumericIdentifierFactory() {
+ public static StringIdentifierFactory stringNumericIdentifierFactory() {
return new StringNumericIdentifierFactory(true, 0L);
}
@@ -229,11 +228,11 @@
* representing numbers increasing in size.</p>
*
* @param wrap should the factory wrap when it reaches the maximum
- * long value (or throw an exception)
+ * long value (or throw an IllegalStateException)
* @param initialValue the initial long value to start at
* @return a new identifier factory
*/
- public static Factory stringNumericIdentifierFactory(boolean wrap, long initialValue) {
+ public static StringIdentifierFactory stringNumericIdentifierFactory(boolean wrap, long initialValue) {
return new StringNumericIdentifierFactory(wrap, initialValue);
}
@@ -250,7 +249,7 @@
* @return a new identifier
*/
public static String nextStringAlphanumericIdentifier() {
- return (String) STRING_ALPHANUMERIC_IDENTIFIER_FACTORY.create();
+ return STRING_ALPHANUMERIC_IDENTIFIER_FACTORY.nextStringIdentifier();
}
/**
@@ -261,7 +260,7 @@
*
* @return a new identifier factory
*/
- public static Factory stringAlphanumericIdentifierFactory() {
+ public static StringIdentifierFactory stringAlphanumericIdentifierFactory() {
return new StringAlphanumericIdentifierFactory(true, 15);
}
@@ -270,11 +269,11 @@
* representing numbers increasing in size in base-36.</p>
*
* @param wrap should the factory wrap when it reaches the maximum
- * size (or throw an exception)
+ * size (or throw an IllegalStateException)
* @param size the number of characters the id should fill
* @return a new identifier factory
*/
- public static Factory stringAlphanumericIdentifierFactory(boolean wrap, int size) {
+ public static StringIdentifierFactory stringAlphanumericIdentifierFactory(boolean wrap, int size) {
return new StringAlphanumericIdentifierFactory(wrap, size);
}
@@ -285,14 +284,13 @@
* String Session factory.
* </p>
*
- * <p>The singleton instance is not guaranteed to be unique (although its
- * pretty unlikely), so in a long- lived server, the id may be duplicated.
- * </p>
+ * <p>The generation routine is based on a random number and a counter
+ * within a 2 second time interval.</p>
*
* @return a new identifier
*/
public static String nextStringSessionIdentifier() {
- return (String) STRING_SESSION_IDENTIFIER_FACTORY.create();
+ return STRING_SESSION_IDENTIFIER_FACTORY.nextStringIdentifier();
}
/**
@@ -305,7 +303,7 @@
*
* @return a new identifier factory
*/
- public static Factory stringSessionIdentifierFactory() {
+ public static StringIdentifierFactory stringSessionIdentifierFactory() {
return new StringSessionIdentifierFactory();
}
@@ -317,7 +315,7 @@
*
* @author Stephen Colebourne
*/
- private static class LongIdentifierFactory implements Factory, Serializable {
+ private static class LongNumericIdentifierFactory implements LongIdentifierFactory, Serializable {
/** Should the counter wrap. */
private final boolean wrap;
@@ -331,18 +329,27 @@
* long value (or throw an exception)
* @param initialValue the initial long value to start at
*/
- private LongIdentifierFactory(boolean wrap, long initialValue) {
+ private LongNumericIdentifierFactory(boolean wrap, long initialValue) {
super();
this.wrap = wrap;
this.count = initialValue;
}
/**
- * Create a new identifier.
+ * Gets the next new identifier.
+ *
+ * @return a new identifier as a Long
+ */
+ public Object nextIdentifier() {
+ return nextLongIdentifier();
+ }
+
+ /**
+ * Gets the next new identifier.
*
* @return a new identifier as a Long
*/
- public Object create() {
+ public Long nextLongIdentifier() {
long value = 0;
if (wrap) {
synchronized (this) {
@@ -351,7 +358,7 @@
} else {
synchronized (this) {
if (count == Long.MAX_VALUE) {
- throw new FactoryException("The maximum number of identifiers has been reached");
+ throw new IllegalStateException("The maximum number of identifiers has been reached");
}
value = count++;
}
@@ -368,7 +375,7 @@
*
* @author Stephen Colebourne
*/
- private static class StringNumericIdentifierFactory implements Factory, Serializable {
+ private static class StringNumericIdentifierFactory implements StringIdentifierFactory, Serializable {
/** Should the counter wrap. */
private final boolean wrap;
@@ -389,11 +396,20 @@
}
/**
- * Create a new identifier.
+ * Gets the next new identifier.
*
* @return a new identifier as a String
*/
- public Object create() {
+ public Object nextIdentifier() {
+ return nextStringIdentifier();
+ }
+
+ /**
+ * Gets the next new identifier.
+ *
+ * @return a new identifier as a String
+ */
+ public String nextStringIdentifier() {
long value = 0;
if (wrap) {
synchronized (this) {
@@ -402,7 +418,7 @@
} else {
synchronized (this) {
if (count == Long.MAX_VALUE) {
- throw new FactoryException("The maximum number of identifiers has been reached");
+ throw new IllegalStateException("The maximum number of identifiers has been reached");
}
value = count++;
}
@@ -421,7 +437,7 @@
*
* @author Stephen Colebourne
*/
- private static class StringAlphanumericIdentifierFactory implements Factory, Serializable {
+ private static class StringAlphanumericIdentifierFactory implements StringIdentifierFactory, Serializable {
/** Should the counter wrap. */
private final boolean wrap;
@@ -448,17 +464,26 @@
}
/**
- * Create a new identifier.
+ * Gets the next new identifier.
+ *
+ * @return a new identifier as a String
+ */
+ public Object nextIdentifier() {
+ return nextStringIdentifier();
+ }
+
+ /**
+ * Gets the next new identifier.
*
* @return a new identifier as a String
*/
- public synchronized Object create() {
+ public synchronized String nextStringIdentifier() {
for (int i = count.length - 1; i >= 0; i--) {
switch (count[i]) {
case 122: // z
count[i] = '0';
if (i == 0 && wrap == false) {
- throw new FactoryException("The maximum number of identifiers has been reached");
+ throw new IllegalStateException("The maximum number of identifiers has been reached");
}
break;
@@ -493,7 +518,7 @@
* @author Neeme Praks
* @author Stephen Colebourne
*/
- private static class StringSessionIdentifierFactory implements Factory, Serializable {
+ private static class StringSessionIdentifierFactory implements StringIdentifierFactory, Serializable {
/**
* We want to have a random string with a length of 6 characters.
@@ -527,17 +552,26 @@
private StringSessionIdentifierFactory() {
super();
}
+
+ /**
+ * Gets the next identifier.
+ *
+ * @return the next 10 char String identifier
+ */
+ public Object nextIdentifier() {
+ return nextStringIdentifier();
+ }
/**
- * Create a new identifier. Only guaranteed unique within
+ * Gets the next 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
+ * @return the next 10 char String identifier
*/
- public Object create() {
+ public String nextStringIdentifier() {
// Random value
//--------------
long currentRandom = randomizer.nextLong();
1.1 jakarta-commons/lang/src/java/org/apache/commons/lang/util/StringIdentifierFactory.java
Index: StringIdentifierFactory.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following 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.lang.util;
/**
* <p><code>StringIdentifierFactory</code> defines a simple interface for
* String based identifier generation.</p>
*
* @author Stephen Colebourne
* @since 2.0
* @version $Id: StringIdentifierFactory.java,v 1.1 2003/05/16 22:06:43 scolebourne Exp $
*/
public interface StringIdentifierFactory extends IdentifierFactory {
/**
* <p>Gets the next identifier in the sequence.</p>
*
* @return the next String identifier in sequence
*/
String nextStringIdentifier();
}
1.1 jakarta-commons/lang/src/java/org/apache/commons/lang/util/IdentifierFactory.java
Index: IdentifierFactory.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following 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.lang.util;
/**
* <p><code>IdentifierFactory</code> defines a simple interface for
* identifier generation.</p>
*
* @author Stephen Colebourne
* @since 2.0
* @version $Id: IdentifierFactory.java,v 1.1 2003/05/16 22:06:43 scolebourne Exp $
*/
public interface IdentifierFactory {
/**
* <p>Gets the next identifier in the sequence.</p>
*
* @return the next identifier in sequence
*/
Object nextIdentifier();
}
1.1 jakarta-commons/lang/src/java/org/apache/commons/lang/util/LongIdentifierFactory.java
Index: LongIdentifierFactory.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following 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.lang.util;
/**
* <p><code>LongIdentifierFactory</code> defines a simple interface for
* Long based identifier generation.</p>
*
* @author Stephen Colebourne
* @since 2.0
* @version $Id: LongIdentifierFactory.java,v 1.1 2003/05/16 22:06:43 scolebourne Exp $
*/
public interface LongIdentifierFactory extends IdentifierFactory {
/**
* <p>Gets the next identifier in the sequence.</p>
*
* @return the next Long identifier in sequence
*/
Long nextLongIdentifier();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org