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

org.snapscript.studio.agent.debug.InstanceScopeNode Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.studio.agent.debug;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.snapscript.core.property.Property;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.scope.ScopeState;
import org.snapscript.core.scope.instance.Instance;
import org.snapscript.core.type.Type;
import org.snapscript.core.type.TypeTraverser;
import org.snapscript.core.variable.Value;

public class InstanceScopeNode implements ScopeNode {
   
   private final TypeTraverser extractor;
   private final ScopeNodeBuilder builder;
   private final List nodes;
   private final Scope scope;
   private final String alias;
   private final String path;
   private final String name;
   private final int depth;
   
   public InstanceScopeNode(ScopeNodeBuilder builder, Instance scope, String path, String name, String alias, int depth) {
      this.extractor = new TypeTraverser();
      this.nodes = new ArrayList();
      this.builder = builder;
      this.scope = scope;
      this.depth = depth;
      this.alias = alias;
      this.name = name;
      this.path = path;
   }
   
   @Override
   public int getDepth() {
      return depth;
   }
   
   @Override
   public String getName() {
      return name;
   }
   
   @Override
   public String getAlias() {
      return alias;
   }
   
   @Override
   public String getPath() {
      return path;
   }

   @Override
   public List getNodes() {
      if(nodes.isEmpty()) {
         ScopeState state = scope.getState();
         Iterator names = state.iterator();
         Type type = scope.getType();
         Set types = extractor.findHierarchy(type);
         
         if(names.hasNext() && !types.isEmpty()) {
            Map include = new HashMap();
            
            for(Type base : types) {
               List fields = base.getProperties();
               
               for(Property property : fields) {
                  String alias = property.getAlias();
                  String name = property.getName();
                  
                  include.put(alias, name);
               }
            }
            while(names.hasNext()) {
               String alias = names.next();
               
               if(include.containsKey(alias)) {
                  String name = include.get(alias);
                  Value value = state.getValue(alias);                  
                  Object object = value.getValue();
                  int modifiers = value.getModifiers();
                  ScopeNode node = builder.createNode(path + "." + alias, name, alias, object, modifiers, depth);
                  
                  if(node != null) {
                     nodes.add(node);
                  }
               }
            }
         }
      }
      return nodes;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy