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/01/29 21:17:17 UTC
svn commit: r373349 - in /jakarta/commons/sandbox/id/trunk/src:
java/org/apache/commons/id/CompositeIdentifierGenerator.java
test/org/apache/commons/id/CompositeIdentifierGeneratorTest.java
Author: psteitz
Date: Sun Jan 29 12:16:37 2006
New Revision: 373349
URL: http://svn.apache.org/viewcvs?rev=373349&view=rev
Log:
Added CompositeIdentifierGenerator.
Added:
jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/CompositeIdentifierGenerator.java
jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/CompositeIdentifierGeneratorTest.java
Added: jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/CompositeIdentifierGenerator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/CompositeIdentifierGenerator.java?rev=373349&view=auto
==============================================================================
--- jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/CompositeIdentifierGenerator.java (added)
+++ jakarta/commons/sandbox/id/trunk/src/java/org/apache/commons/id/CompositeIdentifierGenerator.java Sun Jan 29 12:16:37 2006
@@ -0,0 +1,140 @@
+/*
+ * 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 java.util.Collection;
+import java.util.Iterator;
+
+/**
+ * Identifier generator that concatenates the results of a list of string
+ * identifier generators.
+ *
+ * @since 1.0
+ * @version $Revision$ $Date$
+ *
+ */
+public class CompositeIdentifierGenerator extends AbstractStringIdentifierGenerator {
+
+ /** The identifier generators to concatenate */
+ private final StringIdentifierGenerator[] identifierGenerators;
+
+ /**
+ * Factory method to create a new <code>CompositeIdentifierGenerator</code>
+ * from an input array of <code>StringIdentifierGenerator</code> instances.
+ * <p>
+ * The input array is (shallow) copied - i.e., the object references in the
+ * input array are copied into a new array used internally. The array is
+ * expected to be non-empty and not to contain nulls.
+ *
+ * @param generators the identifiers to concatenate, copied by reference
+ * @return the composite identifier generator
+ * @throws IllegalArgumentException if the generators array is null
+ * @throws IllegalArgumentException if any generator in the array is null
+ */
+ public static StringIdentifierGenerator getInstance(
+ StringIdentifierGenerator[] generators) {
+ if (generators == null) {
+ throw new IllegalArgumentException(
+ "Generator array must not be null");
+ }
+ if (generators.length == 0) {
+ throw new IllegalArgumentException(
+ "Generator array must not be empty");
+ }
+ StringIdentifierGenerator[] generatorsCopy =
+ new StringIdentifierGenerator[generators.length];
+ for (int i = 0; i < generators.length; i++) {
+ if (generators[i] == null) {
+ throw new IllegalArgumentException(
+ "Generators must not be null");
+ }
+ generatorsCopy[i] = generators[i];
+ }
+ return new CompositeIdentifierGenerator(generatorsCopy);
+ }
+
+ /**
+ * Create a new <code>CompositeIdentifierGenerator</code> that concatenates
+ * the results of the provided collection of generators. Order is
+ * determined by the <code>iterator()</code> method on the collection.
+ *
+ * @param generators a collection of string identifier generators to
+ * concatenate
+ * @return the composite identifier generator
+ * @throws IllegalArgumentException if the generators collection is null,
+ * empty, or contains nulls
+ */
+ public static StringIdentifierGenerator getInstance(Collection generators) {
+ if (generators == null) {
+ throw new IllegalArgumentException(
+ "Generator collection must not be null");
+ }
+ if (generators.size() == 0) {
+ throw new IllegalArgumentException(
+ "Generator collection must not be empty");
+ }
+ StringIdentifierGenerator[] generatorsCopy =
+ new StringIdentifierGenerator[generators.size()];
+ int i = 0;
+ Iterator it = generators.iterator();
+ while (it.hasNext()) {
+ generatorsCopy[i] = (StringIdentifierGenerator) it.next();
+ if (generatorsCopy[i] == null) {
+ throw new IllegalArgumentException(
+ "Generators must not be null");
+ }
+ i++;
+ }
+ return new CompositeIdentifierGenerator(generatorsCopy);
+ }
+
+
+ /**
+ * Constructor that does not check for nulls. Use
+ * {@link #getInstance(StringIdentifierGenerator[])}
+ * to validate the input array.
+ *
+ * @param identifierGenerators the identifier generators to concatenate
+ */
+ public CompositeIdentifierGenerator(
+ StringIdentifierGenerator[] identifierGenerators) {
+ super();
+ this.identifierGenerators = identifierGenerators;
+ }
+
+ public String nextStringIdentifier() {
+ StringBuffer buffer = new StringBuffer();
+ for (int i = 0; i < identifierGenerators.length; i++) {
+ buffer.append(identifierGenerators[i].nextStringIdentifier());
+ }
+ return buffer.toString();
+ }
+
+ /**
+ * Returns a (shallow) copy of the array of identifier generators
+ * concatenated by this generator.
+ *
+ * @return the identifier generators
+ */
+ public StringIdentifierGenerator[] getIdentifierGenerators() {
+ int len = identifierGenerators.length;
+ StringIdentifierGenerator[] out =
+ new StringIdentifierGenerator[len];
+ System.arraycopy(identifierGenerators, 0, out, 0, len);
+ return out;
+ }
+
+}
Added: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/CompositeIdentifierGeneratorTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/CompositeIdentifierGeneratorTest.java?rev=373349&view=auto
==============================================================================
--- jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/CompositeIdentifierGeneratorTest.java (added)
+++ jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/CompositeIdentifierGeneratorTest.java Sun Jan 29 12:16:37 2006
@@ -0,0 +1,129 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.commons.id.serial.NumericGenerator;
+import org.apache.commons.id.serial.AlphanumericGenerator;
+
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Modifier;
+
+/**
+ * Tests the org.apache.commons.id.CompositeIdentifierGenerator class.
+ *
+ * @author Commons-id team
+ * @version $Id$
+ */
+public class CompositeIdentifierGeneratorTest extends junit.framework.TestCase {
+
+
+ public void testGetInstanceArrayInput() {
+ // positive test
+ CompositeIdentifierGenerator gen = (CompositeIdentifierGenerator)
+ CompositeIdentifierGenerator.getInstance(makeTestArray());
+ assertEquals(2, gen.getIdentifierGenerators().length);
+
+ // null array
+ StringIdentifierGenerator[] generators = null;
+ try {
+ CompositeIdentifierGenerator.getInstance(generators);
+ fail("Expecting IllegalArgumentException for null array");
+ } catch (IllegalArgumentException ex) {}
+
+ // empty array
+ try {
+ CompositeIdentifierGenerator.getInstance(
+ new StringIdentifierGenerator[] {});
+ fail("Expecting IllegalArgumentException for empty array");
+ } catch (IllegalArgumentException ex) {}
+
+ // nulls in array
+ generators = makeTestArray();
+ try {
+ generators[0] = null;
+ CompositeIdentifierGenerator.getInstance(generators);
+ fail("Expecting IllegalArgumentException for array with nulls");
+ } catch (IllegalArgumentException ex) {}
+ generators = makeTestArray();
+ try {
+ generators[1] = null;
+ CompositeIdentifierGenerator.getInstance(generators);
+ fail("Expecting IllegalArgumentException for array with nulls");
+ } catch (IllegalArgumentException ex) {}
+ }
+
+ public void testGetInstanceCollectionInput() {
+ // positive test
+ CompositeIdentifierGenerator gen = (CompositeIdentifierGenerator)
+ CompositeIdentifierGenerator.getInstance(makeTestCollection());
+ assertEquals(2, gen.getIdentifierGenerators().length);
+
+ // null collection
+ ArrayList generators = null;
+ try {
+ CompositeIdentifierGenerator.getInstance(generators);
+ fail("Expecting IllegalArgumentException for null collection");
+ } catch (IllegalArgumentException ex) {}
+
+ // empty collection
+ try {
+ CompositeIdentifierGenerator.getInstance(
+ new ArrayList());
+ fail("Expecting IllegalArgumentException for empty collection");
+ } catch (IllegalArgumentException ex) {}
+
+ // nulls in collection
+ generators = makeTestCollection();
+ try {
+ generators.add(0, null);
+ CompositeIdentifierGenerator.getInstance(generators);
+ fail("Expecting IllegalArgumentException for collection with nulls");
+ } catch (IllegalArgumentException ex) {}
+ generators = makeTestCollection();
+ try {
+ generators.add(2, null);
+ CompositeIdentifierGenerator.getInstance(generators);
+ fail("Expecting IllegalArgumentException for array with nulls");
+ } catch (IllegalArgumentException ex) {}
+ }
+
+ public void testNextStringIdentifier() {
+ StringIdentifierGenerator generator =
+ CompositeIdentifierGenerator.getInstance(makeTestArray());
+ assertEquals("0000000000000001",
+ generator.nextStringIdentifier());
+ assertEquals("1000000000000002",
+ generator.nextStringIdentifier());
+ assertEquals("2000000000000003",
+ generator.nextStringIdentifier());
+ }
+
+ protected StringIdentifierGenerator[] makeTestArray() {
+ StringIdentifierGenerator[] out =
+ {new NumericGenerator(false, 0),
+ new AlphanumericGenerator(true)};
+ return out;
+ }
+
+ protected ArrayList makeTestCollection() {
+ return new ArrayList(Arrays.asList(makeTestArray()));
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org