From: Daniel Kirschten Date: Thu, 5 Sep 2019 22:43:49 +0000 (+0200) Subject: Cleaned up ID overriding in DeserializedSubmodelComponent X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=43f832fac0531473dc97af75edb7c5b1cb9f6ce4;p=Mograsim.git Cleaned up ID overriding in DeserializedSubmodelComponent --- diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelComponent.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelComponent.java index ffe6ed63..5aa5e38c 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelComponent.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelComponent.java @@ -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); } diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelInterface.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelInterface.java index f87df342..331db123 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelInterface.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/submodels/SubmodelInterface.java @@ -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 diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/DeserializedSubmodelComponent.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/DeserializedSubmodelComponent.java index 33f00e15..8e4d459c 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/DeserializedSubmodelComponent.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/DeserializedSubmodelComponent.java @@ -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
+ * 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
+ * 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) { diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LegacySubmodelComponentSerializer.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LegacySubmodelComponentSerializer.java index 531913f1..b41fc523 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LegacySubmodelComponentSerializer.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LegacySubmodelComponentSerializer.java @@ -140,8 +140,8 @@ public final class LegacySubmodelComponentSerializer } /** - * {@link #serialize(SubmodelComponent, Function)} using a default {@link IdentifierGetter} (see IdentifierGetter's - * {@link IdentifierGetter#IdentifierGetter() default constructor}) + * {@link #serialize(SubmodelComponent, Function)} using the default {@link IdentifyParams} (see IdentifyParams'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++; } diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LogicModelSerializer.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LogicModelSerializer.java index c3b70bdf..f871c873 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LogicModelSerializer.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LogicModelSerializer.java @@ -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 IdentifierGetter's - * {@link IdentifierGetter#IdentifierGetter() default constructor}) + * {@link #serialize(LogicModel, IdentifyParams)} using the default {@link IdentifyParams} (see IdentifyParams'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}.
- * Components are serialized in the following way:
- * If a component is a SubmodelComponent 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.
- * If this case doesn't apply (e.g. if the component is not a SubmodelComponent; or it is a SubmodelComponent, - * but hasn't been deserialized; or it has no {@link DeserializedSubmodelComponent#idForSerializingOverride idForSerializingOverride} - * set), the ID defined by idGetter and the params obtained by {@link ModelComponent#getParamsForSerializing() getParams()} - * are written. + * Components are serialized using {@link ModelComponent#getIDForSerializing(IdentifyParams)} and + * {@link ModelComponent#getParamsForSerializingJSON(IdentifyParams)}
* * @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); diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/SubmodelComponentSerializer.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/SubmodelComponentSerializer.java index b4b2ff9c..cf139885 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/SubmodelComponentSerializer.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/SubmodelComponentSerializer.java @@ -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 IdentifierGetter's - * {@link IdentifierGetter#IdentifierGetter() default constructor}) + * {@link #serialize(SubmodelComponent, IdentifyParams)} using the default {@link IdentifyParams} (see IdentifyParams's + * {@link IdentifyParams#IdentifyParams() default constructor}) * * @author Daniel Kirschten */ @@ -152,7 +152,7 @@ public final class SubmodelComponentSerializer * When serializing a SubmodelComponent, it is undesired for every subcomponent to be serialized with its complete inner * structure. Instead, these sub-SubmodelComponents should be serialized with the ID and params which were used to * determine the SubmodelComponentParams defining the sub-SubmodelComponent. 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