Cleaned up ID overriding in DeserializedSubmodelComponent
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Thu, 5 Sep 2019 22:43:49 +0000 (00:43 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Thu, 5 Sep 2019 22:43:49 +0000 (00:43 +0200)
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelComponent.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelInterface.java
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/DeserializedSubmodelComponent.java
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LegacySubmodelComponentSerializer.java
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LogicModelSerializer.java
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/SubmodelComponentSerializer.java

index ffe6ed6..5aa5e38 100644 (file)
@@ -115,7 +115,7 @@ public abstract class SubmodelComponent extends ModelComponent
                this.supermodelPins = new HashMap<>();
                this.supermodelMovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
                this.supermodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
-               this.submodelInterface = new SubmodelInterface(submodelModifiable, SUBMODEL_INTERFACE_NAME);
+               this.submodelInterface = new SubmodelInterface(submodelModifiable);
 
                this.submodelScale = 1;
                this.maxVisibleRegionFillRatioForAlpha0 = 0.8;
@@ -429,14 +429,28 @@ public abstract class SubmodelComponent extends ModelComponent
 
        // serializing
 
+       /**
+        * {@link SubmodelComponent}'s implementation of {@link ModelComponent#getIDForSerializing(IdentifyParams)} returns "submodel". It is
+        * recommended to override this behaviour.
+        * 
+        * @see ModelComponent#getIDForSerializing(IdentifyParams)
+        * @see ModelComponent#getParamsForSerializing(IdentifyParams)
+        */
        @Override
        public String getIDForSerializing(IdentifyParams idParams)
        {
                return "submodel";// TODO what ID?
        }
 
+       /**
+        * {@link SubmodelComponent}'s implementation of {@link ModelComponent#getParamsForSerializing(IdentifyParams)} returns an instance of
+        * {@link SubmodelComponentParams}. It is recommended to override this behaviour.
+        * 
+        * @see ModelComponent#getIDForSerializing(IdentifyParams)
+        * @see ModelComponent#getParamsForSerializing(IdentifyParams)
+        */
        @Override
-       public SubmodelComponentParams getParamsForSerializing(IdentifyParams idParams)
+       public Object getParamsForSerializing(IdentifyParams idParams)
        {
                return SubmodelComponentSerializer.serialize(this, idParams);
        }
index f87df34..331db12 100644 (file)
@@ -9,9 +9,9 @@ import net.mograsim.logic.model.serializing.IdentifyParams;
 
 public class SubmodelInterface extends ModelComponent
 {
-       public SubmodelInterface(LogicModelModifiable model, String name)
+       public SubmodelInterface(LogicModelModifiable model)
        {
-               super(model, name, true);
+               super(model, SubmodelComponent.SUBMODEL_INTERFACE_NAME, true);
        }
 
        @Override
index 33f00e1..8e4d459 100644 (file)
@@ -23,10 +23,10 @@ public class DeserializedSubmodelComponent extends SubmodelComponent
        /**
         * See {@link #idForSerializingOverride}
         */
-       public final JsonElement paramsForSerializingOverride;
+       public final Object paramsForSerializingOverride;
 
        public DeserializedSubmodelComponent(LogicModelModifiable model, String name, String idForSerializingOverride,
-                       JsonElement paramsForSerializingOverride)
+                       Object paramsForSerializingOverride)
        {
                super(model, name, false);
                this.idForSerializingOverride = idForSerializingOverride;
@@ -34,6 +34,32 @@ public class DeserializedSubmodelComponent extends SubmodelComponent
                init();
        }
 
+       /**
+        * If this component has an {@link #idForSerializingOverride} set (e.g. non-null) (see
+        * {@link SubmodelComponentSerializer#deserialize(LogicModelModifiable, SubmodelComponentParams, String, String, JsonElement)
+        * SubmodelComponentSerializer.deserialize(...)}), this ID is returned<br>
+        * If this case doesn't apply (this component has no {@link #idForSerializingOverride} set),
+        * {@link SubmodelComponent#getIDForSerializing(IdentifyParams)} is invoced.
+        */
+       @Override
+       public String getIDForSerializing(IdentifyParams idParams)
+       {
+               return idForSerializingOverride == null ? super.getIDForSerializing(idParams) : idForSerializingOverride;
+       }
+
+       /**
+        * If this component has an {@link #idForSerializingOverride} set (e.g. non-null) (see
+        * {@link SubmodelComponentSerializer#deserialize(LogicModelModifiable, SubmodelComponentParams, String, String, JsonElement)
+        * SubmodelComponentSerializer.deserialize(...)}), {@link #paramsForSerializingOverride} is returned<br>
+        * If this case doesn't apply (this component has no {@link #idForSerializingOverride} set),
+        * {@link SubmodelComponent#getParamsForSerializing(IdentifyParams)} is invoced.
+        */
+       @Override
+       public Object getParamsForSerializing(IdentifyParams idParams)
+       {
+               return idForSerializingOverride == null ? super.getParamsForSerializing(idParams) : paramsForSerializingOverride;
+       }
+
        @Override
        public void setSymbolRenderer(Renderer symbolRenderer)
        {
index 531913f..b41fc52 100644 (file)
@@ -140,8 +140,8 @@ public final class LegacySubmodelComponentSerializer
        }
 
        /**
-        * {@link #serialize(SubmodelComponent, Function)} using a default {@link IdentifierGetter} (see <code>IdentifierGetter</code>'s
-        * {@link IdentifierGetter#IdentifierGetter() default constructor})
+        * {@link #serialize(SubmodelComponent, Function)} using the default {@link IdentifyParams} (see <code>IdentifyParams</code>'s
+        * {@link IdentifyParams#IdentifyParams() default constructor})
         * 
         * @author Daniel Kirschten
         */
@@ -232,17 +232,8 @@ public final class LegacySubmodelComponentSerializer
                        LegacyInnerComponentParams innerComponentParams = new LegacyInnerComponentParams();
                        componentParams[i1] = innerComponentParams;
                        innerComponentParams.pos = new Point(innerComponent.getPosX(), innerComponent.getPosY());
-                       DeserializedSubmodelComponent innerCompCasted;
-                       if (innerComponent instanceof DeserializedSubmodelComponent
-                                       && (innerCompCasted = (DeserializedSubmodelComponent) innerComponent).idForSerializingOverride != null)
-                       {
-                               innerComponentParams.id = innerCompCasted.idForSerializingOverride;
-                               innerComponentParams.params = innerCompCasted.paramsForSerializingOverride;
-                       } else
-                       {
-                               innerComponentParams.id = innerComponent.getIDForSerializing(idParams);
-                               innerComponentParams.params = innerComponent.getParamsForSerializingJSON(idParams);
-                       }
+                       innerComponentParams.id = innerComponent.getIDForSerializing(idParams);
+                       innerComponentParams.params = innerComponent.getParamsForSerializingJSON(idParams);
                        innerComponentParams.name = innerComponent.name;
                        i1++;
                }
index c3b70bd..f871c87 100644 (file)
@@ -9,8 +9,6 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
-import com.google.gson.JsonElement;
-
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
 import net.mograsim.logic.model.model.LogicModel;
 import net.mograsim.logic.model.model.LogicModelModifiable;
@@ -62,8 +60,8 @@ public class LogicModelSerializer
        }
 
        /**
-        * Like {@link #serialize(LogicModel)}, but instead of returning the generated {@link LogicModelParams} they are written to a file at the
-        * given path.
+        * Like {@link #serialize(LogicModel)}, but instead of returning the generated {@link LogicModelParams} they are written to a file at
+        * the given path.
         * 
         * @author Daniel Kirschten
         */
@@ -73,7 +71,7 @@ public class LogicModelSerializer
        }
 
        /**
-        * Like {@link #serialize(LogicModel, IdentifierGetter)}, but instead of returning the generated {@link LogicModelParams} they are written
+        * Like {@link #serialize(LogicModel, IdentifyParams)}, but instead of returning the generated {@link LogicModelParams} they are written
         * to a file at the given path.
         * 
         * @author Daniel Kirschten
@@ -84,8 +82,8 @@ public class LogicModelSerializer
        }
 
        /**
-        * {@link #serialize(LogicModel, IdentifierGetter)} using a default {@link IdentifierGetter} (see <code>IdentifierGetter</code>'s
-        * {@link IdentifierGetter#IdentifierGetter() default constructor})
+        * {@link #serialize(LogicModel, IdentifyParams)} using the default {@link IdentifyParams} (see <code>IdentifyParams</code>'s
+        * {@link IdentifyParams#IdentifyParams() default constructor})
         * 
         * @author Daniel Kirschten
         */
@@ -96,7 +94,8 @@ public class LogicModelSerializer
 
        // "core" methods
        /**
-        * Deserializes components and wires from the specified {@link LogicModelParams} and adds them to the given {@link LogicModelModifiable}.
+        * Deserializes components and wires from the specified {@link LogicModelParams} and adds them to the given
+        * {@link LogicModelModifiable}.
         * 
         * @author Fabian Stemmler
         * @author Daniel Kirschten
@@ -123,16 +122,8 @@ public class LogicModelSerializer
 
        /**
         * Returns {@link LogicModelModifiable}, which describe the components and wires in the given {@link LogicModel}. <br>
-        * Components are serialized in the following way: <br>
-        * If a component is a <code>SubmodelComponent</code> which has been deserialized, and it has an
-        * {@link DeserializedSubmodelComponent#idForSerializingOverride idForSerializingOverride} set (e.g. non-null; see
-        * {@link SubmodelComponentSerializer#deserialize(LogicModelModifiable, SubmodelComponentParams, String, String, JsonElement)
-        * SubmodelComponentSerializer.deserialize(...)}), this ID and the component's
-        * {@link DeserializedSubmodelComponent#paramsForSerializingOverride paramsForSerializingOverride} are written.<br>
-        * If this case doesn't apply (e.g. if the component is not a <code>SubmodelComponent</code>; or it is a <code>SubmodelComponent</code>,
-        * but hasn't been deserialized; or it has no {@link DeserializedSubmodelComponent#idForSerializingOverride idForSerializingOverride}
-        * set), the ID defined by <code>idGetter</code> and the params obtained by {@link ModelComponent#getParamsForSerializing() getParams()}
-        * are written.
+        * Components are serialized using {@link ModelComponent#getIDForSerializing(IdentifyParams)} and
+        * {@link ModelComponent#getParamsForSerializingJSON(IdentifyParams)} <br>
         * 
         * @author Fabian Stemmler
         * @author Daniel Kirschten
@@ -149,17 +140,8 @@ public class LogicModelSerializer
                        ComponentParams compParams = new ComponentParams();
                        componentsParams.add(compParams);
                        compParams.pos = new Point(component.getPosX(), component.getPosY());
-                       DeserializedSubmodelComponent innerCompCasted;
-                       if (component instanceof DeserializedSubmodelComponent
-                                       && (innerCompCasted = (DeserializedSubmodelComponent) component).idForSerializingOverride != null)
-                       {
-                               compParams.id = innerCompCasted.idForSerializingOverride;
-                               compParams.params = innerCompCasted.paramsForSerializingOverride;
-                       } else
-                       {
-                               compParams.id = component.getIDForSerializing(idParams);
-                               compParams.params = component.getParamsForSerializingJSON(idParams);
-                       }
+                       compParams.id = component.getIDForSerializing(idParams);
+                       compParams.params = component.getParamsForSerializingJSON(idParams);
                        compParams.name = component.name;
                }
                modelParams.components = componentsParams.toArray(ComponentParams[]::new);
index b4b2ff9..cf13988 100644 (file)
@@ -124,7 +124,7 @@ public final class SubmodelComponentSerializer
        }
 
        /**
-        * Like {@link #serialize(SubmodelComponent, IdentifierGetter)}, but instead of returning the generated {@link SubmodelComponentParams}
+        * Like {@link #serialize(SubmodelComponent, IdentifyParams)}, but instead of returning the generated {@link SubmodelComponentParams}
         * they are written to a file at the given path.
         * 
         * @author Daniel Kirschten
@@ -135,8 +135,8 @@ public final class SubmodelComponentSerializer
        }
 
        /**
-        * {@link #serialize(SubmodelComponent, IdentifierGetter)} using a default {@link IdentifierGetter} (see <code>IdentifierGetter</code>'s
-        * {@link IdentifierGetter#IdentifierGetter() default constructor})
+        * {@link #serialize(SubmodelComponent, IdentifyParams)} using the default {@link IdentifyParams} (see <code>IdentifyParams</code>'s
+        * {@link IdentifyParams#IdentifyParams() default constructor})
         * 
         * @author Daniel Kirschten
         */
@@ -152,7 +152,7 @@ public final class SubmodelComponentSerializer
         * When serializing a <code>SubmodelComponent</code>, it is undesired for every subcomponent to be serialized with its complete inner
         * structure. Instead, these sub-<code>SubmodelComponent</code>s should be serialized with the ID and params which were used to
         * determine the <code>SubmodelComponentParams</code> defining the sub-<code>SubmodelComponent</code>. Because of this, it is possible
-        * to override the ID and params used in {@link #serialize(SubmodelComponent, IdentifierGetter) serialize(...)} to describe this
+        * to override the ID and params used in {@link #serialize(SubmodelComponent, IdentifyParams) serialize(...)} to describe this
         * subcomponent. See there for details.
         * 
         * @author Fabian Stemmler