Change Identifier generation code to be independent of functors

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137319 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-05-16 22:07:38 +00:00
parent 9e0419790b
commit 70cab8f8e6
5 changed files with 455 additions and 204 deletions

View File

@ -0,0 +1,73 @@
/* ====================================================================
* 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();
}

View File

@ -56,19 +56,18 @@
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
* @version $Id: IdentifierUtils.java,v 1.4 2003/04/09 01:04:48 ggregory Exp $
* @version $Id: IdentifierUtils.java,v 1.5 2003/05/16 22:06:43 scolebourne Exp $
*/
public class IdentifierUtils {
@ -87,7 +86,7 @@ public class IdentifierUtils {
* <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 @@ public class IdentifierUtils {
* <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 @@ public class IdentifierUtils {
* <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 @@ public class IdentifierUtils {
*
* <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 @@ public IdentifierUtils() {
* @return a new identifier
*/
public static Long nextLongIdentifier() {
return (Long) LONG_IDENTIFIER_FACTORY.create();
return LONG_IDENTIFIER_FACTORY.nextLongIdentifier();
}
/**
@ -178,8 +177,8 @@ public static Long nextLongIdentifier() {
*
* @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 @@ public static Factory longIdentifierFactory() {
* 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 @@ public static Factory longIdentifierFactory(boolean wrap, long initialValue) {
* @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 @@ public static String nextStringNumericIdentifier() {
*
* @return a new identifier factory
*/
public static Factory stringNumericIdentifierFactory() {
public static StringIdentifierFactory stringNumericIdentifierFactory() {
return new StringNumericIdentifierFactory(true, 0L);
}
@ -229,11 +228,11 @@ public static Factory stringNumericIdentifierFactory() {
* 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 @@ public static Factory stringNumericIdentifierFactory(boolean wrap, long initialV
* @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 @@ public static String nextStringAlphanumericIdentifier() {
*
* @return a new identifier factory
*/
public static Factory stringAlphanumericIdentifierFactory() {
public static StringIdentifierFactory stringAlphanumericIdentifierFactory() {
return new StringAlphanumericIdentifierFactory(true, 15);
}
@ -270,11 +269,11 @@ public static Factory stringAlphanumericIdentifierFactory() {
* 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 @@ public static Factory stringAlphanumericIdentifierFactory(boolean wrap, int size
* 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 @@ public static String nextStringSessionIdentifier() {
*
* @return a new identifier factory
*/
public static Factory stringSessionIdentifierFactory() {
public static StringIdentifierFactory stringSessionIdentifierFactory() {
return new StringSessionIdentifierFactory();
}
@ -317,7 +315,7 @@ public static Factory stringSessionIdentifierFactory() {
*
* @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 @@ private static class LongIdentifierFactory implements Factory, Serializable {
* 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 create() {
public Object nextIdentifier() {
return nextLongIdentifier();
}
/**
* Gets the next new identifier.
*
* @return a new identifier as a Long
*/
public Long nextLongIdentifier() {
long value = 0;
if (wrap) {
synchronized (this) {
@ -351,7 +358,7 @@ public Object create() {
} 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 @@ public Object create() {
*
* @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 @@ private StringNumericIdentifierFactory(boolean wrap, long initialValue) {
}
/**
* 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 @@ public Object create() {
} 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 @@ public Object create() {
*
* @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 @@ private StringAlphanumericIdentifierFactory(boolean wrap, int size) {
}
/**
* Create a new identifier.
* Gets the next new identifier.
*
* @return a new identifier as a String
*/
public synchronized Object create() {
public Object nextIdentifier() {
return nextStringIdentifier();
}
/**
* Gets the next new identifier.
*
* @return a new identifier as a String
*/
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 @@ public synchronized Object create() {
* @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 static class StringSessionIdentifierFactory implements Factory, Serializ
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();

View File

@ -0,0 +1,73 @@
/* ====================================================================
* 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();
}

View File

@ -0,0 +1,73 @@
/* ====================================================================
* 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();
}

View File

@ -56,13 +56,11 @@
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.lang.functor.Factory;
import org.apache.commons.lang.functor.FactoryException;
/**
* Tests the org.apache.commons.lang.util.IdentifierUtils class.
*
* @author Stephen Colebourne
* @version $Id: IdentifierUtilsTest.java,v 1.1 2002/12/29 21:35:03 scolebourne Exp $
* @version $Id: IdentifierUtilsTest.java,v 1.2 2003/05/16 22:07:38 scolebourne Exp $
*/
public class IdentifierUtilsTest extends junit.framework.TestCase {
@ -85,227 +83,227 @@ public static Test suite() {
//--------------------------------------------------------------------------
public void testLongIncrementing() {
Factory f = IdentifierUtils.LONG_IDENTIFIER_FACTORY;
assertEquals(new Long(0), f.create());
assertEquals(new Long(1), f.create());
assertEquals(new Long(2), f.create());
assertEquals(new Long(3), f.create());
LongIdentifierFactory f = IdentifierUtils.LONG_IDENTIFIER_FACTORY;
assertEquals(new Long(0), f.nextLongIdentifier());
assertEquals(new Long(1), f.nextLongIdentifier());
assertEquals(new Long(2), f.nextIdentifier());
assertEquals(new Long(3), f.nextLongIdentifier());
assertEquals(new Long(4), IdentifierUtils.nextLongIdentifier());
assertEquals(new Long(5), f.create());
assertEquals(new Long(5), f.nextLongIdentifier());
assertEquals(new Long(6), IdentifierUtils.nextLongIdentifier());
assertEquals(new Long(7), IdentifierUtils.nextLongIdentifier());
}
public void testLongIncrementingNoArgs() {
Factory f = IdentifierUtils.longIdentifierFactory();
assertEquals(new Long(0), f.create());
assertEquals(new Long(1), f.create());
LongIdentifierFactory f = IdentifierUtils.longIdentifierFactory();
assertEquals(new Long(0), f.nextLongIdentifier());
assertEquals(new Long(1), f.nextLongIdentifier());
assertTrue(f != IdentifierUtils.LONG_IDENTIFIER_FACTORY);
}
public void testLongIncrementingInit() {
Factory f = IdentifierUtils.longIdentifierFactory(true, 100);
assertEquals(new Long(100), f.create());
assertEquals(new Long(101), f.create());
LongIdentifierFactory f = IdentifierUtils.longIdentifierFactory(true, 100);
assertEquals(new Long(100), f.nextLongIdentifier());
assertEquals(new Long(101), f.nextLongIdentifier());
}
public void testLongIncrementingWrap() {
Factory f = IdentifierUtils.longIdentifierFactory(true, Long.MAX_VALUE);
assertEquals(new Long(Long.MAX_VALUE), f.create());
assertEquals(new Long(Long.MIN_VALUE), f.create());
LongIdentifierFactory f = IdentifierUtils.longIdentifierFactory(true, Long.MAX_VALUE);
assertEquals(new Long(Long.MAX_VALUE), f.nextLongIdentifier());
assertEquals(new Long(Long.MIN_VALUE), f.nextLongIdentifier());
}
public void testLongIncrementingNoWrap() {
Factory f = IdentifierUtils.longIdentifierFactory(false, Long.MAX_VALUE);
LongIdentifierFactory f = IdentifierUtils.longIdentifierFactory(false, Long.MAX_VALUE);
try {
f.create();
f.nextLongIdentifier();
fail();
} catch (FactoryException ex) {}
} catch (IllegalStateException ex) {}
}
//--------------------------------------------------------------------------
public void testStringNumericLong() {
Factory f = IdentifierUtils.STRING_NUMERIC_IDENTIFIER_FACTORY;
assertEquals("0", f.create());
assertEquals("1", f.create());
assertEquals("2", f.create());
assertEquals("3", f.create());
StringIdentifierFactory f = IdentifierUtils.STRING_NUMERIC_IDENTIFIER_FACTORY;
assertEquals("0", f.nextStringIdentifier());
assertEquals("1", f.nextStringIdentifier());
assertEquals("2", f.nextIdentifier());
assertEquals("3", f.nextStringIdentifier());
assertEquals("4", IdentifierUtils.nextStringNumericIdentifier());
assertEquals("5", f.create());
assertEquals("5", f.nextStringIdentifier());
assertEquals("6", IdentifierUtils.nextStringNumericIdentifier());
assertEquals("7", IdentifierUtils.nextStringNumericIdentifier());
}
public void testStringNumericNoArgs() {
Factory f = IdentifierUtils.stringNumericIdentifierFactory();
assertEquals("0", f.create());
assertEquals("1", f.create());
StringIdentifierFactory f = IdentifierUtils.stringNumericIdentifierFactory();
assertEquals("0", f.nextStringIdentifier());
assertEquals("1", f.nextStringIdentifier());
assertTrue(f != IdentifierUtils.STRING_NUMERIC_IDENTIFIER_FACTORY);
}
public void testStringNumericInit() {
Factory f = IdentifierUtils.stringNumericIdentifierFactory(true, 100);
assertEquals("100", f.create());
assertEquals("101", f.create());
StringIdentifierFactory f = IdentifierUtils.stringNumericIdentifierFactory(true, 100);
assertEquals("100", f.nextStringIdentifier());
assertEquals("101", f.nextStringIdentifier());
}
public void testStringNumericWrap() {
Factory f = IdentifierUtils.stringNumericIdentifierFactory(true, Long.MAX_VALUE);
assertEquals(Long.toString(Long.MAX_VALUE), f.create());
assertEquals(Long.toString(Long.MIN_VALUE), f.create());
StringIdentifierFactory f = IdentifierUtils.stringNumericIdentifierFactory(true, Long.MAX_VALUE);
assertEquals(Long.toString(Long.MAX_VALUE), f.nextStringIdentifier());
assertEquals(Long.toString(Long.MIN_VALUE), f.nextStringIdentifier());
}
public void testStringNumericNoWrap() {
Factory f = IdentifierUtils.stringNumericIdentifierFactory(false, Long.MAX_VALUE);
StringIdentifierFactory f = IdentifierUtils.stringNumericIdentifierFactory(false, Long.MAX_VALUE);
try {
f.create();
f.nextStringIdentifier();
fail();
} catch (FactoryException ex) { }
} catch (IllegalStateException ex) { }
}
//--------------------------------------------------------------------------
public void testStringAlphanumeric() {
Factory f = IdentifierUtils.STRING_ALPHANUMERIC_IDENTIFIER_FACTORY;
assertEquals("000000000000001", f.create());
assertEquals("000000000000002", f.create());
assertEquals("000000000000003", f.create());
assertEquals("000000000000004", f.create());
assertEquals("000000000000005", f.create());
assertEquals("000000000000006", f.create());
assertEquals("000000000000007", f.create());
assertEquals("000000000000008", f.create());
assertEquals("000000000000009", f.create());
assertEquals("00000000000000a", f.create());
assertEquals("00000000000000b", f.create());
assertEquals("00000000000000c", f.create());
StringIdentifierFactory f = IdentifierUtils.STRING_ALPHANUMERIC_IDENTIFIER_FACTORY;
assertEquals("000000000000001", f.nextStringIdentifier());
assertEquals("000000000000002", f.nextStringIdentifier());
assertEquals("000000000000003", f.nextStringIdentifier());
assertEquals("000000000000004", f.nextStringIdentifier());
assertEquals("000000000000005", f.nextStringIdentifier());
assertEquals("000000000000006", f.nextStringIdentifier());
assertEquals("000000000000007", f.nextStringIdentifier());
assertEquals("000000000000008", f.nextStringIdentifier());
assertEquals("000000000000009", f.nextStringIdentifier());
assertEquals("00000000000000a", f.nextStringIdentifier());
assertEquals("00000000000000b", f.nextStringIdentifier());
assertEquals("00000000000000c", f.nextStringIdentifier());
assertEquals("00000000000000d", IdentifierUtils.nextStringAlphanumericIdentifier());
assertEquals("00000000000000e", f.create());
assertEquals("00000000000000f", f.create());
assertEquals("00000000000000g", f.create());
assertEquals("00000000000000h", f.create());
assertEquals("00000000000000i", f.create());
assertEquals("00000000000000j", f.create());
assertEquals("00000000000000k", f.create());
assertEquals("00000000000000l", f.create());
assertEquals("00000000000000m", f.create());
assertEquals("00000000000000n", f.create());
assertEquals("00000000000000o", f.create());
assertEquals("00000000000000p", f.create());
assertEquals("00000000000000q", f.create());
assertEquals("00000000000000r", f.create());
assertEquals("00000000000000s", f.create());
assertEquals("00000000000000t", f.create());
assertEquals("00000000000000u", f.create());
assertEquals("00000000000000v", f.create());
assertEquals("00000000000000w", f.create());
assertEquals("00000000000000x", f.create());
assertEquals("00000000000000y", f.create());
assertEquals("00000000000000z", f.create());
assertEquals("000000000000010", f.create());
assertEquals("000000000000011", f.create());
assertEquals("000000000000012", f.create());
assertEquals("000000000000013", f.create());
assertEquals("00000000000000e", f.nextStringIdentifier());
assertEquals("00000000000000f", f.nextStringIdentifier());
assertEquals("00000000000000g", f.nextStringIdentifier());
assertEquals("00000000000000h", f.nextStringIdentifier());
assertEquals("00000000000000i", f.nextStringIdentifier());
assertEquals("00000000000000j", f.nextStringIdentifier());
assertEquals("00000000000000k", f.nextStringIdentifier());
assertEquals("00000000000000l", f.nextStringIdentifier());
assertEquals("00000000000000m", f.nextStringIdentifier());
assertEquals("00000000000000n", f.nextStringIdentifier());
assertEquals("00000000000000o", f.nextStringIdentifier());
assertEquals("00000000000000p", f.nextStringIdentifier());
assertEquals("00000000000000q", f.nextStringIdentifier());
assertEquals("00000000000000r", f.nextStringIdentifier());
assertEquals("00000000000000s", f.nextStringIdentifier());
assertEquals("00000000000000t", f.nextStringIdentifier());
assertEquals("00000000000000u", f.nextStringIdentifier());
assertEquals("00000000000000v", f.nextStringIdentifier());
assertEquals("00000000000000w", f.nextStringIdentifier());
assertEquals("00000000000000x", f.nextStringIdentifier());
assertEquals("00000000000000y", f.nextStringIdentifier());
assertEquals("00000000000000z", f.nextStringIdentifier());
assertEquals("000000000000010", f.nextStringIdentifier());
assertEquals("000000000000011", f.nextStringIdentifier());
assertEquals("000000000000012", f.nextStringIdentifier());
assertEquals("000000000000013", f.nextStringIdentifier());
}
public void testLongAlphanumericNoArgs() {
Factory f = IdentifierUtils.stringAlphanumericIdentifierFactory();
assertEquals("000000000000001", f.create());
assertEquals("000000000000002", f.create());
StringIdentifierFactory f = IdentifierUtils.stringAlphanumericIdentifierFactory();
assertEquals("000000000000001", f.nextStringIdentifier());
assertEquals("000000000000002", f.nextStringIdentifier());
assertTrue(f != IdentifierUtils.STRING_ALPHANUMERIC_IDENTIFIER_FACTORY);
}
public void testStringAlphanumericWrap() {
Factory f = IdentifierUtils.stringAlphanumericIdentifierFactory(true, 1);
assertEquals("1", f.create());
assertEquals("2", f.create());
assertEquals("3", f.create());
assertEquals("4", f.create());
assertEquals("5", f.create());
assertEquals("6", f.create());
assertEquals("7", f.create());
assertEquals("8", f.create());
assertEquals("9", f.create());
assertEquals("a", f.create());
assertEquals("b", f.create());
assertEquals("c", f.create());
assertEquals("d", f.create());
assertEquals("e", f.create());
assertEquals("f", f.create());
assertEquals("g", f.create());
assertEquals("h", f.create());
assertEquals("i", f.create());
assertEquals("j", f.create());
assertEquals("k", f.create());
assertEquals("l", f.create());
assertEquals("m", f.create());
assertEquals("n", f.create());
assertEquals("o", f.create());
assertEquals("p", f.create());
assertEquals("q", f.create());
assertEquals("r", f.create());
assertEquals("s", f.create());
assertEquals("t", f.create());
assertEquals("u", f.create());
assertEquals("v", f.create());
assertEquals("w", f.create());
assertEquals("x", f.create());
assertEquals("y", f.create());
assertEquals("z", f.create());
assertEquals("0", f.create());
StringIdentifierFactory f = IdentifierUtils.stringAlphanumericIdentifierFactory(true, 1);
assertEquals("1", f.nextStringIdentifier());
assertEquals("2", f.nextStringIdentifier());
assertEquals("3", f.nextStringIdentifier());
assertEquals("4", f.nextStringIdentifier());
assertEquals("5", f.nextStringIdentifier());
assertEquals("6", f.nextStringIdentifier());
assertEquals("7", f.nextStringIdentifier());
assertEquals("8", f.nextStringIdentifier());
assertEquals("9", f.nextStringIdentifier());
assertEquals("a", f.nextStringIdentifier());
assertEquals("b", f.nextStringIdentifier());
assertEquals("c", f.nextStringIdentifier());
assertEquals("d", f.nextStringIdentifier());
assertEquals("e", f.nextStringIdentifier());
assertEquals("f", f.nextStringIdentifier());
assertEquals("g", f.nextStringIdentifier());
assertEquals("h", f.nextStringIdentifier());
assertEquals("i", f.nextStringIdentifier());
assertEquals("j", f.nextStringIdentifier());
assertEquals("k", f.nextStringIdentifier());
assertEquals("l", f.nextStringIdentifier());
assertEquals("m", f.nextStringIdentifier());
assertEquals("n", f.nextStringIdentifier());
assertEquals("o", f.nextStringIdentifier());
assertEquals("p", f.nextStringIdentifier());
assertEquals("q", f.nextStringIdentifier());
assertEquals("r", f.nextStringIdentifier());
assertEquals("s", f.nextStringIdentifier());
assertEquals("t", f.nextStringIdentifier());
assertEquals("u", f.nextStringIdentifier());
assertEquals("v", f.nextStringIdentifier());
assertEquals("w", f.nextStringIdentifier());
assertEquals("x", f.nextStringIdentifier());
assertEquals("y", f.nextStringIdentifier());
assertEquals("z", f.nextStringIdentifier());
assertEquals("0", f.nextStringIdentifier());
}
public void testStringAlphanumericNoWrap() {
Factory f = IdentifierUtils.stringAlphanumericIdentifierFactory(false, 1);
assertEquals("1", f.create());
assertEquals("2", f.create());
assertEquals("3", f.create());
assertEquals("4", f.create());
assertEquals("5", f.create());
assertEquals("6", f.create());
assertEquals("7", f.create());
assertEquals("8", f.create());
assertEquals("9", f.create());
assertEquals("a", f.create());
assertEquals("b", f.create());
assertEquals("c", f.create());
assertEquals("d", f.create());
assertEquals("e", f.create());
assertEquals("f", f.create());
assertEquals("g", f.create());
assertEquals("h", f.create());
assertEquals("i", f.create());
assertEquals("j", f.create());
assertEquals("k", f.create());
assertEquals("l", f.create());
assertEquals("m", f.create());
assertEquals("n", f.create());
assertEquals("o", f.create());
assertEquals("p", f.create());
assertEquals("q", f.create());
assertEquals("r", f.create());
assertEquals("s", f.create());
assertEquals("t", f.create());
assertEquals("u", f.create());
assertEquals("v", f.create());
assertEquals("w", f.create());
assertEquals("x", f.create());
assertEquals("y", f.create());
assertEquals("z", f.create());
StringIdentifierFactory f = IdentifierUtils.stringAlphanumericIdentifierFactory(false, 1);
assertEquals("1", f.nextStringIdentifier());
assertEquals("2", f.nextStringIdentifier());
assertEquals("3", f.nextStringIdentifier());
assertEquals("4", f.nextStringIdentifier());
assertEquals("5", f.nextStringIdentifier());
assertEquals("6", f.nextStringIdentifier());
assertEquals("7", f.nextStringIdentifier());
assertEquals("8", f.nextStringIdentifier());
assertEquals("9", f.nextStringIdentifier());
assertEquals("a", f.nextStringIdentifier());
assertEquals("b", f.nextStringIdentifier());
assertEquals("c", f.nextStringIdentifier());
assertEquals("d", f.nextStringIdentifier());
assertEquals("e", f.nextStringIdentifier());
assertEquals("f", f.nextStringIdentifier());
assertEquals("g", f.nextStringIdentifier());
assertEquals("h", f.nextStringIdentifier());
assertEquals("i", f.nextStringIdentifier());
assertEquals("j", f.nextStringIdentifier());
assertEquals("k", f.nextStringIdentifier());
assertEquals("l", f.nextStringIdentifier());
assertEquals("m", f.nextStringIdentifier());
assertEquals("n", f.nextStringIdentifier());
assertEquals("o", f.nextStringIdentifier());
assertEquals("p", f.nextStringIdentifier());
assertEquals("q", f.nextStringIdentifier());
assertEquals("r", f.nextStringIdentifier());
assertEquals("s", f.nextStringIdentifier());
assertEquals("t", f.nextStringIdentifier());
assertEquals("u", f.nextStringIdentifier());
assertEquals("v", f.nextStringIdentifier());
assertEquals("w", f.nextStringIdentifier());
assertEquals("x", f.nextStringIdentifier());
assertEquals("y", f.nextStringIdentifier());
assertEquals("z", f.nextStringIdentifier());
try {
f.create();
f.nextStringIdentifier();
fail();
} catch (FactoryException ex) {}
} catch (IllegalStateException ex) {}
}
//--------------------------------------------------------------------------
public void testStringSession() {
Factory f = IdentifierUtils.STRING_SESSION_IDENTIFIER_FACTORY;
StringIdentifierFactory f = IdentifierUtils.STRING_SESSION_IDENTIFIER_FACTORY;
assertTrue(f != IdentifierUtils.stringSessionIdentifierFactory());
String a = (String) f.create();
String a = (String) f.nextStringIdentifier();
String b = (String) IdentifierUtils.nextStringSessionIdentifier();
assertTrue(a.length() >= 10);
assertTrue(b.length() >= 10);