MapReader.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.resources;

  21. import java.util.Collection;
  22. import java.util.Enumeration;
  23. import java.util.Map;

  24. import org.apache.commons.collections.FastHashMap;
  25. import org.apache.commons.collections.iterators.IteratorEnumeration;

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

  34.     protected Map<K, V> javaMap;
  35.     protected boolean readCallsNotSynchronized = false;
  36.     protected FastHashMap fastMap;
  37.    
  38.     public MapReader(Map<K, V> map, boolean readCallsNotSynchronized){
  39.         this.readCallsNotSynchronized = readCallsNotSynchronized;
  40.         if(this.readCallsNotSynchronized){
  41.            
  42.             // NOTE: This class is not cross-platform.
  43.             // Using it may cause unexpected failures on some architectures.
  44.             // It suffers from the same problems as the double-checked locking idiom.
  45.             // In particular, the instruction that clones the internal collection and the instruction that sets the internal reference to the clone
  46.             // can be executed or perceived out-of-order.
  47.             // This means that any read operation might fail unexpectedly,
  48.             // as it may be reading the state of the internal collection before the internal collection is fully formed.
  49.             // For more information on the double-checked locking idiom, see the Double-Checked Locking Idiom Is Broken Declaration.
  50.             // http://commons.apache.org/proper/commons-collections/javadocs/api-3.2.1/index.html
  51.             // http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
  52.            
  53.             // In pratica non effettuare mai delle PUT dopo aver creato l'oggetto.
  54.             // Usare solo in lettura.
  55.             // Questo garantisce che non si presenta il problema sopra descritto
  56.            
  57.             this.fastMap = new FastHashMap(map);
  58.             this.fastMap.setFast(true);
  59.             /**Enumeration<?> keys = this.javaProperties.keys();
  60.             while (keys.hasMoreElements()) {
  61.                 Object key = (Object) keys.nextElement();
  62.                 this.fastProperties.put(key, properties.get(key));
  63.             }*/
  64.         }
  65.         else{
  66.             this.javaMap = map;
  67.         }
  68.     }
  69.    
  70.     public V get(K key) {
  71.         return this.getValue(key);
  72.     }
  73.    
  74.     @SuppressWarnings("unchecked")
  75.     public V getValue(K key) {
  76.         Object o = null;
  77.         if(this.readCallsNotSynchronized){
  78.             o = this.fastMap.get(key);
  79.         }else{
  80.             o = this.javaMap.get(key);
  81.         }
  82.         if(o==null){
  83.             return null;
  84.         }
  85.         else{
  86.             return (V) o;
  87.         }
  88.     }
  89.    
  90.     @SuppressWarnings("unchecked")
  91.     public java.util.Enumeration<K> keys(){
  92.         if(this.readCallsNotSynchronized){
  93.             return (new IteratorEnumeration(this.fastMap.keySet().iterator()));
  94.         }else{
  95.             return (new IteratorEnumeration(this.javaMap.keySet().iterator()));
  96.         }
  97.     }
  98.    
  99.     @SuppressWarnings("unchecked")
  100.     public Collection<V> values(){
  101.         if(this.readCallsNotSynchronized){
  102.             return this.fastMap.values();
  103.         }else{
  104.             return this.javaMap.values();
  105.         }
  106.     }
  107.    
  108.     @SuppressWarnings("unchecked")
  109.     public Enumeration<V> elements(){
  110.         return new IteratorEnumeration(this.values().iterator());
  111.     }
  112.    
  113.     public boolean containsKey(K key){
  114.         if(this.readCallsNotSynchronized){
  115.             return this.fastMap.containsKey(key);
  116.         }else{
  117.             return this.javaMap.containsKey(key);
  118.         }
  119.     }
  120.    
  121.     public int size(){
  122.         if(this.readCallsNotSynchronized){
  123.             return this.fastMap.size();
  124.         }else{
  125.             return this.javaMap.size();
  126.         }
  127.     }
  128. }