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

org.snapscript.core.scope.instance.InstanceState Maven / Gradle / Ivy

package org.snapscript.core.scope.instance;

import java.util.Iterator;
import java.util.Set;

import org.snapscript.common.Cache;
import org.snapscript.common.CompoundIterator;
import org.snapscript.common.CopyOnWriteCache;
import org.snapscript.common.HashCache;
import org.snapscript.core.constraint.Constraint;
import org.snapscript.core.error.InternalStateException;
import org.snapscript.core.scope.State;
import org.snapscript.core.variable.Value;

public class InstanceState implements State {
   
   private final Cache constraints;
   private final Cache values;   
   private final Instance instance;

   public InstanceState(Instance instance) {
      this.constraints = new HashCache();
      this.values = new CopyOnWriteCache();
      this.instance = instance;
   }

   @Override
   public Iterator iterator() {
      Set keys = values.keySet();
      Iterator inner = keys.iterator();
      
      if(instance != null) {
         State state = instance.getState();
         Iterator outer = state.iterator();
         
         return new CompoundIterator(inner, outer);
      }
      return inner;
   }

   @Override
   public Value getValue(String name) {
      Value value = values.fetch(name);
      
      if(value == null) {
         State state = instance.getState();
         
         if(state == null) {
            throw new InternalStateException("Scope for '" + name + "' does not exist");
         }
         value = state.getValue(name);
         
         if(value != null) {
            values.cache(name, value);
         }
      }
      return value;
   }
   
   @Override
   public void addValue(String name, Value value) {
      Value existing = values.fetch(name);

      if(existing != null) {
         throw new InternalStateException("Variable '" + name + "' already exists");
      }
      values.cache(name, value); 
   }
   
   @Override
   public Constraint getConstraint(String name) {
      Constraint constraint = constraints.fetch(name);
      
      if(constraint == null) {
         State state = instance.getState();
         
         if(state == null) {
            throw new InternalStateException("Scope for '" + name + "' does not exist");
         }
         return state.getConstraint(name);
      }
      return constraint;
   }
   
   @Override
   public void addConstraint(String name, Constraint constraint) {
      Constraint existing = constraints.fetch(name);

      if(existing != null) {
         throw new InternalStateException("Constraint '" + name + "' already exists");
      }
      constraints.cache(name, constraint); 
   }
   
   @Override
   public String toString() {
      return String.valueOf(values);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy