Coverage Report - org.apache.commons.id.CompositeIdentifierGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
CompositeIdentifierGenerator
100%
43/43
100%
22/22
3.429
 
 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  7
         if (generators == null) {
 57  1
             throw new IllegalArgumentException(
 58  
                     "Generator array must not be null");
 59  
         }
 60  6
         if (generators.length == 0) {
 61  1
             throw new IllegalArgumentException(
 62  
                     "Generator array must not be empty");
 63  
         }
 64  5
         StringIdentifierGenerator[] generatorsCopy = 
 65  
             new StringIdentifierGenerator[generators.length];
 66  12
         for (int i = 0; i < generators.length; i++) {
 67  9
             if (generators[i] == null) {
 68  2
                 throw new IllegalArgumentException(
 69  
                         "Generators must not be null");
 70  
             }
 71  7
             generatorsCopy[i] = generators[i];
 72  
         }  
 73  3
         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  5
         if (generators == null) {
 89  1
             throw new IllegalArgumentException(
 90  
                     "Generator collection must not be null");
 91  
         }
 92  4
         if (generators.size() == 0) {
 93  1
             throw new IllegalArgumentException(
 94  
                     "Generator collection must not be empty");
 95  
         }
 96  3
         StringIdentifierGenerator[] generatorsCopy = 
 97  
             new StringIdentifierGenerator[generators.size()];
 98  3
         int i = 0;
 99  3
         Iterator it = generators.iterator();
 100  7
         while (it.hasNext()) {
 101  6
             generatorsCopy[i] = (StringIdentifierGenerator) it.next();
 102  6
             if (generatorsCopy[i] == null) {
 103  2
                 throw new IllegalArgumentException(
 104  
                         "Generators must not be null");
 105  
             }
 106  4
             i++;
 107  4
         }
 108  1
         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  8
         super();
 122  8
         this.identifierGenerators = identifierGenerators;
 123  8
     }
 124  
 
 125  
     public String nextStringIdentifier() {
 126  3
         StringBuffer buffer = new StringBuffer();
 127  9
         for (int i = 0; i < identifierGenerators.length; i++) {
 128  6
             buffer.append(identifierGenerators[i].nextStringIdentifier());
 129  
         }
 130  3
         return buffer.toString();
 131  
     }
 132  
 
 133  
     public long maxLength() {
 134  1
         long length = 0;
 135  3
         for (int i = 0; i < identifierGenerators.length; i++) {
 136  2
             length += identifierGenerators[i].maxLength();
 137  
         }
 138  1
         return length;
 139  
     }
 140  
 
 141  
     public long minLength() {
 142  1
         long length = 0;
 143  3
         for (int i = 0; i < identifierGenerators.length; i++) {
 144  2
             length += identifierGenerators[i].minLength();
 145  
         }
 146  1
         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  6
         int len = identifierGenerators.length;
 157  6
         StringIdentifierGenerator[] out = 
 158  
             new StringIdentifierGenerator[len];
 159  6
         System.arraycopy(identifierGenerators, 0, out, 0, len);
 160  6
         return out;
 161  
     }
 162  
 
 163  
 }