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 java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashCache implements Cache {
   
   private volatile Map map;

   public HashCache() {
      super();
   }

   @Override
   public Set keySet() {
      if(map != null) {
         return map.keySet();
      }
      return Collections.emptySet();
   }

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

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

   @Override
   public boolean isEmpty() {
      if(map != null) {
         return map.isEmpty();
      }
      return true;
   }

   @Override
   public boolean contains(K key) {
      if(map != null) {
         return map.containsKey(key);
      }
      return false;
   }

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

   @Override
   public void clear() {
      if(map != null) {
         map.clear();
      }
   }

   @Override
   public int size() {
      if(map != null) {
         return map.size();
      }
      return 0;
   }
   
   @Override
   public String toString() {
      return String.valueOf(map);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy