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

org.snapscript.bridge.generate.BridgeInstanceConverter Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.bridge.generate;

import static org.snapscript.core.Reserved.TYPE_THIS;

import java.util.List;

import org.snapscript.core.State;
import org.snapscript.core.Type;
import org.snapscript.core.Value;
import org.snapscript.core.ValueType;
import org.snapscript.core.define.Instance;
import org.snapscript.core.property.Property;
import org.snapscript.core.property.PropertyValue;

public class BridgeInstanceConverter {
   
   private final Type type;
   
   public BridgeInstanceConverter(Type type) {
      this.type = type;
   }

   public void convert(Instance instance) {
      Value self = ValueType.getReference(instance);
      List types = type.getTypes();
      State state = instance.getState();
      Object bridge = instance.getBridge();
      
      update(state, bridge, type);
      
      for(Type type : types) {
         update(state, bridge, type);
      }
      state.add(TYPE_THIS, self);
   }

   private void update(State state, Object object, Type type) {
      List properties = type.getProperties();
      
      for(Property prop : properties) {
         String name = prop.getName();
         
         if(!name.equals(TYPE_THIS)) {
            Object current = state.get(name);
            
            if(current == null) {
               Value value = new PropertyValue(prop, object, name);
               state.add(name, value);
            }
         }
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy