IDSerialGeneratorBuffer.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.utils.id.serial;

  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.openspcoop2.utils.SemaphoreLock;

  26. /**
  27.  * IDSerialGeneratorBuffer
  28.  *
  29.  * @author Andrea Poli (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class IDSerialGeneratorBuffer {

  34.     private static org.openspcoop2.utils.Semaphore semaphore = new org.openspcoop2.utils.Semaphore("IDSerialGeneratorBuffer");
  35.     private static Map<String, List<String>> buffer = new HashMap<>();
  36.    
  37.     private static String getPrefix(Class<?> cIdSerialGenerator){
  38.         if(cIdSerialGenerator.isAssignableFrom(IDSerialGenerator_alphanumeric.class)){
  39.             return "A_";
  40.         }
  41.         else if(cIdSerialGenerator.isAssignableFrom(IDSerialGenerator_numeric.class)){
  42.             return "N_";
  43.         }
  44.         else{
  45.             return "U_";
  46.         }
  47.     }
  48.    
  49.     private static String getKey(Class<?> cIdSerialGenerator,String relativeInfo){
  50.         StringBuilder bf = new StringBuilder();
  51.         bf.append(getPrefix(cIdSerialGenerator));
  52.         if(relativeInfo!=null){
  53.             bf.append(relativeInfo);
  54.         }
  55.         else{
  56.             bf.append("@_NONRELATIVE_@");
  57.         }
  58.         return bf.toString();
  59.     }
  60.    
  61.     protected static String nextValue(Class<?> cIdSerialGenerator,String relativeInfo){
  62.         String key = getKey(cIdSerialGenerator, relativeInfo);
  63.         //synchronized(buffer){
  64.         SemaphoreLock lock = semaphore.acquireThrowRuntime("nextValue");
  65.         try {
  66.             if(buffer.size()<=0 || !buffer.containsKey(key)){
  67.                 return null;
  68.             }
  69.             else{
  70.                 List<String> l = buffer.get(key);
  71.                 if(l==null || l.size()<=0){
  72.                     return null;
  73.                 }
  74.                 else{
  75.                     if(l.size()==1){
  76.                         l = buffer.remove(key);
  77.                     }
  78.                     String v = l.remove(0);
  79.                     //System.out.println("Return ["+v+"] from Buffer (newSize:"+l.size()+") key["+key+"]");
  80.                     return v;
  81.                 }
  82.             }
  83.         }finally {
  84.             semaphore.release(lock, "nextValue");
  85.         }
  86.     }
  87.    
  88.     protected static void putAll(List<String> valuesGenerated,Class<?> cIdSerialGenerator,String relativeInfo){
  89.         String key = getKey(cIdSerialGenerator, relativeInfo);
  90.         //synchronized(buffer){
  91.         SemaphoreLock lock = semaphore.acquireThrowRuntime("putAll");
  92.         try {
  93.             List<String> l = null;
  94.             if(buffer.containsKey(key)){
  95.                 l = buffer.get(key);
  96.                 //System.out.println("ADD BUFFER ["+valuesGenerated.size()+"] to exists (size:"+l.size()+") key["+key+"]");
  97.             }
  98.             else{
  99.                 l = new ArrayList<>();
  100.                 buffer.put(key, l);
  101.                 //System.out.println("CREATE BUFFER ["+valuesGenerated.size()+"] from Buffer key["+key+"]");
  102.             }
  103.             l.addAll(valuesGenerated);  
  104.         }finally {
  105.             semaphore.release(lock, "putAll");
  106.         }
  107.     }
  108.    
  109.     protected static void clearBuffer(){
  110.         //synchronized(buffer){
  111.         SemaphoreLock lock = semaphore.acquireThrowRuntime("clearBuffer");
  112.         try {
  113.             if(buffer!=null && buffer.size()>0){
  114.                 buffer.clear();
  115.             }
  116.         }finally {
  117.             semaphore.release(lock, "clearBuffer");
  118.         }
  119.     }
  120.    
  121.     protected static void clearBuffer(Class<?> cIdSerialGenerator){
  122.         String prefix = getPrefix(cIdSerialGenerator);
  123.         //synchronized(buffer){
  124.         SemaphoreLock lock = semaphore.acquireThrowRuntime("clearBuffer_class");
  125.         try {
  126.             if(buffer!=null && buffer.size()>0){
  127.                 List<String> remove = new ArrayList<>();
  128.                 for (String key : buffer.keySet()) {
  129.                     if(key.startsWith(prefix)){
  130.                         remove.add(key);
  131.                     }
  132.                 }
  133.                 while(!remove.isEmpty()) {
  134.                     buffer.remove(remove.remove(0));    
  135.                 }
  136.             }
  137.         }finally {
  138.             semaphore.release(lock, "clearBuffer_class");
  139.         }
  140.     }

  141.    
  142. }