LongGenerator.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.AbstractLongIdentifierGenerator;

  27. import java.io.Serializable;

  28. /**
  29.  * <code>LongGenerator</code> is an Identifier Generator
  30.  * that generates an incrementing number as a Long object.
  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 LongGenerator extends AbstractLongIdentifierGenerator 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 = 20060122L;

  50.     /** Should the counter wrap. */
  51.     private boolean wrapping;
  52.     /** The counter. */
  53.     private long count = 0;
  54.    
  55.     /**
  56.      * Constructor.
  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 LongGenerator(boolean wrap, long initialValue) {
  63.         super();
  64.         this.wrapping = wrap;
  65.         this.count = initialValue;
  66.     }

  67.     /**
  68.      * Getter for property wrap.
  69.      *
  70.      * @return <code>true</code> if this generator is set up to wrap.
  71.      *
  72.      */
  73.     public boolean isWrap() {
  74.         return this.wrapping;
  75.     }

  76.     /**
  77.      * Sets the wrap property.
  78.      *
  79.      * @param wrap value for the wrap property
  80.      *
  81.      */
  82.     public void setWrap(boolean wrap) {
  83.         this.wrapping = wrap;
  84.     }
  85.    
  86.     @Override
  87.     public Long nextLongIdentifier() throws MaxReachedException {
  88.         long value = 0;
  89.         if (this.wrapping) {
  90.             synchronized (this) {
  91.                 value = this.count++;
  92.             }
  93.         } else {
  94.             synchronized (this) {
  95.                 if (this.count == Long.MAX_VALUE) {
  96.                     throw new MaxReachedException
  97.                     ("The maximum number of identifiers has been reached");
  98.                 }
  99.                 value = this.count++;
  100.             }
  101.         }
  102.         return value;
  103.     }
  104. }