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

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


  25. /**
  26.  * Map
  27.  *
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class Map<T> extends HashMap<MapKey<String>, T> implements Serializable {
  34.    
  35.     /**
  36.      *
  37.      */
  38.     private static final long serialVersionUID = -2577197242840238762L;

  39.    
  40.     private static int uniqueIndex = 0;
  41.     /**private static org.openspcoop2.utils.Semaphore semaphore_getUniqueSerialNumber = new org.openspcoop2.utils.Semaphore("Map");*/
  42.     private static int getUniqueIndex(){
  43.         // la sincronizzazione non sembra servire perchè la chiamata a questo metodo viene fatta esclusivamente dal metodo sottostante che è già sotto semaforo.
  44.         /**SemaphoreLock lock = semaphore_getUniqueSerialNumber.acquireThrowRuntime("getUniqueIndex");*/
  45.         /**try {*/
  46.            
  47.             if((Map.uniqueIndex+1) > Integer.MAX_VALUE){
  48.                 throw new UtilsRuntimeException("Max id reached");
  49.             }
  50.             Map.uniqueIndex++;
  51.             return Map.uniqueIndex;
  52.            
  53.         /**}finally {
  54.             semaphore_getUniqueSerialNumber.release(lock, "getUniqueIndex");
  55.         }*/
  56.     }
  57.    
  58.    
  59.     private static HashMap<String, MapKey<String>> internalMap = new HashMap<>();
  60.        
  61.     private static org.openspcoop2.utils.Semaphore semaphoreNewMapKey = new org.openspcoop2.utils.Semaphore("Map");
  62.     private static MapKey<String> internalNewMapKey(String key){
  63.         SemaphoreLock lock = semaphoreNewMapKey.acquireThrowRuntime("newMapKey");
  64.         try {
  65.             if(!internalMap.containsKey(key)) {
  66.                 int uniqueIndex = getUniqueIndex();
  67.                 MapKey<String> mapKey = new MapKey<>(key, uniqueIndex);
  68.                 internalMap.put(key, mapKey);
  69.                 return mapKey;
  70.             }
  71.             else {
  72.                 return internalMap.get(key);
  73.             }
  74.         }finally {
  75.             semaphoreNewMapKey.release(lock, "newMapKey");
  76.         }
  77.     }
  78.    
  79.     public static MapKey<String> newMapKey(String key) {
  80.         if(!internalMap.containsKey(key)) {
  81.              return internalNewMapKey(key);
  82.         }
  83.         else {
  84.             return internalMap.get(key);
  85.         }
  86.     }
  87.    
  88.    
  89.    

  90.     @Override
  91.     public boolean isEmpty() {
  92.         return super.isEmpty();
  93.     }
  94.    
  95.    
  96.     public T addObject(MapKey<String> key,T value){
  97.         if(key!=null && value!=null) {
  98.             if(value instanceof MapKey<?>) {
  99.                 throw new UtilsRuntimeException("Add object of type 'MapKey<>' in the map not allowed");
  100.             }
  101.             return super.put(key, value);
  102.         }
  103.         return null;
  104.     }
  105.     @Deprecated
  106.     public T addObject(String key,T value){
  107.         if(key==null) {
  108.             return null;
  109.         }
  110.         return addObject(Map.newMapKey(key), value);
  111.     }
  112.     @Override
  113.     public T put(MapKey<String> key, T value) {
  114.         return addObject(key, value);
  115.     }
  116.     @Deprecated
  117.     public T put(String key, T value) {
  118.         return addObject(key, value);
  119.     }

  120.     @Override
  121.     public void putAll(java.util.Map<? extends MapKey<String>, ? extends T> m) {
  122.         if(m!=null && !m.isEmpty()) {
  123.             for (MapKey<String> key : m.keySet()) {
  124.                 addObject(key, m.get(key));
  125.             }
  126.         }
  127.     }
  128.     @Deprecated
  129.     public void putAllObjects(java.util.Map<String, ? extends T> m) {
  130.         if(m!=null && !m.isEmpty()) {
  131.             for (String key : m.keySet()) {
  132.                 addObject(Map.newMapKey(key), m.get(key));
  133.             }
  134.         }
  135.     }
  136.    
  137.     public void addAll(Map<T> map,boolean overwriteIfExists){
  138.         List<MapKey<String>> keys = map.keys();
  139.         if(keys!=null) {
  140.             for (MapKey<String> key : keys) {
  141.                 if(this.containsKey(key)){
  142.                     if(overwriteIfExists){
  143.                         this.removeObject(key);
  144.                         this.addObject(key, map.getObject(key));
  145.                     }
  146.                 }
  147.                 else{
  148.                     this.addObject(key, map.getObject(key));
  149.                 }
  150.             }
  151.         }
  152.     }
  153.    
  154.    
  155.     public T getObject(MapKey<String> key){
  156.         if(key!=null)
  157.             return super.get(key);
  158.         else
  159.             return null;
  160.     }
  161.     public T get(MapKey<String> key){
  162.         return getObject(key);
  163.     }
  164.     @SuppressWarnings({ "rawtypes", "unchecked" })
  165.     @Deprecated
  166.     @Override
  167.     public T get(Object key) {
  168.         if(key==null) {
  169.             return null;
  170.         }
  171.         if(key instanceof MapKey<?>) {
  172.             return (T) getObject((MapKey) key);
  173.         }
  174.         else if(key instanceof String) {
  175.             return getObject(Map.newMapKey((String)key));
  176.         }
  177.         else {
  178.             throw new UtilsRuntimeException("Object key '"+key.getClass().getName()+"' unsopported");
  179.         }
  180.     }
  181.     @Override
  182.     @Deprecated
  183.     public T getOrDefault(Object key, T defaultValue) {
  184.         T o = this.get(key);
  185.         if(o!=null) {
  186.             return o;
  187.         }
  188.         return defaultValue;
  189.     }
  190.    
  191.    
  192.     public T removeObject(MapKey<String> key){
  193.         if(key!=null)
  194.             return super.remove(key);
  195.         else
  196.             return null;
  197.     }
  198.     public T remove(MapKey<String> key){
  199.         return removeObject(key);
  200.     }
  201.     @SuppressWarnings({ "rawtypes", "unchecked" })
  202.     @Deprecated
  203.     @Override
  204.     public T remove(Object key) {
  205.         if(key==null) {
  206.             return null;
  207.         }
  208.         if(key instanceof MapKey<?>) {
  209.             return (T) removeObject((MapKey) key);
  210.         }
  211.         else if(key instanceof String) {
  212.             return removeObject(Map.newMapKey((String)key));
  213.         }
  214.         else {
  215.             throw new UtilsRuntimeException("Object key '"+key.getClass().getName()+"' unsopported");
  216.         }
  217.     }
  218.     @Deprecated
  219.     @Override
  220.     public boolean remove(Object key, Object value) {
  221.         T o = this.remove(key);
  222.         return value.equals(o);
  223.     }
  224.    
  225.    
  226.    
  227.     public List<MapKey<String>> keys(){
  228.         List<MapKey<String>> keys = null;
  229.         if(!this.isEmpty()) {
  230.             keys = new ArrayList<>();
  231.             for (MapKey<String> key : super.keySet()) {
  232.                 keys.add(key);
  233.             }
  234.         }
  235.         return keys;
  236.     }
  237.     @Deprecated
  238.     public List<String> keysAsString(){
  239.         List<String> keys = null;
  240.         if(!this.isEmpty()) {
  241.             keys = new ArrayList<>();
  242.             for (MapKey<String> key : super.keySet()) {
  243.                 keys.add(key.getValue());
  244.             }
  245.         }
  246.         return keys;
  247.     }
  248.    
  249.    
  250.     public boolean containsKey(MapKey<String> key){
  251.         return super.containsKey(key);
  252.     }
  253.     @SuppressWarnings({ "rawtypes", "unchecked" })
  254.     @Deprecated
  255.     @Override
  256.     public boolean containsKey(Object key) {
  257.         if(key==null) {
  258.             return false;
  259.         }
  260.         if(key instanceof MapKey<?>) {
  261.             return this.containsKey((MapKey) key);
  262.         }
  263.         else if(key instanceof String) {
  264.             return this.containsKey(Map.newMapKey((String)key));
  265.         }
  266.         else {
  267.             throw new UtilsRuntimeException("Object key '"+key.getClass().getName()+"' unsopported");
  268.         }
  269.     }
  270.    

  271. }