PrefixedLeftPaddedNumericGenerator.java

  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. /*
  18.  * Modificato da Link.it (https://link.it) per supportare le seguenti funzionalità:
  19.  * - Generazione ID all'interno delle interfacce di OpenSPCoop2
  20.  * - Gestione caratteri massimi per numeri e cifre
  21.  * - Possibilità di utilizzare lowerCase e/o upperCase
  22.  *
  23.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  24.  */
  25. package org.openspcoop2.utils.id.apache.serial;

  26. import org.openspcoop2.utils.id.apache.AbstractStringIdentifierGenerator;

  27. /**
  28.  * <code>PrefixedLeftPaddedNumericGenerator</code> is an Identifier Generator
  29.  * that generates a left-padded incrementing number with a prefix as a String object.
  30.  *
  31.  * <p>All generated ids have the same length (prefixed and padded with 0's
  32.  * on the left), which is determined by the <code>size</code> parameter passed
  33.  * to the constructor.<p>
  34.  *
  35.  * <p>The <code>wrap</code> property determines whether or not the sequence wraps
  36.  * when it reaches the largest value that can be represented in <code>size</code>
  37.  * base 10 digits. If <code>wrap</code> is false and the the maximum representable
  38.  * value is exceeded, an {@link IllegalStateException} is thrown.</p>.
  39.  *
  40.  * Author of the original commons apache code:
  41.  * @author Commons-Id team
  42.  * @version $Id$
  43.  *
  44.  * Authors of the Link.it modification to the code:
  45.  * @author $Author$
  46.  * @version $Rev$, $Date$
  47.  */
  48. public class PrefixedLeftPaddedNumericGenerator extends AbstractStringIdentifierGenerator {

  49.     /** Prefix. */
  50.     private final String prefix;

  51.     /** Should the counter wrap. */
  52.     private boolean wrap = true;

  53.     /** The counter. */
  54.     private char[] count = null;

  55.     /** '9' char. */
  56.     private static final char NINE_CHAR = '9';


  57.     /**
  58.      * Create a new prefixed left-padded numeric generator with the specified prefix.
  59.      *
  60.      * @param prefix prefix, must not be null or empty
  61.      * @param wrap should the factory wrap when it reaches the maximum
  62.      *  value that can be represented in <code>size</code> base 10 digits
  63.      *  (or throw an exception)
  64.      * @param size the size of the identifier, including prefix length
  65.      * @throws IllegalArgumentException if size less prefix length is not at least one
  66.      * @throws NullPointerException if prefix is <code>null</code>
  67.      */
  68.     public PrefixedLeftPaddedNumericGenerator(String prefix, boolean wrap, int size) {
  69.         super();

  70.         if (prefix == null) {
  71.             throw new NullPointerException("prefix must not be null");
  72.         }
  73.         if (size < 1) {
  74.             throw new IllegalArgumentException("size must be at least one");
  75.         }
  76.         if (size <= prefix.length()) {
  77.             throw new IllegalArgumentException("size less prefix length must be at least one");
  78.         }
  79.         this.wrap = wrap;
  80.         this.prefix = prefix;

  81.         int countLength = size - prefix.length();
  82.         this.count = new char[countLength];
  83.         for (int i = 0; i < countLength; i++) {
  84.             this.count[i] = '0';
  85.         }
  86.     }


  87.     /**
  88.      * Return the prefix for this prefixed numeric generator.
  89.      *
  90.      * @return the prefix for this prefixed numeric generator
  91.      */
  92.     public String getPrefix() {
  93.         return this.prefix;
  94.     }

  95.     @Override
  96.     public long maxLength() {
  97.         return ((long)this.count.length) + ((long)this.prefix.length());
  98.     }

  99.     @Override
  100.     public long minLength() {
  101.         return ((long)this.count.length) + ((long)this.prefix.length());
  102.     }

  103.     /**
  104.      * Returns the (constant) size of the strings generated by this generator.
  105.      *
  106.      * @return the size of generated identifiers
  107.      */
  108.     public int getSize() {
  109.         return this.count.length + this.prefix.length();
  110.     }

  111.     /**
  112.      * Getter for property wrap.
  113.      *
  114.      * @return <code>true</code> if this generator is set up to wrap
  115.      */
  116.     public boolean isWrap() {
  117.         return this.wrap;
  118.     }

  119.     /**
  120.      * Setter for property wrap.
  121.      *
  122.      * @param wrap should the factory wrap when it reaches the maximum
  123.      *  value that can be represented in <code>size</code> base 10 digits
  124.      *  (or throw an exception)
  125.      */
  126.     public void setWrap(boolean wrap) {
  127.         this.wrap = wrap;
  128.     }

  129.     @Override
  130.     public String nextStringIdentifier() throws MaxReachedException {
  131.         for (int i = this.count.length - 1; i >= 0; i--) {
  132.             switch (this.count[i]) {
  133.                 case NINE_CHAR:  // 9
  134.                     this.count[i] = '0';
  135.                     if (i == 0 && !this.wrap) {
  136.                         throw new MaxReachedException
  137.                         ("The maximum number of identifiers has been reached");
  138.                     }
  139.                     break;

  140.                 default:
  141.                     this.count[i]++;
  142.                     i = -1;
  143.                     break;
  144.             }
  145.         }

  146.         StringBuilder sb = new StringBuilder(this.prefix);
  147.         sb.append(this.count);
  148.         return sb.toString();
  149.     }
  150. }