AbstractStringIdentifierGenerator.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;

  26. import org.openspcoop2.utils.id.apache.serial.MaxReachedException;

  27. /**
  28.  * Abstract superclass for StringIdentifierGenerator implementations.
  29.  *
  30.  * Author of the original commons apache code:
  31.  * @author Commons-Id team
  32.  * @version $Id$
  33.  *
  34.  * Authors of the Link.it modification to the code:
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public abstract class AbstractStringIdentifierGenerator implements StringIdentifierGenerator {

  39.     /**
  40.      * Maximum length for a numeric string representing a long value.
  41.      */
  42.     protected static final int MAX_LONG_NUMERIC_VALUE_LENGTH =
  43.     Long.toString(Long.MIN_VALUE).length();

  44.     /**
  45.      * Number of alphanumeric characters.
  46.      */
  47.     protected static final int ALPHA_NUMERIC_CHARSET_SIZE = 36;

  48.     /**
  49.      * Maximum length for an alphanumeric string representing a long value.
  50.      */
  51.     protected static final int MAX_LONG_ALPHANUMERIC_VALUE_LENGTH =
  52.     Long.toString(Long.MAX_VALUE, ALPHA_NUMERIC_CHARSET_SIZE).length();

  53.     /**
  54.      * Maximum length for a numeric string representing an integer value.
  55.      */
  56.     protected static final int MAX_INT_NUMERIC_VALUE_LENGTH =
  57.     Integer.toString(Integer.MIN_VALUE).length();

  58.     /**
  59.      * Maximum length for an alphanumeric string representing an integer value.
  60.      */
  61.     protected static final int MAX_INT_ALPHANUMERIC_VALUE_LENGTH =
  62.     Integer.toString(Integer.MAX_VALUE, ALPHA_NUMERIC_CHARSET_SIZE).length();

  63.     /**
  64.      * Default size of an alphanumeric identifier.
  65.      */
  66.     protected static final int DEFAULT_ALPHANUMERIC_IDENTIFIER_SIZE = 15;

  67.     /**
  68.      * Constructor.
  69.      */
  70.     protected AbstractStringIdentifierGenerator() {
  71.         super();
  72.     }

  73.     @Override
  74.     public abstract String nextStringIdentifier() throws MaxReachedException;

  75.     /**
  76.      * Returns the maximum length (number or characters) for an identifier
  77.      * from this sequence.
  78.      *
  79.      * <p>The default implementation returns {@link #INFINITE_MAX_LENGTH}. Implementations
  80.      * with bounded length identifiers should override this method to
  81.      * return the maximum length of a generated identifier.</p>
  82.      *
  83.      * @return {@inheritDoc}
  84.      */
  85.     @Override
  86.     public long maxLength() {
  87.         return INFINITE_MAX_LENGTH;
  88.     }

  89.     /**
  90.      * Returns the minimum length (number of characters) for an identifier
  91.      * from this sequence.
  92.      *
  93.      * <p>The default implementation returns 0. Implementations
  94.      * with identifiers having a postive minimum length should override this
  95.      * method to return the maximum length of a generated identifier.</p>
  96.      *
  97.      * @return {@inheritDoc}
  98.      */
  99.     @Override
  100.     public long minLength() {
  101.         return 0;
  102.     }

  103.     @Override
  104.     public Object nextIdentifier() throws MaxReachedException {
  105.         return nextStringIdentifier();
  106.     }
  107. }