Added methods to LogicModel for easy Component/Wire retrieval.
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / LogicModel.java
index eb6f7ee..33cc22e 100644 (file)
@@ -5,9 +5,11 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.function.Consumer;
 
 import net.mograsim.logic.model.model.components.ModelComponent;
+import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
 import net.mograsim.logic.model.model.wires.ModelWire;
 
 public class LogicModel
@@ -51,10 +53,10 @@ public class LogicModel
         */
        protected void componentCreated(ModelComponent component, Runnable destroyed)
        {
-               if (components.containsKey(component.name))
+               if (components.containsKey(component.getName()))
                        throw new IllegalStateException("Don't add the same component twice!");
-               components.put(component.name, component);
-               componentDestroyFunctions.put(component.name, destroyed);
+               components.put(component.getName(), component);
+               componentDestroyFunctions.put(component.getName(), destroyed);
                callComponentAddedListeners(component);
                requestRedraw();
        }
@@ -66,16 +68,17 @@ public class LogicModel
         */
        protected void destroyComponent(ModelComponent component)
        {
-               componentDestroyFunctions.get(component.name).run();
-               if (!components.containsKey(component.name))
+               componentDestroyFunctions.get(component.getName()).run();
+               if (!components.containsKey(component.getName()))
                        throw new IllegalStateException("Don't remove the same component twice!");
-               components.remove(component.name);
+               components.remove(component.getName());
                callComponentRemovedListeners(component);
                requestRedraw();
        }
 
        /**
-        * Adds the given wire to the list of wires and calls all wireAddedListeners.
+        * Adds the given wire to the list of wires and calls all wireAddedListeners. Don't call this method from application code as it is
+        * automatically called in {@link ModelWire}'s constructor.
         * 
         * @author Daniel Kirschten
         */
@@ -114,6 +117,46 @@ public class LogicModel
                return wiresUnmodifiable;
        }
 
+       public <T extends ModelComponent> T getComponentByName(String name, Class<T> modelClass)
+       {
+               return getByName(name, modelClass, components);
+       }
+
+       public ModelWire getWireByName(String name)
+       {
+               return getByName(name, ModelWire.class, wires);
+       }
+
+       @SuppressWarnings("unchecked")
+       private static <T> T getByName(String name, Class<T> modelClass, Map<String, ? super T> map)
+       {
+               Object comp = map.get(name);
+               Objects.requireNonNull(comp, "Invaild path, component " + name + " not found");
+               if (modelClass.isInstance(comp))
+                       return (T) comp;
+               throw new IllegalArgumentException("The component " + name + " is not an instance of " + modelClass);
+       }
+
+       public <T extends ModelComponent> T getComponentByPath(String path, Class<T> modelClass)
+       {
+               int firstDot = path.indexOf('.');
+               if (firstDot == -1)
+                       return getComponentByName(path, modelClass);
+               String first = path.substring(0, firstDot);
+               String rest = path.substring(firstDot + 1);
+               return getComponentByName(first, SubmodelComponent.class).submodel.getComponentByPath(rest, modelClass);
+       }
+
+       public ModelWire getWireByPath(String path)
+       {
+               int firstDot = path.indexOf('.');
+               if (firstDot == -1)
+                       return getWireByName(path);
+               String first = path.substring(0, firstDot);
+               String rest = path.substring(firstDot + 1);
+               return getComponentByName(first, SubmodelComponent.class).submodel.getWireByPath(rest);
+       }
+
        // @formatter:off
        public void addComponentAddedListener         (Consumer<? super ModelComponent> listener) {componentAddedListeners      .add   (listener);}
        public void addComponentRemovedListener       (Consumer<? super ModelComponent> listener) {componentRemovedListeners    .add   (listener);}