View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.id;
18  
19  import java.io.Serializable;
20  import java.util.Collection;
21  import java.util.Iterator;
22  
23  /**
24   * Identifier generator that concatenates the results of a list of string
25   * identifier generators.
26   * 
27   * @since 1.0
28   * @version $Revision$ $Date$
29   * 
30   */
31  public class CompositeIdentifierGenerator extends AbstractStringIdentifierGenerator implements Serializable {
32  
33      /**
34       * <code>serialVersionUID</code> is the serializable UID for the binary version of the class.
35       */
36      private static final long serialVersionUID = 20060206L;
37      
38      /** The identifier generators to concatenate */
39      private final StringIdentifierGenerator[] identifierGenerators;
40  
41      /**
42       * Factory method to create a new <code>CompositeIdentifierGenerator</code>
43       * from an input array of <code>StringIdentifierGenerator</code> instances.
44       * <p>
45       * The input array is (shallow) copied - i.e., the object references in the
46       * input array are copied into a new array used internally.  The array is
47       * expected to be non-empty and not to contain nulls.
48       * 
49       * @param generators the identifiers to concatenate, copied by reference
50       * @return the composite identifier generator
51       * @throws IllegalArgumentException if the generators array is null
52       * @throws IllegalArgumentException if any generator in the array is null
53       */
54      public static StringIdentifierGenerator getInstance(
55              StringIdentifierGenerator[] generators) {
56          if (generators == null) {
57              throw new IllegalArgumentException(
58                      "Generator array must not be null");
59          }
60          if (generators.length == 0) {
61              throw new IllegalArgumentException(
62                      "Generator array must not be empty");
63          }
64          StringIdentifierGenerator[] generatorsCopy = 
65              new StringIdentifierGenerator[generators.length];
66          for (int i = 0; i < generators.length; i++) {
67              if (generators[i] == null) {
68                  throw new IllegalArgumentException(
69                          "Generators must not be null");
70              }
71              generatorsCopy[i] = generators[i];
72          }  
73          return new CompositeIdentifierGenerator(generatorsCopy);
74      }
75  
76      /**
77       * Create a new <code>CompositeIdentifierGenerator</code> that concatenates
78       * the results of the provided collection of generators. Order is
79       * determined by the <code>iterator()</code> method on the collection.
80       * 
81       * @param generators a collection of string identifier generators to
82       * concatenate
83       * @return the composite identifier generator
84       * @throws IllegalArgumentException if the generators collection is null,
85       * empty, or contains nulls
86       */
87      public static StringIdentifierGenerator getInstance(Collection generators) {
88          if (generators == null) {
89              throw new IllegalArgumentException(
90                      "Generator collection must not be null");
91          }
92          if (generators.size() == 0) {
93              throw new IllegalArgumentException(
94                      "Generator collection must not be empty");
95          }
96          StringIdentifierGenerator[] generatorsCopy = 
97              new StringIdentifierGenerator[generators.size()];
98          int i = 0;
99          Iterator it = generators.iterator();
100         while (it.hasNext()) {
101             generatorsCopy[i] = (StringIdentifierGenerator) it.next();
102             if (generatorsCopy[i] == null) {
103                 throw new IllegalArgumentException(
104                         "Generators must not be null");
105             }
106             i++;
107         }
108         return new CompositeIdentifierGenerator(generatorsCopy);
109     }
110 
111     
112     /**
113      * Constructor that does not check for nulls. Use 
114      * {@link #getInstance(StringIdentifierGenerator[])}
115      * to validate the input array.
116      * 
117      * @param identifierGenerators the identifier generators to concatenate
118      */
119     public CompositeIdentifierGenerator(
120             StringIdentifierGenerator[] identifierGenerators) {
121         super();
122         this.identifierGenerators = identifierGenerators;
123     }
124 
125     public String nextStringIdentifier() {
126         StringBuffer buffer = new StringBuffer();
127         for (int i = 0; i < identifierGenerators.length; i++) {
128             buffer.append(identifierGenerators[i].nextStringIdentifier());
129         }
130         return buffer.toString();
131     }
132 
133     public long maxLength() {
134         long length = 0;
135         for (int i = 0; i < identifierGenerators.length; i++) {
136             length += identifierGenerators[i].maxLength();
137         }
138         return length;
139     }
140 
141     public long minLength() {
142         long length = 0;
143         for (int i = 0; i < identifierGenerators.length; i++) {
144             length += identifierGenerators[i].minLength();
145         }
146         return length;
147     }
148 
149     /**
150      * Returns a (shallow) copy of the array of identifier generators
151      * concatenated by this generator.
152      * 
153      * @return the identifier generators
154      */
155     public StringIdentifierGenerator[] getIdentifierGenerators() {
156         int len = identifierGenerators.length;
157         StringIdentifierGenerator[] out = 
158             new StringIdentifierGenerator[len];
159         System.arraycopy(identifierGenerators, 0, out, 0, len);
160         return out;
161     }
162 
163 }