NumericGenerator.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. import java.io.Serializable;

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

  46.     /**
  47.      * <code>serialVersionUID</code> is the serializable UID for the binary version of the class.
  48.      */
  49.     private static final long serialVersionUID = 20060121L;
  50.    
  51.     /** Should the counter wrap. */
  52.     private boolean wrapping;
  53.     /** The counter. */
  54.     private long count = 0;

  55.     /**
  56.      * <p>Constructor.</p>
  57.      *
  58.      * @param wrap should the factory wrap when it reaches the maximum
  59.      *  long value (or throw an exception)
  60.      * @param initialValue  the initial long value to start at
  61.      */
  62.     public NumericGenerator(boolean wrap, long initialValue) {
  63.         super();
  64.         this.wrapping = wrap;
  65.         this.count = initialValue;
  66.     }

  67.     /**
  68.      * Returns the maximum length (number or characters) for an identifier
  69.      * from this sequence.
  70.      *
  71.      * <p>The maximum value is determined from the length of the string
  72.      * representation of {@link Long#MAX_VALUE}.</p>
  73.      *
  74.      * @return the maximum identifier length
  75.      */
  76.     @Override
  77.     public long maxLength() {
  78.         return AbstractStringIdentifierGenerator.MAX_LONG_NUMERIC_VALUE_LENGTH;
  79.     }

  80.     /**
  81.      * <p>Returns the minimum length (number of characters) for an identifier
  82.      * from this sequence.</p>
  83.      *
  84.      * @return the minimum identifier length: <code>1</code>
  85.      */
  86.     @Override
  87.     public long minLength() {
  88.         return 1;
  89.     }

  90.     /**
  91.      * Getter for property wrap.
  92.      *
  93.      * @return <code>true</code> if this generator is set up to wrap.
  94.      *
  95.      */
  96.     public boolean isWrap() {
  97.         return this.wrapping;
  98.     }

  99.     /**
  100.      * Sets the wrap property.
  101.      *
  102.      * @param wrap value for the wrap property
  103.      *
  104.      */
  105.     public void setWrap(boolean wrap) {
  106.         this.wrapping = wrap;
  107.     }

  108.     @Override
  109.     public String nextStringIdentifier() throws MaxReachedException {
  110.         long value = 0;
  111.         if (this.wrapping) {
  112.             synchronized (this) {
  113.                 value = this.count++;
  114.             }
  115.         } else {
  116.             synchronized (this) {
  117.                 if (this.count == Long.MAX_VALUE) {
  118.                     throw new MaxReachedException
  119.                     ("The maximum number of identifiers has been reached");
  120.                 }
  121.                 value = this.count++;
  122.             }
  123.         }
  124.         return Long.toString(value);
  125.     }
  126. }