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

org.snapscript.tree.reference.ConstraintReference Maven / Gradle / Ivy

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

import java.util.List;

import org.snapscript.core.Entity;
import org.snapscript.core.Evaluation;
import org.snapscript.core.constraint.Constraint;
import org.snapscript.core.constraint.ConstraintDescription;
import org.snapscript.core.error.InternalStateException;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.type.Type;
import org.snapscript.core.variable.Value;

public abstract class ConstraintReference extends Evaluation {

   private volatile ConstraintValue value;
   
   protected ConstraintReference() {
      super();
   }

   @Override
   public Constraint compile(Scope scope, Constraint left) throws Exception {
      if(value == null) {
         value = create(scope);
      }
      return value.constraint;
   }
   
   @Override
   public Value evaluate(Scope scope, Value left) throws Exception {
      if(value == null) {
         value = create(scope);
      }
      return value;
   }
   
   protected abstract ConstraintValue create(Scope scope) throws Exception;
   
   protected static class ConstraintValue extends Value {
      
      private final Constraint constraint;
      private final Value value;
      
      public ConstraintValue(Constraint constraint, Value value, Entity entity) {
         this.constraint = new ConstraintDefinition(constraint, entity);
         this.value = value;       
      }
      
      @Override
      public Constraint getConstraint() {
         return constraint;
      }
      
      @Override
      public  T getValue() {
         return value.getValue();
      }
      
      @Override
      public void setValue(Object value){
         throw new InternalStateException("Illegal modification of literal '" + value + "'");
      }    
      
      @Override
      public String toString() {
         return value.toString();
      }      
   }   

   protected static class ConstraintDefinition extends Constraint {
      
      private ConstraintDescription description;
      private List generics;
      private List imports;
      private Constraint constraint;
      private Type type;
      private String name;
      
      public ConstraintDefinition(Constraint constraint, Entity entity) {
         this.description = new ConstraintDescription(constraint, entity);
         this.constraint = constraint;      
      }
      
      @Override
      public List getImports(Scope scope) {
         if(imports == null) {
            imports = constraint.getImports(scope);
         }
         return imports;
      }
      
      @Override
      public List getGenerics(Scope scope) {
         if(generics == null) {
            generics = constraint.getGenerics(scope);
         }
         return generics;
      }
      
      @Override
      public Type getType(Scope scope) {
         if(type == null) {
            type = constraint.getType(scope);
         }
         return type;
      }
      
      @Override
      public String getName(Scope scope) {
         if(name == null) {
            name = constraint.getName(scope);
         }
         return name;
      }      

      @Override
      public String toString() {
         return description.toString();
      }      
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy