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

org.snapscript.tree.variable.MapPointer Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version

package org.snapscript.tree.variable;

import static org.snapscript.core.ModifierType.PUBLIC;

import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import org.snapscript.core.Module;
import org.snapscript.core.Scope;
import org.snapscript.core.Type;
import org.snapscript.core.Value;
import org.snapscript.core.property.MapProperty;
import org.snapscript.core.property.Property;
import org.snapscript.core.property.PropertyValue;

public class MapPointer implements VariablePointer {
   
   private final AtomicReference reference;
   private final ObjectPointer pointer;
   private final String name;
   
   public MapPointer(String name) {
      this.reference = new AtomicReference();
      this.pointer = new ObjectPointer(name);
      this.name = name;
   }
   
   @Override
   public Value get(Scope scope, Map left) {
      Property accessor = reference.get();
      
      if(accessor == null) {
         Property match = match(scope, left);
         
         if(match != null) {
            reference.set(match);
            return new PropertyValue(match, left, name);
         }
         return null;
      }
      return new PropertyValue(accessor, left, name);
   }
      
   public Property match(Scope scope, Object left) {
      Property property = pointer.match(scope, left);
   
      if(property == null) {
         Module module = scope.getModule();
         Class type = left.getClass();
         String alias = type.getName();
         Type source = module.getType(alias);
         
         return new MapProperty(name, source, PUBLIC.mask);
      }
      return property;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy