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

org.snapscript.core.EntityCache Maven / Gradle / Ivy

package org.snapscript.core;

import org.snapscript.common.Cache;
import org.snapscript.common.CopyOnWriteCache;
import org.snapscript.common.CopyOnWriteSparseArray;
import org.snapscript.common.SparseArray;

public class EntityCache {

   private final SparseArray array;
   private final Cache cache;
   
   public EntityCache() {
      this(10000);
   }
   
   public EntityCache(int capacity) {
      this.array = new CopyOnWriteSparseArray(capacity); // for order > 0
      this.cache = new CopyOnWriteCache(); // for order = 0
   }

   public V take(Entity type) {
      int order = type.getOrder();
      
      if(order == 0) {
         return cache.take(type);
      }
      return array.remove(order);
   }

   public V fetch(Entity type) {
      int order = type.getOrder();
      
      if(order == 0) {
         return cache.fetch(type);
      }
      return array.get(order);
   }

   public boolean contains(Entity type) {
      int order = type.getOrder();
      
      if(order == 0) {
         return cache.contains(type);
      }
      return array.get(order) != null;
   }

   public void cache(Entity type, V value) {
      int order = type.getOrder();
      
      if(order == 0) {
         cache.cache(type, value);
      }
      array.set(order, value);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy