You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2006/02/25 21:54:39 UTC
svn commit: r380993 - in /jakarta/commons/sandbox/id/trunk/src:
java/org/apache/commons/id/ConstantIdentifierGenerator.java
test/org/apache/commons/id/ConstantIdentifierGeneratorTest.java
Author: psteitz
Date: Sat Feb 25 12:54:36 2006
New Revision: 380993
URL: http://svn.apache.org/viewcvs?rev=380993&view=rev
Log:
Added ConstantIdentifierGenerator.
Added:
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/ConstantIdentifierGenerator.java (with props)
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/ConstantIdentifierGeneratorTest.java (with props)
Added: jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/ConstantIdentifierGenerator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/ConstantIdentifierGenerator.java?rev=380993&view=auto
==============================================================================
--- jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/ConstantIdentifierGenerator.java (added)
+++ jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/ConstantIdentifierGenerator.java Sat Feb 25 12:54:36 2006
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.id;
+
+/**
+ * <code>StringIdentifierGenerator</code> that always returns the same
+ * string. Use with {@link CompositeIdentifierGenerator} to embed constant
+ * string identifiers, or to add prefixes or suffixes.
+ * <p>
+ * Null constant values are not allowed. The default
+ * (assumed by the argumentless constructor) is an empty string.</p>
+ *
+ * @author Commons-Id team
+ * @version $Id$
+ */
+public class ConstantIdentifierGenerator extends
+ AbstractStringIdentifierGenerator {
+
+ /**
+ * Constant string value always returned by this generator.
+ */
+ private final String identifier;
+
+ /**
+ * Factory method to create a new <code>ConstantIdentifierGenerator</code>
+ * with the given constant value. Does not allow null values.
+ *
+ * @param identifier constant value returned by the newly created generator.
+ * Must not be null.
+ * @return a new ConstantIdentifierGenerator
+ * @throws IllegalArgumentException if identifier is null
+ */
+ public static StringIdentifierGenerator getInstance(String identifier) {
+ return new ConstantIdentifierGenerator(identifier);
+ }
+
+ /**
+ * Creates a new ConstantIdentifierGenerator using the
+ * default (empty string) constant value.
+ *
+ */
+ public ConstantIdentifierGenerator() {
+ super();
+ this.identifier = "";
+ }
+
+ /**
+ * Creates a new ConstantIdentifierGenerator using the
+ * given constant value.
+ * <p>
+ * Null constant values are not allowed.</p>
+ *
+ * @param identifier constant value returned by the generator. Must not
+ * be null.
+ * @throws IllegalArgumentExeption if identifier is null
+ */
+ public ConstantIdentifierGenerator(String identifier) {
+ super();
+ if (identifier == null) {
+ throw new IllegalArgumentException
+ ("Constant identifier value must not be null");
+ }
+ this.identifier = identifier;
+ }
+
+ public String nextStringIdentifier() {
+ return identifier;
+ }
+
+ /**
+ * Returns the length of the constant string returned by this generator.
+ * If the constant is null or an empty string, 0 is returned.
+ *
+ * @return the length of the constant string returned by this generator
+ */
+ public long maxLength() {
+ if (identifier == null) {
+ return 0;
+ } else {
+ return identifier.length();
+ }
+ }
+
+ /**
+ * Returns the length of the constant string returned by this generator.
+ * If the constant is null or an empty string, 0 is returned.
+ *
+ * @return the length of the constant string returned by this generator
+ */
+ public long minLength() {
+ return maxLength();
+ }
+}
Propchange: jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/ConstantIdentifierGenerator.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/ConstantIdentifierGeneratorTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/ConstantIdentifierGeneratorTest.java?rev=380993&view=auto
==============================================================================
--- jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/ConstantIdentifierGeneratorTest.java (added)
+++ jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/ConstantIdentifierGeneratorTest.java Sat Feb 25 12:54:36 2006
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.id;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.id.serial.AlphanumericGenerator;
+import org.apache.commons.id.serial.NumericGenerator;
+import org.apache.commons.id.test.AssertSerialization;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+/**
+ * Tests the org.apache.commons.id.ConstantIdentifierGenerator class.
+ *
+ * @author Commons-id team
+ * @version $Id$
+ */
+public class ConstantIdentifierGeneratorTest extends TestCase {
+
+ protected String testConstant = "testConstant";
+
+ public void testGetInstance() {
+ StringIdentifierGenerator identifier =
+ ConstantIdentifierGenerator.getInstance(testConstant);
+ assertEquals(testConstant,identifier.nextStringIdentifier());
+ }
+
+ public void testNextStringIdentifier() {
+ ConstantIdentifierGenerator identifier =
+ new ConstantIdentifierGenerator(testConstant);
+ assertEquals(testConstant,identifier.nextStringIdentifier());
+ }
+
+ public void testNullConstant() {
+ try {
+ ConstantIdentifierGenerator identifier =
+ new ConstantIdentifierGenerator(null);
+ fail("Expecting IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ StringIdentifierGenerator identifier =
+ ConstantIdentifierGenerator.getInstance(null);
+ fail("Expecting IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+ public void testEmptyConstant() {
+ ConstantIdentifierGenerator identifier =
+ new ConstantIdentifierGenerator("");
+ assertEquals("",identifier.nextStringIdentifier());
+ }
+
+ public void testDefault() {
+ ConstantIdentifierGenerator identifier =
+ new ConstantIdentifierGenerator();
+ assertEquals("",identifier.nextStringIdentifier());
+ }
+
+ public void testMaxMinLength() {
+ ConstantIdentifierGenerator identifier =
+ new ConstantIdentifierGenerator(testConstant);
+ long len = testConstant.length();
+ assertEquals(len, identifier.maxLength());
+ assertEquals(len, identifier.minLength());
+
+ identifier = new ConstantIdentifierGenerator("");
+ assertEquals(0, identifier.maxLength());
+ assertEquals(0, identifier.minLength());
+ }
+}
Propchange: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/ConstantIdentifierGeneratorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org