All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.common.HashCache Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.common;

import static java.util.Collections.EMPTY_MAP;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashCache implements Cache {
   
   private volatile Map map;

   public HashCache() {
      this.map = EMPTY_MAP;
   }

   @Override
   public Set keySet() {      
      return map.keySet();
   }

   @Override
   public V take(K key) {
      return map.remove(key);
   }

   @Override
   public V fetch(K key) {      
      return map.get(key);
   }

   @Override
   public boolean isEmpty() {      
      return map.isEmpty();
   }

   @Override
   public boolean contains(K key) {      
      return map.containsKey(key);
   }

   @Override
   public void cache(K key, V value) {
      if(map == EMPTY_MAP) {
         map = new HashMap();
      }
      map.put(key, value);
   }

   @Override
   public void clear() {
      map.clear();     
   }

   @Override
   public int size() {
      return map.size();
   }
   
   @Override
   public String toString() {
      return String.valueOf(map);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy