IDGenerator.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.generic_project.utils;

  21. import org.openspcoop2.generic_project.beans.ComplexField;
  22. import org.openspcoop2.generic_project.beans.IField;

  23. /**
  24.  * IDGenerator
  25.  *
  26.  * @author Poli Andrea (apoli@link.it)
  27.  * @author $Author$
  28.  * @version $Rev$, $Date$
  29.  */
  30. public class IDGenerator {

  31.     private static long serialNumber = 0;
  32.     public static synchronized String getUniqueID(){
  33.             if((IDGenerator.serialNumber+1) > Long.MAX_VALUE){
  34.                 IDGenerator.serialNumber = 0;
  35.             }
  36.             IDGenerator.serialNumber++;
  37.             return "T"+System.currentTimeMillis()+"_" +"S" +IDGenerator.serialNumber;
  38.     }
  39.    
  40.     private static long sequence = 0;
  41.     public static synchronized long getUniqueSequenceValue(){
  42.         if((IDGenerator.sequence+1) > Long.MAX_VALUE){
  43.             IDGenerator.sequence = 0;
  44.         }
  45.         IDGenerator.sequence++;
  46.         return IDGenerator.sequence;
  47. }
  48.    
  49.     public static String getUniqueID(IField field){
  50.         if(field instanceof ComplexField){
  51.             ComplexField cf = (ComplexField) field;
  52.             if(cf.getFather()!=null){
  53.                 return IDGenerator.normalize(cf.getFather().getFieldName()+"_"+field.getFieldName()+"_"+IDGenerator.getUniqueID());
  54.             }
  55.         }
  56.         return IDGenerator.normalize(field.getClassName()+"_"+field.getFieldName()+"_"+IDGenerator.getUniqueID());
  57.     }

  58.     private static String normalize(String value){
  59.         return value.replaceAll("-", "_");
  60.     }
  61. }