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

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

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

import java.util.Set;

public class LazyCache implements Cache {

   private final LazyBuilder builder;
   private final Cache cache;

   public LazyCache(LazyBuilder builder) {
      this(builder, 16);
   }

   public LazyCache(LazyBuilder builder, int size) {
      this.cache = new CopyOnWriteCache(size);
      this.builder = builder;
   }
   
   @Override
   public void clear() {
      cache.clear();
   }

   @Override
   public int size() {
      return cache.size();
   }

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

   @Override
   public V fetch(K key) {
      V value = cache.fetch(key);
      
      if(value == null) {
         value = builder.create(key);
         cache.cache(key, value);
      }
      return value;
   }

   @Override
   public V cache(K key, V value) {
      return cache.cache(key, value);
   }

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

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

   @Override
   public boolean isEmpty() {
      return cache.isEmpty();
   }
   
   @Override
   public String toString() {
      return String.valueOf(cache);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy