Coverage Report - org.apache.commons.id.serial.PrefixedAlphanumericGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
PrefixedAlphanumericGenerator
93%
13/14
83%
5/6
1.667
 
 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.serial;
 18  
 
 19  
 /**
 20  
  * <code>PrefixedAlphanumericGenerator</code> is an identifier generator
 21  
  * that generates an incrementing number in base 36 with a prefix as a String
 22  
  * object.
 23  
  *
 24  
  * <p>All generated ids have the same length (prefixed and padded with 0's
 25  
  * on the left), which is determined by the <code>size</code> parameter passed
 26  
  * to the constructor.<p>
 27  
  *
 28  
  * <p>The <code>wrap</code> property determines whether or not the sequence wraps
 29  
  * when it reaches the largest value that can be represented in <code>size</code>
 30  
  * base 36 digits. If <code>wrap</code> is false and the the maximum representable
 31  
  * value is exceeded, an {@link IllegalStateException} is thrown.</p>
 32  
  *
 33  
  * @author Commons-Id team
 34  
  * @version $Id$
 35  
  */
 36  
 public class PrefixedAlphanumericGenerator extends AlphanumericGenerator {
 37  
 
 38  
     /** Prefix. */
 39  
     private final String prefix;
 40  
 
 41  
 
 42  
     /**
 43  
      * Create a new prefixed alphanumeric generator with the specified prefix.
 44  
      *
 45  
      * @param prefix prefix, must not be null
 46  
      * @param wrap should the factory wrap when it reaches the maximum
 47  
      *  value that can be represented in <code>size</code> base 36 digits
 48  
      *  (or throw an exception)
 49  
      * @param size the size of the identifier, including prefix length
 50  
      * @throws IllegalArgumentException if size less prefix length is not at least one
 51  
      * @throws NullPointerException if prefix is <code>null</code>
 52  
      */
 53  
     public PrefixedAlphanumericGenerator(String prefix, boolean wrap, int size) {
 54  13
         super(wrap, size - ((prefix == null) ? 0 : prefix.length()));
 55  
 
 56  9
         if (prefix == null) {
 57  1
             throw new NullPointerException("prefix must not be null");
 58  
         }
 59  8
         if (size <= prefix.length()) {
 60  0
             throw new IllegalArgumentException("size less prefix length must be at least one");
 61  
         }
 62  8
         this.prefix = prefix;
 63  8
     }
 64  
 
 65  
 
 66  
     /**
 67  
      * Return the prefix for this prefixed alphanumeric generator.
 68  
      *
 69  
      * @return the prefix for this prefixed alphanumeric generator
 70  
      */
 71  
     public String getPrefix() {
 72  1
         return prefix;
 73  
     }
 74  
 
 75  
     public long maxLength() {
 76  1
         return super.maxLength() + prefix.length();
 77  
     }
 78  
 
 79  
     public long minLength() {
 80  1
         return super.minLength() + prefix.length();
 81  
     }
 82  
 
 83  
     public int getSize() {
 84  1
         return super.getSize() + prefix.length();
 85  
     }
 86  
 
 87  
     public String nextStringIdentifier() {
 88  84
         StringBuffer sb = new StringBuffer(prefix);
 89  84
         sb.append(super.nextStringIdentifier());
 90  83
         return sb.toString();
 91  
     }
 92  
 }