Small improvement regarding IDs
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Sun, 1 Sep 2019 11:13:41 +0000 (13:13 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Sun, 1 Sep 2019 11:13:41 +0000 (13:13 +0200)
net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/SaveLoadManager.java
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IdentifierGetter.java
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/CodeSnippetSupplier.java
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/standardSnippetIDMapping.json
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/standardSnippetIDMapping.json

index 1e3d271..e690200 100644 (file)
@@ -1,8 +1,6 @@
 package net.mograsim.logic.model.editor;
 
 import java.io.IOException;
-import java.util.Map;
-import java.util.Map.Entry;
 
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.FileDialog;
@@ -10,11 +8,8 @@ import org.eclipse.swt.widgets.Shell;
 
 import net.mograsim.logic.model.model.ViewModelModifiable;
 import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
-import net.mograsim.logic.model.serializing.IdentifierGetter;
 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
 import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
-import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers;
-import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandlerSnippetSuppliers;
 
 public class SaveLoadManager
 {
@@ -53,19 +48,7 @@ public class SaveLoadManager
        {
                try
                {
-                       IdentifierGetter idGetter = new IdentifierGetter();
-                       idGetter.componentIDs = c -> getStandardID(c, IndirectGUIComponentCreator.getStandardComponentIDs(), true);
-                       idGetter.symbolRendererIDs = h -> getStandardID(h,
-                                       SubmodelComponentSnippetSuppliers.symbolRendererSupplier.getStandardSnippetIDs());
-                       idGetter.outlineRendererIDs = h -> getStandardID(h,
-                                       SubmodelComponentSnippetSuppliers.outlineRendererSupplier.getStandardSnippetIDs());
-                       idGetter.highLevelStateHandlerIDs = h -> getStandardID(h,
-                                       SubmodelComponentSnippetSuppliers.highLevelStateHandlerSupplier.getStandardSnippetIDs());
-                       idGetter.atomicHighLevelStateHandlerIDs = h -> getStandardID(h,
-                                       StandardHighLevelStateHandlerSnippetSuppliers.atomicHandlerSupplier.getStandardSnippetIDs());
-                       idGetter.subcomponentHighLevelStateHandlerIDs = h -> getStandardID(h,
-                                       StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier.getStandardSnippetIDs());
-                       SubmodelComponentSerializer.serialize(editor.toBeEdited, idGetter, savePath);
+                       SubmodelComponentSerializer.serialize(editor.toBeEdited, savePath);
                }
                catch (IOException e)
                {
@@ -75,18 +58,6 @@ public class SaveLoadManager
                }
        }
 
-       private static String getStandardID(Object o, Map<String, String> standardIDs)
-       {
-               return getStandardID(o, standardIDs, false);
-       }
-
-       private static String getStandardID(Object o, Map<String, String> standardIDs, boolean standardIDsHaveClassConcatenated)
-       {
-               String verboseID = (standardIDsHaveClassConcatenated ? "class:" : "") + o.getClass().getCanonicalName();
-               return standardIDs.entrySet().stream().filter(e -> e.getValue().equals(verboseID)).map(Entry::getKey).findAny()
-                               .orElseGet(() -> (standardIDsHaveClassConcatenated ? "" : "class:") + verboseID);
-       }
-
        public static void openLoadDialog() throws IOException
        {
                Shell fdShell = new Shell();
index f1bb5f0..7e2cfd8 100644 (file)
@@ -1,8 +1,17 @@
 package net.mograsim.logic.model.serializing;
 
+import static net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers.highLevelStateHandlerSupplier;
+import static net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers.outlineRendererSupplier;
+import static net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers.symbolRendererSupplier;
+import static net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandlerSnippetSuppliers.atomicHandlerSupplier;
+import static net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier;
+
+import java.util.Map;
+import java.util.Map.Entry;
 import java.util.function.Function;
 
 import net.mograsim.logic.model.model.components.GUIComponent;
+import net.mograsim.logic.model.snippets.CodeSnippetSupplier;
 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
 import net.mograsim.logic.model.snippets.Renderer;
 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.AtomicHighLevelStateHandler;
@@ -20,17 +29,33 @@ public class IdentifierGetter
        public Function<AtomicHighLevelStateHandler, String> atomicHighLevelStateHandlerIDs;
 
        /**
-        * Creates a new IdentifierGetter using "class:" concatenated with a component's / snippet's complete (canonical) class name as the
-        * default for all ID getter functions.
+        * Creates a new IdentifierGetter using the following as the default for all ID getter functions: <br>
+        * Define the verbose ID as <code>"class:" + canonicalClassName</code>.<br>
+        * If there is a standard ID mapping to this verbose ID recorded in the matching {@link CodeSnippetSupplier}, use this ID; if not, use
+        * the verbose ID.
         */
        public IdentifierGetter()
        {
-               Function<Object, String> defaultSnippetIDGetter = c -> "class:" + c.getClass().getCanonicalName();
-               this.componentIDs = defaultSnippetIDGetter::apply;
-               this.symbolRendererIDs = defaultSnippetIDGetter::apply;
-               this.outlineRendererIDs = defaultSnippetIDGetter::apply;
-               this.highLevelStateHandlerIDs = defaultSnippetIDGetter::apply;
-               this.subcomponentHighLevelStateHandlerIDs = defaultSnippetIDGetter::apply;
-               this.atomicHighLevelStateHandlerIDs = defaultSnippetIDGetter::apply;
+               componentIDs = generateStandardIDFunction(IndirectGUIComponentCreator.getStandardComponentIDs());
+               symbolRendererIDs = generateStandardIDFunction(symbolRendererSupplier);
+               outlineRendererIDs = generateStandardIDFunction(outlineRendererSupplier);
+               highLevelStateHandlerIDs = generateStandardIDFunction(highLevelStateHandlerSupplier);
+               atomicHighLevelStateHandlerIDs = generateStandardIDFunction(atomicHandlerSupplier);
+               subcomponentHighLevelStateHandlerIDs = generateStandardIDFunction(subcomponentHandlerSupplier);
+       }
+
+       private static <T> Function<T, String> generateStandardIDFunction(CodeSnippetSupplier<?, T> snippetSupplier)
+       {
+               return generateStandardIDFunction(snippetSupplier.getStandardSnippetIDs());
+       }
+
+       private static <T> Function<T, String> generateStandardIDFunction(Map<String, String> standardComponentIDs)
+       {
+               return t ->
+               {
+                       String verboseID = "class:" + t.getClass().getCanonicalName();
+                       return standardComponentIDs.entrySet().stream().filter(e -> e.getValue().equals(verboseID)).map(Entry::getKey).findAny()
+                                       .orElse(verboseID);
+               };
        }
 }
\ No newline at end of file
index 0016cc7..26d5a0d 100644 (file)
@@ -16,9 +16,11 @@ public class CodeSnippetSupplier<C, S>
                this.defaultSnippetSupplier = defaultSnippetSupplier;
        }
 
-       public void addStandardSnippetID(String standardSnippetID, String associatedSnippetClassName)
+       public void addStandardSnippetID(String standardSnippetID, String associatedSnippetID)
        {
-               standardSnippetIDClassNames.put(standardSnippetID, associatedSnippetClassName);
+               if (!associatedSnippetID.startsWith("class:"))
+                       throw new IllegalArgumentException("Unrecognized snippet ID format: " + associatedSnippetID);
+               standardSnippetIDClassNames.put(standardSnippetID, associatedSnippetID);
        }
 
        public Map<String, String> getStandardSnippetIDs()
@@ -36,13 +38,10 @@ public class CodeSnippetSupplier<C, S>
        {
                if (id != null)
                {
-                       String snippetClassName;
-                       if (id.startsWith("class:"))
-                               snippetClassName = id.substring(6);
-                       else
-                               snippetClassName = standardSnippetIDClassNames.get(id);
-                       if (snippetClassName != null)
+                       String resolvedID = resolveID(id);
+                       if (resolvedID != null)
                        {
+                               String snippetClassName = resolvedID.substring(6);
                                tryLoadSnippetClass(snippetClassName);
                                SnippetDefinintion<C, ?, S> snippetSupplier = snippetSuppliersForClassNames.get(snippetClassName);
                                if (snippetSupplier != null)
@@ -55,6 +54,13 @@ public class CodeSnippetSupplier<C, S>
                return defaultSnippetSupplier;
        }
 
+       public String resolveID(String id)
+       {
+               if (id.startsWith("class:"))
+                       return id;
+               return standardSnippetIDClassNames.get(id);
+       }
+
        // static helpers
 
        private static void tryLoadSnippetClass(String snippetClassName)
index 627f5dc..ef9d686 100644 (file)
@@ -1,11 +1,11 @@
 mograsim version: 0.1.3
 {
   "standardSubcomponentHandlerSuppliers": {
-    "delegating": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent.DelegatingSubcomponentHighLevelStateHandler"
+    "delegating": "class:net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent.DelegatingSubcomponentHighLevelStateHandler"
   },
   "standardAtomicHandlerSuppliers": {
-    "delegating": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.DelegatingAtomicHighLevelStateHandler",
-    "wireForcing": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.WireForcingAtomicHighLevelStateHandler",
-    "bitVectorSplitting": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.BitVectorSplittingAtomicHighLevelStateHandler"
+    "delegating": "class:net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.DelegatingAtomicHighLevelStateHandler",
+    "wireForcing": "class:net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.WireForcingAtomicHighLevelStateHandler",
+    "bitVectorSplitting": "class:net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.BitVectorSplittingAtomicHighLevelStateHandler"
   }
 }
\ No newline at end of file
index 9f7856f..0500769 100644 (file)
@@ -1,16 +1,16 @@
 mograsim version: 0.1.3
 {
   "standardOutlineRendererSuppliers": {
-    "default": "net.mograsim.logic.model.snippets.outlinerenderers.DefaultOutlineRenderer"
+    "default": "class:net.mograsim.logic.model.snippets.outlinerenderers.DefaultOutlineRenderer"
   },
   "standardSymbolRendererSuppliers": {
-    "default": "net.mograsim.logic.model.snippets.symbolrenderers.DefaultSymbolRenderer",
-    "centeredText": "net.mograsim.logic.model.snippets.symbolrenderers.CenteredTextSymbolRenderer",
-    "simpleRectangularLike": "net.mograsim.logic.model.snippets.symbolrenderers.SimpleRectangularLikeSymbolRenderer",
-    "pinNames": "net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer"
+    "default": "class:net.mograsim.logic.model.snippets.symbolrenderers.DefaultSymbolRenderer",
+    "centeredText": "class:net.mograsim.logic.model.snippets.symbolrenderers.CenteredTextSymbolRenderer",
+    "simpleRectangularLike": "class:net.mograsim.logic.model.snippets.symbolrenderers.SimpleRectangularLikeSymbolRenderer",
+    "pinNames": "class:net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer"
   },
   "standardHighLevelStateHandlerSuppliers": {
-    "default": "net.mograsim.logic.model.snippets.highlevelstatehandlers.DefaultHighLevelStateHandler",
-    "standard": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandler"
+    "default": "class:net.mograsim.logic.model.snippets.highlevelstatehandlers.DefaultHighLevelStateHandler",
+    "standard": "class:net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandler"
   }
 }
\ No newline at end of file