cdc7e4a2d9ee284b2d961dd824e4ed9f39b7f454
[Mograsim.git] /
1 package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic;
2
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;
8
9 public class DelegatingAtomicHighLevelStateHandler implements AtomicHighLevelStateHandler
10 {
11         private final SubmodelComponent parentComponent;
12         private ModelComponent delegateTarget;
13         private String subStateID;
14
15         public DelegatingAtomicHighLevelStateHandler(SubmodelComponent component)
16         {
17                 this(component, null);
18         }
19
20         public DelegatingAtomicHighLevelStateHandler(SubmodelComponent component, DelegatingAtomicHighLevelStateHandlerParams params)
21         {
22                 this.parentComponent = component;
23                 if (params != null)
24                 {
25                         // TODO document this
26                         if (params.delegateTarget == null)
27                                 setDelegateTarget(parentComponent);
28                         else
29                         {
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);
34                         }
35                         setSubStateID(params.subStateID);
36                 }
37                 parentComponent.submodel.addComponentRemovedListener(c ->
38                 {
39                         if (delegateTarget == c)
40                                 delegateTarget = null;
41                 });
42         }
43
44         public void set(ModelComponent delegateTarget, String subStateID)
45         {
46                 setDelegateTarget(delegateTarget);
47                 setSubStateID(subStateID);
48         }
49
50         public void setDelegateTarget(ModelComponent delegateTarget)
51         {
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;
58         }
59
60         public ModelComponent getDelegateTarget()
61         {
62                 return delegateTarget;
63         }
64
65         public void setSubStateID(String subStateID)
66         {
67                 this.subStateID = subStateID;
68         }
69
70         public String getSubStateID()
71         {
72                 return subStateID;
73         }
74
75         @Override
76         public Object getHighLevelState()
77         {
78                 if (delegateTarget == null)
79                         throw new IllegalStateException("Delegating to a component that was destroyed");
80                 return delegateTarget.getHighLevelState(subStateID);
81         }
82
83         @Override
84         public void setHighLevelState(Object newState)
85         {
86                 if (delegateTarget == null)
87                         throw new IllegalStateException("Delegating to a component that was destroyed");
88                 delegateTarget.setHighLevelState(subStateID, newState);
89         }
90
91         @Override
92         public String getIDForSerializing(IdentifyParams idParams)
93         {
94                 return "delegating";
95         }
96
97         @Override
98         public DelegatingAtomicHighLevelStateHandlerParams getParamsForSerializing(IdentifyParams idParams)
99         {
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;
105                 return params;
106         }
107
108         public static class DelegatingAtomicHighLevelStateHandlerParams
109         {
110                 public String delegateTarget;
111                 public String subStateID;
112         }
113
114         static
115         {
116                 StandardHighLevelStateHandlerSnippetSuppliers.atomicHandlerSupplier.setSnippetSupplier(
117                                 DelegatingAtomicHighLevelStateHandler.class.getCanonicalName(),
118                                 SnippetDefinintion.create(DelegatingAtomicHighLevelStateHandlerParams.class, DelegatingAtomicHighLevelStateHandler::new));
119         }
120 }