X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fserializing%2FIndirectModelComponentCreator.java;h=13205b286f3c1e590256cf551555eb674711261f;hb=5995c2c9f891ae852a40b4c4736b090d514e7c0a;hp=ab1b9bcaba58db0b1ddc7806e48241ac547f599b;hpb=93a03b54040e4eb0e460d7ed17675b4c7fc3f21e;p=Mograsim.git diff --git a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectModelComponentCreator.java b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectModelComponentCreator.java index ab1b9bca..13205b28 100644 --- a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectModelComponentCreator.java +++ b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectModelComponentCreator.java @@ -10,7 +10,6 @@ import java.util.Objects; import com.google.gson.JsonElement; import com.google.gson.JsonNull; -import com.google.gson.JsonObject; import net.mograsim.logic.model.model.LogicModelModifiable; import net.mograsim.logic.model.model.components.ModelComponent; @@ -24,7 +23,7 @@ public class IndirectModelComponentCreator private static final Map componentSuppliers = new HashMap<>(); private static final Map resourceLoaders = new HashMap<>(); - private static final Map componentCache = new HashMap<>(); + private static final Map componentCache = new HashMap<>(); private static final ResourceLoader defaultResourceLoader; static @@ -96,7 +95,7 @@ public class IndirectModelComponentCreator if (id == null) throw new NullPointerException("Component ID is null"); if (componentCache.containsKey(id)) - return loadComponentFromJsonObject(model, id, name, componentCache.get(id)); + return loadComponentFromJsonObject(model, id, name, componentCache.get(id), false); String resolvedID = resolveID(id); if (resolvedID == null) throw new IllegalArgumentException("Unknown standard ID or illegal resolved ID: " + id); @@ -104,17 +103,17 @@ public class IndirectModelComponentCreator String firstPart = parts[0]; if (firstPart.equals("jsonfile")) { - JsonObject jsonContents; + SubmodelComponentParams jsonContents; try { // don't use parts[1], because the path could contain ':' - jsonContents = JsonHandler.readJson(resolvedID.substring("jsonfile:".length()), JsonObject.class); + jsonContents = JsonHandler.readJson(resolvedID.substring("jsonfile:".length()), SubmodelComponentParams.class); } catch (IOException e) { throw new UncheckedIOException("Error loading JSON file", e); } - return loadComponentFromJsonObject(model, id, name, jsonContents); + return loadComponentFromJsonObject(model, id, name, jsonContents, false); } ResourceLoader loader; String resTypeID; @@ -139,18 +138,18 @@ public class IndirectModelComponentCreator } if (resTypeID.equals("jsonres")) { - JsonObject jsonContents; + SubmodelComponentParams jsonContents; try { @SuppressWarnings("resource") // jsonStream is closed in JsonHandler InputStream jsonStream = Objects.requireNonNull(loader.loadResource(resID), "Error loading JSON resource: Not found"); - jsonContents = JsonHandler.readJson(jsonStream, JsonObject.class); + jsonContents = JsonHandler.readJson(jsonStream, SubmodelComponentParams.class); } catch (IOException e) { throw new UncheckedIOException("Error loading JSON resource", e); } - return loadComponentFromJsonObject(model, id, name, jsonContents); + return loadComponentFromJsonObject(model, id, name, jsonContents, true); } else if (resTypeID.equals("class")) { ComponentSupplier componentSupplier = componentSuppliers.get(resID); @@ -185,11 +184,16 @@ public class IndirectModelComponentCreator } private static SubmodelComponent loadComponentFromJsonObject(LogicModelModifiable model, String id, String name, - JsonObject jsonContents) + SubmodelComponentParams jsonContents, boolean cache) { - componentCache.putIfAbsent(id, jsonContents); - return SubmodelComponentSerializer.deserialize(model, JsonHandler.parser.fromJson(jsonContents, SubmodelComponentParams.class), - name, id, null); + if (cache) + componentCache.putIfAbsent(id, jsonContents); + return SubmodelComponentSerializer.deserialize(model, jsonContents, name, id, null); + } + + public static void clearComponentCache() + { + componentCache.clear(); } public static void registerResourceLoader(ResourceLoader resourceLoader)