PrefixedAlphanumericGenerator.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. /**
  27.  * <code>PrefixedAlphanumericGenerator</code> is an identifier generator
  28.  * that generates an incrementing number in base 36 with a prefix as a String
  29.  * 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 36 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 PrefixedAlphanumericGenerator extends AlphanumericGenerator {

  49.     /**
  50.      *
  51.      */
  52.     private static final long serialVersionUID = 1L;
  53.    
  54.     /** Prefix. */
  55.     private final String prefix;

  56.     private static int _getSize(String prefix, int size) {
  57.         if (prefix == null) {
  58.             throw new NullPointerException("prefix must not be null");
  59.         }
  60.         return size - prefix.length();
  61.     }
  62.     public PrefixedAlphanumericGenerator(String prefix, boolean wrap) {
  63.         super(wrap, _getSize(prefix, DEFAULT_ALPHANUMERIC_IDENTIFIER_SIZE));

  64.         if (DEFAULT_ALPHANUMERIC_IDENTIFIER_SIZE <= prefix.length()) {
  65.             throw new IllegalArgumentException("size less prefix length must be at least one");
  66.         }
  67.         this.prefix = prefix;
  68.     }
  69.     public PrefixedAlphanumericGenerator(String prefix, boolean wrap, int size) {
  70.         super(wrap, _getSize(prefix, size));

  71.         if (size <= prefix.length()) {
  72.             throw new IllegalArgumentException("size less prefix length must be at least one");
  73.         }
  74.         this.prefix = prefix;
  75.     }
  76.    
  77.     private static String _getInitialValue(String prefix, String initialValue) {
  78.         if (prefix == null) {
  79.             throw new NullPointerException("prefix must not be null");
  80.         }
  81.         return initialValue.substring(prefix.length());
  82.     }
  83.     public PrefixedAlphanumericGenerator(String prefix, boolean wrap, String initialValue) {
  84.         super(wrap, _getInitialValue(prefix, initialValue));

  85.         if (initialValue.length() <= prefix.length()) {
  86.             throw new IllegalArgumentException("size less prefix length must be at least one");
  87.         }
  88.         this.prefix = prefix;
  89.     }


  90.     /**
  91.      * Return the prefix for this prefixed alphanumeric generator.
  92.      *
  93.      * @return the prefix for this prefixed alphanumeric generator
  94.      */
  95.     public String getPrefix() {
  96.         return this.prefix;
  97.     }

  98.     @Override
  99.     public long maxLength() {
  100.         return super.maxLength() + this.prefix.length();
  101.     }

  102.     @Override
  103.     public long minLength() {
  104.         return super.minLength() + this.prefix.length();
  105.     }

  106.     @Override
  107.     public int getSize() {
  108.         return super.getSize() + this.prefix.length();
  109.     }

  110.     @Override
  111.     public synchronized String nextStringIdentifier() throws MaxReachedException {
  112.         StringBuilder sb = new StringBuilder(this.prefix);
  113.         sb.append(super.nextStringIdentifier());
  114.         return sb.toString();
  115.     }
  116. }