1 package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic;
3 import net.mograsim.logic.model.model.components.ModelComponent;
4 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
5 import net.mograsim.logic.model.serializing.IdentifyParams;
6 import net.mograsim.logic.model.snippets.SnippetDefinintion;
7 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandlerSnippetSuppliers;
9 public class DelegatingAtomicHighLevelStateHandler implements AtomicHighLevelStateHandler
11 private final SubmodelComponent parentComponent;
12 private ModelComponent delegateTarget;
13 private String subStateID;
15 public DelegatingAtomicHighLevelStateHandler(SubmodelComponent component)
17 this(component, null);
20 public DelegatingAtomicHighLevelStateHandler(SubmodelComponent component, DelegatingAtomicHighLevelStateHandlerParams params)
22 this.parentComponent = component;
26 if (params.delegateTarget == null)
27 setDelegateTarget(parentComponent);
30 ModelComponent delegateTarget = parentComponent.submodel.getComponentsByName().get(params.delegateTarget);
31 if (delegateTarget == null)
32 throw new NullPointerException("No subcomponent with name " + params.delegateTarget);
33 setDelegateTarget(delegateTarget);
35 setSubStateID(params.subStateID);
37 parentComponent.submodel.addComponentRemovedListener(c ->
39 if (delegateTarget == c)
40 delegateTarget = null;
44 public void set(ModelComponent delegateTarget, String subStateID)
46 setDelegateTarget(delegateTarget);
47 setSubStateID(subStateID);
50 public void setDelegateTarget(ModelComponent delegateTarget)
52 if (delegateTarget == null)
53 this.delegateTarget = parentComponent;
54 else if (parentComponent.submodel.getComponentsByName().get(delegateTarget.getName()) != delegateTarget)
55 throw new IllegalArgumentException(
56 "Can only set components belonging to the submodel of the parent component of this handler as the delegate target");
57 this.delegateTarget = delegateTarget;
60 public ModelComponent getDelegateTarget()
62 return delegateTarget;
65 public void setSubStateID(String subStateID)
67 this.subStateID = subStateID;
70 public String getSubStateID()
76 public Object getHighLevelState()
78 if (delegateTarget == null)
79 throw new IllegalStateException("Delegating to a component that was destroyed");
80 return delegateTarget.getHighLevelState(subStateID);
84 public void setHighLevelState(Object newState)
86 if (delegateTarget == null)
87 throw new IllegalStateException("Delegating to a component that was destroyed");
88 delegateTarget.setHighLevelState(subStateID, newState);
92 public String getIDForSerializing(IdentifyParams idParams)
98 public DelegatingAtomicHighLevelStateHandlerParams getParamsForSerializing(IdentifyParams idParams)
100 if (delegateTarget == null)
101 throw new IllegalStateException("Delegating to a component that was destroyed");
102 DelegatingAtomicHighLevelStateHandlerParams params = new DelegatingAtomicHighLevelStateHandlerParams();
103 params.delegateTarget = delegateTarget == parentComponent ? null : delegateTarget.getName();
104 params.subStateID = subStateID;
108 public static class DelegatingAtomicHighLevelStateHandlerParams
110 public String delegateTarget;
111 public String subStateID;
116 StandardHighLevelStateHandlerSnippetSuppliers.atomicHandlerSupplier.setSnippetSupplier(
117 DelegatingAtomicHighLevelStateHandler.class.getCanonicalName(),
118 SnippetDefinintion.create(DelegatingAtomicHighLevelStateHandlerParams.class, DelegatingAtomicHighLevelStateHandler::new));