PrefixedNumericGenerator.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>PrefixedNumericGenerator</code> is an Identifier Generator
  28.  * that generates an incrementing number with a prefix as a String object.
  29.  *
  30.  * <p>If the <code>wrap</code> argument passed to the constructor is set to
  31.  * <code>true</code>, the sequence will wrap, returning negative values when
  32.  * {@link Long#MAX_VALUE} reached; otherwise an {@link IllegalStateException}
  33.  * will be thrown.</p>.
  34.  *
  35.  * Author of the original commons apache code:
  36.  * @author Commons-Id team
  37.  * @version $Id$
  38.  *
  39.  * Authors of the Link.it modification to the code:
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public class PrefixedNumericGenerator extends NumericGenerator {

  44.     /**
  45.      *
  46.      */
  47.     private static final long serialVersionUID = 1L;
  48.    
  49.     /** Prefix. */
  50.     private final String prefix;


  51.     /**
  52.      * Create a new prefixed numeric generator with the specified prefix.
  53.      *
  54.      * @param prefix prefix, must not be null
  55.      * @param wrap should the factory wrap when it reaches the maximum
  56.      *  long value (or throw an exception)
  57.      * @param initialValue the initial long value to start at
  58.      * @throws NullPointerException if prefix is <code>null</code>
  59.      */
  60.     public PrefixedNumericGenerator(String prefix, boolean wrap, long initialValue) {
  61.         super(wrap, initialValue);

  62.         if (prefix == null) {
  63.             throw new NullPointerException("prefix must not be null");
  64.         }
  65.         this.prefix = prefix;
  66.     }


  67.     /**
  68.      * Return the prefix for this prefixed numeric generator.
  69.      *
  70.      * @return the prefix for this prefixed numeric generator
  71.      */
  72.     public String getPrefix() {
  73.         return this.prefix;
  74.     }

  75.     @Override
  76.     public long maxLength() {
  77.         return super.maxLength() + this.prefix.length();
  78.     }

  79.     @Override
  80.     public long minLength() {
  81.         return super.minLength() + this.prefix.length();
  82.     }

  83.     @Override
  84.     public String nextStringIdentifier() throws MaxReachedException {
  85.         StringBuilder sb = new StringBuilder(this.prefix);
  86.         sb.append(super.nextStringIdentifier());
  87.         return sb.toString();
  88.     }
  89. }