Added file version and closed resource leak
authorFabian Stemmler <stemmler@in.tum.de>
Tue, 18 Jun 2019 08:17:48 +0000 (10:17 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Tue, 18 Jun 2019 08:17:48 +0000 (10:17 +0200)
Component json files are now created with the version in the first line.
Removed a superfluous FileWriter in JsonHandler.

14 files changed:
net.mograsim.logic.ui/FullAdder.json
net.mograsim.logic.ui/HalfAdder.json
net.mograsim.logic.ui/Test.json
net.mograsim.logic.ui/src/net/mograsim/logic/ui/examples/JsonExample.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUICustomComponentCreator.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SimpleRectangularSubmodelComponent.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponentParams.java [new file with mode: 0644]
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/ComponentCompositionParams.java [deleted file]
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/JsonHandler.java [deleted file]
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/SubmodelComponentParams.java [deleted file]
net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java [new file with mode: 0644]
net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/Version.java [new file with mode: 0644]

index 4740f05..782652d 100644 (file)
@@ -1,3 +1,4 @@
+mograsim version: 0.1.0
 {
   "type": "SimpleRectangularSubmodelComponent",
   "width": 35.0,
index 99fd0f4..802ba0d 100644 (file)
@@ -1,3 +1,4 @@
+mograsim version: 0.1.0
 {
   "type": "SimpleRectangularSubmodelComponent",
   "width": 35.0,
index 4e376db..36e1ca9 100644 (file)
@@ -1,3 +1,4 @@
+mograsim version: 0.1.0
 {
   "type": "SimpleRectangularSubmodelComponent",
   "width": 35.0,
index 82b2c8c..9cc65b3 100644 (file)
@@ -9,9 +9,9 @@ import net.mograsim.logic.ui.model.components.GUICustomComponentCreator;
 import net.mograsim.logic.ui.model.components.GUIManualSwitch;
 import net.mograsim.logic.ui.model.components.SimpleRectangularSubmodelComponent;
 import net.mograsim.logic.ui.model.components.SubmodelComponent;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams;
 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIfulladder;
 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIhalfadder;
-import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams;
 import net.mograsim.logic.ui.model.wires.GUIWire;
 
 public class JsonExample
@@ -76,7 +76,8 @@ public class JsonExample
                        e.printStackTrace();
                }
 
-               SimpleRectangularSubmodelComponent adder = new GUIfulladder(model);
+               SimpleRectangularSubmodelComponent adder = (SimpleRectangularSubmodelComponent) GUICustomComponentCreator.create(model,
+                               "FullAdder.json");
 
                GUIManualSwitch swA = new GUIManualSwitch(model);
                swA.moveTo(0, 0);
index 89c532e..7254d8b 100644 (file)
@@ -9,7 +9,6 @@ import java.util.function.Supplier;
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
-import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams;
 import net.mograsim.logic.ui.model.wires.Pin;
 
 public abstract class GUIComponent
index c150397..ea45b61 100644 (file)
@@ -6,11 +6,10 @@ import java.lang.reflect.InvocationTargetException;
 import java.util.Map;
 
 import net.mograsim.logic.ui.model.ViewModelModifiable;
-import net.mograsim.logic.ui.model.components.params.ComponentCompositionParams;
-import net.mograsim.logic.ui.model.components.params.ComponentCompositionParams.InnerComponentParams;
-import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams;
-import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams.InnerWireParams;
-import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams.InterfacePinParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerWireParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InterfacePinParams;
 import net.mograsim.logic.ui.model.wires.GUIWire;
 
 /**
@@ -21,8 +20,9 @@ public final class GUICustomComponentCreator
        private static final String rectC = SimpleRectangularSubmodelComponent.class.getSimpleName();
 
        /**
-        * Creates a {@link SubmodelComponent} from the {@link SubmodelComponentParams}, specified at the given path. The returned SubmodelComponent
-        * can also be e.g. a {@link SimpleRectangularSubmodelComponent}, depending on what the {@link SubmodelComponentParams} describe.
+        * Creates a {@link SubmodelComponent} from the {@link SubmodelComponentParams}, specified at the given path. The returned
+        * SubmodelComponent can also be e.g. a {@link SimpleRectangularSubmodelComponent}, depending on what the
+        * {@link SubmodelComponentParams} describe.
         * 
         * @param path The path of the file describing the {@link SubmodelComponentParams}, which define the new {@link SubmodelComponent}
         * @return A new SubmodelComponent, as described in the file located at the given path
@@ -44,8 +44,8 @@ public final class GUICustomComponentCreator
        }
 
        /**
-        * Creates a {@link SubmodelComponent} from the specified {@link SubmodelComponentParams}. The returned SubmodelComponent can also be e.g. a
-        * {@link SimpleRectangularSubmodelComponent}, depending on what the {@link SubmodelComponentParams} describe.
+        * Creates a {@link SubmodelComponent} from the specified {@link SubmodelComponentParams}. The returned SubmodelComponent can also be
+        * e.g. a {@link SimpleRectangularSubmodelComponent}, depending on what the {@link SubmodelComponentParams} describe.
         * 
         * @param params The parameters describing the {@link SubmodelComponent}
         * @param path   This value is used when the new {@link SubmodelComponent} is an inner component to a different SubmodelComponent, which
index da8bd48..51e5b28 100644 (file)
@@ -11,7 +11,6 @@ import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
-import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams;
 import net.mograsim.logic.ui.model.wires.Pin;
 
 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
index f82c377..16c8c2a 100644 (file)
@@ -15,14 +15,13 @@ import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.ui.LogicUIRenderer;
 import net.mograsim.logic.ui.model.ViewModel;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
-import net.mograsim.logic.ui.model.components.params.ComponentCompositionParams;
-import net.mograsim.logic.ui.model.components.params.ComponentCompositionParams.InnerComponentParams;
-import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams;
-import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams.InterfacePinParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerPinParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerWireParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InterfacePinParams;
 import net.mograsim.logic.ui.model.wires.GUIWire;
 import net.mograsim.logic.ui.model.wires.Pin;
-import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams.InnerPinParams;
-import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams.InnerWireParams;
 
 public abstract class SubmodelComponent extends GUIComponent
 {
diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponentParams.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponentParams.java
new file mode 100644 (file)
index 0000000..5fd7ab5
--- /dev/null
@@ -0,0 +1,72 @@
+package net.mograsim.logic.ui.model.components;
+
+import java.io.IOException;
+import java.util.Map;
+
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+import net.mograsim.logic.ui.util.JsonHandler;
+
+/**
+ * This class contains all the information necessary to create a new {@link SubmodelComponent}
+ */
+public class SubmodelComponentParams
+{
+       String type;
+       double width, height;
+       InterfacePinParams[] interfacePins;
+       ComponentCompositionParams composition;
+       Map<String, Object> specialized;
+
+       public static class InterfacePinParams
+       {
+               public Point location;
+               public int logicWidth;
+       }
+
+       public static class InnerWireParams
+       {
+               public InnerPinParams pin1, pin2;
+               public Point[] path;
+       }
+
+       public static class InnerPinParams
+       {
+               public int compId, pinIndex;
+       }
+
+       public static class ComponentCompositionParams
+       {
+               double innerScale;
+               InnerComponentParams[] subComps;
+               InnerWireParams[] innerWires;
+
+               public static class InnerComponentParams
+               {
+                       public Point pos;
+                       public String type;
+                       public int logicWidth;
+               }
+       }
+
+       public static SubmodelComponentParams readJson(String path) throws IOException
+       {
+               return JsonHandler.readJson(path, SubmodelComponentParams.class);
+       }
+
+       /**
+        * Writes this {@link SubmodelComponentParams} object into a file in json format. The correct file extension is important! Check
+        * {@link SubmodelComponentParams}.fileExtension
+        */
+       public void writeJson(String path)
+       {
+               try
+               {
+                       JsonHandler.writeJson(this, path);
+               }
+               catch (IOException e)
+               {
+                       System.err.println("Failed to write SubComponentParams to file");
+                       e.printStackTrace();
+               }
+       }
+}
\ No newline at end of file
diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/ComponentCompositionParams.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/ComponentCompositionParams.java
deleted file mode 100644 (file)
index 67cbdfe..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-package net.mograsim.logic.ui.model.components.params;
-
-import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
-import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams.InnerWireParams;
-
-public class ComponentCompositionParams
-{
-       public double innerScale;
-       public InnerComponentParams[] subComps;
-       public InnerWireParams[] innerWires;
-
-       public static class InnerComponentParams
-       {
-               public Point pos;
-               public String type;
-               public int logicWidth;
-       }
-}
diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/JsonHandler.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/JsonHandler.java
deleted file mode 100644 (file)
index c80aba7..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-package net.mograsim.logic.ui.model.components.params;
-
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-
-public class JsonHandler
-{
-       private static Gson parser = new GsonBuilder().setPrettyPrinting().create();
-
-       @SuppressWarnings("resource")
-       public static <T> T readJson(String path, Class<T> type) throws IOException
-       {
-               FileReader reader = new FileReader(path);
-               T params = parser.fromJson(new FileReader(path), type);
-               reader.close();
-               return params;
-       }
-
-       public static void writeJson(Object o, String path) throws IOException
-       {
-               @SuppressWarnings("resource")
-               FileWriter writer = new FileWriter(path);
-               writer.write(parser.toJson(o));
-               writer.close(); // TODO: Insure that writer is closed
-       }
-}
diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/SubmodelComponentParams.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/params/SubmodelComponentParams.java
deleted file mode 100644 (file)
index e0205c0..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-package net.mograsim.logic.ui.model.components.params;
-
-import java.io.IOException;
-import java.util.Map;
-
-import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
-import net.mograsim.logic.ui.model.components.SubmodelComponent;
-
-/**
- * This class contains all the information necessary to create a new {@link SubmodelComponent}
- */
-public class SubmodelComponentParams
-{
-       public String type;
-       public double width, height;
-       public InterfacePinParams[] interfacePins;
-       public ComponentCompositionParams composition;
-       public Map<String, Object> specialized;
-
-       public static class InterfacePinParams
-       {
-               public Point location;
-               public int logicWidth;
-       }
-
-       public static class InnerWireParams
-       {
-               public InnerPinParams pin1, pin2;
-               public Point[] path;
-       }
-
-       public static class InnerPinParams
-       {
-               public int compId, pinIndex;
-       }
-
-       public static SubmodelComponentParams readJson(String path) throws IOException
-       {
-               return JsonHandler.readJson(path, SubmodelComponentParams.class);
-       }
-
-       /**
-        * Writes this {@link SubmodelComponentParams} object into a file in json format. The correct file extension is important! Check
-        * {@link SubmodelComponentParams}.fileExtension
-        */
-       public void writeJson(String path)
-       {
-               try
-               {
-                       JsonHandler.writeJson(this, path);
-               }
-               catch (IOException e)
-               {
-                       System.err.println("Failed to write SubComponentParams to file");
-                       e.printStackTrace();
-               }
-       }
-}
\ No newline at end of file
diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java
new file mode 100644 (file)
index 0000000..94231a3
--- /dev/null
@@ -0,0 +1,35 @@
+package net.mograsim.logic.ui.util;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+public class JsonHandler
+{
+       private static Gson parser = new GsonBuilder().setPrettyPrinting().create();
+
+       @SuppressWarnings("resource")
+       public static <T> T readJson(String path, Class<T> type) throws IOException
+       {
+               FileReader reader = new FileReader(path);
+               BufferedReader bf = new BufferedReader(reader);
+               bf.readLine(); // Skip version
+               String json = bf.lines().dropWhile(s -> s.length() == 0 || s.charAt(0) != '{').reduce("", (x, y) -> x.concat(y));
+               T params = parser.fromJson(json, type);
+               reader.close();
+               return params;
+       }
+
+       public static void writeJson(Object o, String path) throws IOException
+       {
+               @SuppressWarnings("resource")
+               FileWriter writer = new FileWriter(path);
+               writer.write(String.format("mograsim version: %s\n", Version.current.toString()));
+               writer.write(parser.toJson(o));
+               writer.close(); // TODO: Insure that writer is closed
+       }
+}
diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/Version.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/Version.java
new file mode 100644 (file)
index 0000000..cfd168e
--- /dev/null
@@ -0,0 +1,69 @@
+package net.mograsim.logic.ui.util;
+
+public final class Version
+{
+       public final static Version current = new Version(0, 1, 0);
+       public final int major, minor, patch;
+
+       public Version(int major, int minor, int patch)
+       {
+               super();
+               this.major = major;
+               this.minor = minor;
+               this.patch = patch;
+       }
+
+       public int[] getVersionNumbers()
+       {
+               return new int[] { major, minor, patch };
+       }
+
+       @Override
+       public String toString()
+       {
+               return major + "." + minor + "." + patch;
+       }
+
+       @Override
+       public int hashCode()
+       {
+               final int prime = 31;
+               int result = 1;
+               result = prime * result + major;
+               result = prime * result + minor;
+               result = prime * result + patch;
+               return result;
+       }
+
+       @Override
+       public boolean equals(Object obj)
+       {
+               if (this == obj)
+                       return true;
+               if (!(obj instanceof Version))
+                       return false;
+               Version other = (Version) obj;
+               if (major != other.major)
+                       return false;
+               if (minor != other.minor)
+                       return false;
+               if (patch != other.patch)
+                       return false;
+               return true;
+       }
+
+       public boolean is(int major)
+       {
+               return major != this.major;
+       }
+
+       public boolean is(int major, int minor)
+       {
+               return is(major) && this.minor == minor;
+       }
+
+       public boolean is(int major, int minor, int patch)
+       {
+               return is(major, minor) && this.patch == patch;
+       }
+}
\ No newline at end of file