Did some clean up
authorChristian Femers <femers@in.tum.de>
Sun, 19 May 2019 20:39:19 +0000 (22:39 +0200)
committerChristian Femers <femers@in.tum.de>
Sun, 19 May 2019 20:39:19 +0000 (22:39 +0200)
Added @Override, replaced
Collections.unmodifiableList(Arrays.asList(...)) by List.of(...),
corrected documentation of Demux, deleted unused main(String[]) in
Simulation, made some variables package private (timeline and wires do
have their own packages anyway) to prevent synthetic accessors and other
small simplifications

16 files changed:
era.mi/src/era/mi/logic/Bit.java
era.mi/src/era/mi/logic/Simulation.java
era.mi/src/era/mi/logic/components/BasicComponent.java
era.mi/src/era/mi/logic/components/BitDisplay.java
era.mi/src/era/mi/logic/components/Clock.java
era.mi/src/era/mi/logic/components/Demux.java
era.mi/src/era/mi/logic/components/Merger.java
era.mi/src/era/mi/logic/components/Mux.java
era.mi/src/era/mi/logic/components/TriStateBuffer.java
era.mi/src/era/mi/logic/components/gates/MultiInputGate.java
era.mi/src/era/mi/logic/components/gates/NotGate.java
era.mi/src/era/mi/logic/tests/Connector.java [deleted file]
era.mi/src/era/mi/logic/tests/GUITest.java
era.mi/src/era/mi/logic/timeline/Timeline.java
era.mi/src/era/mi/logic/timeline/TimelineEvent.java
era.mi/src/era/mi/logic/wires/WireArray.java

index ff424c7..1118443 100644 (file)
@@ -77,28 +77,28 @@ public enum Bit
        }\r
 \r
        // @formatter:off\r
-       private static Bit[][] JOIN_TABLE = \r
+       private static final Bit[][] JOIN_TABLE = \r
                { { U, U, U,    U,   U    }, \r
                  { U, X, X,    X,   X    }, \r
                  { U, X, ZERO, X,   ZERO },\r
                  { U, X, X,    ONE, ONE  }, \r
                  { U, X, ZERO, ONE, Z    } };\r
 \r
-       private static Bit[][] AND_TABLE = \r
+       private static final Bit[][] AND_TABLE = \r
                { { U,    U,    ZERO, U,    U    }, \r
                  { U,    X,    ZERO, X,    X    },\r
                  { ZERO, ZERO, ZERO, ZERO, ZERO }, \r
                  { U,    X,    ZERO, ONE,  X    }, \r
                  { U,    X,    ZERO, X,    X    } };\r
 \r
-       private static Bit[][] OR_TABLE =\r
+       private static final Bit[][] OR_TABLE =\r
        { { U,   U,   U,    ONE, U    },    \r
          { U,   X,   X,    ONE, X    },    \r
          { U,   X,   ZERO, ONE, X    },    \r
          { ONE, ONE, ONE,  ONE, ONE  },    \r
          { U,   X,   X,    ONE, X    } };\r
        \r
-       private static Bit[][] XOR_TABLE =\r
+       private static final Bit[][] XOR_TABLE =\r
        { { U, U, U,    U,    U },    \r
          { U, X, X,    X,    X },    \r
          { U, X, ZERO, ONE,  X },    \r
index 20ffbaf..ca2c9e9 100644 (file)
@@ -6,7 +6,4 @@ public class Simulation
 {\r
        public final static Timeline TIMELINE = new Timeline(11);\r
 \r
-       public static void main(String[] args)\r
-       {\r
-       }\r
 }
\ No newline at end of file
index 204f3a6..3e5b6e0 100644 (file)
@@ -28,10 +28,7 @@ public abstract class BasicComponent implements WireArrayObserver, Component
        @Override\r
        public void update(WireArray initiator, Bit[] oldValues)\r
        {\r
-               Simulation.TIMELINE.addEvent((e) ->\r
-               {\r
-                       compute();\r
-               }, processTime);\r
+               Simulation.TIMELINE.addEvent(e -> compute(), processTime);\r
        }\r
 \r
        protected abstract void compute();\r
index fd1a88c..38fc2b0 100644 (file)
@@ -1,8 +1,6 @@
 package era.mi.logic.components;\r
 \r
-import java.util.ArrayList;\r
 import java.util.Arrays;\r
-import java.util.Collections;\r
 import java.util.List;\r
 \r
 import era.mi.logic.Bit;\r
@@ -40,12 +38,12 @@ public class BitDisplay extends BasicComponent
        @Override\r
        public List<WireArray> getAllInputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList(in));\r
+               return List.of(in);\r
        }\r
 \r
        @Override\r
        public List<WireArray> getAllOutputs()\r
        {\r
-               return Collections.unmodifiableList(new ArrayList<WireArray>());\r
+               return List.of();\r
        }\r
 }\r
index ac47dbf..2809027 100644 (file)
@@ -1,7 +1,5 @@
 package era.mi.logic.components;\r
 \r
-import java.util.Arrays;\r
-import java.util.Collections;\r
 import java.util.List;\r
 \r
 import era.mi.logic.Bit;\r
@@ -26,14 +24,14 @@ public class Clock implements TimelineEventHandler, Component
        {\r
                this.delta = delta;\r
                this.outI = out.createInput();\r
-               Simulation.TIMELINE.addEvent(this, 50);\r
+               Simulation.TIMELINE.addEvent(this, delta);\r
        }\r
 \r
        @Override\r
        public void handle(TimelineEvent e)\r
        {\r
                addToTimeline();\r
-               outI.feedSignals(new Bit[] { toggle ? Bit.ONE : Bit.ZERO });\r
+               outI.feedSignals(toggle ? Bit.ONE : Bit.ZERO);\r
                toggle = !toggle;\r
        }\r
 \r
@@ -50,12 +48,12 @@ public class Clock implements TimelineEventHandler, Component
        @Override\r
        public List<WireArray> getAllInputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList());\r
+               return List.of();\r
        }\r
 \r
        @Override\r
        public List<WireArray> getAllOutputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList(outI.owner));\r
+               return List.of(outI.owner);\r
        }\r
 }\r
index 64f5d26..24fd29b 100644 (file)
@@ -1,15 +1,13 @@
 package era.mi.logic.components;\r
 \r
-import java.util.Arrays;\r
-import java.util.Collections;\r
 import java.util.List;\r
 \r
 import era.mi.logic.wires.WireArray;\r
 import era.mi.logic.wires.WireArray.WireArrayEnd;\r
 \r
 /**\r
- * Models a multiplexer. Takes an arbitrary amount of input {@link WireArray}s, one of which, as determined by select, is put through to the\r
- * output.\r
+ * Models a multiplexer. Takes an arbitrary amount of outputs {@link WireArray}s, one of which, as determined by select, receives the input\r
+ * signal.\r
  * \r
  * @author Fabian Stemmler\r
  *\r
@@ -23,11 +21,11 @@ public class Demux extends BasicComponent
        private int selected = -1;\r
 \r
        /**\r
-        * Input {@link WireArray}s and out must be of uniform length\r
+        * Output {@link WireArray}s and in must be of uniform length\r
         * \r
-        * @param out     Must be of uniform length with all inputs.\r
-        * @param select  Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.\r
-        * @param outputs One of these inputs is mapped to the output, depending on the select bits\r
+        * @param in      Must be of uniform length with all outputs.\r
+        * @param select  Indexes the output array to which the input is mapped. Must have enough bits to index all outputs.\r
+        * @param outputs One of these outputs receives the input signal, depending on the select bits\r
         */\r
        public Demux(int processTime, WireArray in, WireArray select, WireArray... outputs)\r
        {\r
@@ -73,12 +71,12 @@ public class Demux extends BasicComponent
        @Override\r
        public List<WireArray> getAllInputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList(in, select));\r
+               return List.of(in, select);\r
        }\r
 \r
        @Override\r
        public List<WireArray> getAllOutputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList(outputs));\r
+               return List.of(outputs);\r
        }\r
 }\r
index 75884f8..a738114 100644 (file)
@@ -1,7 +1,5 @@
 package era.mi.logic.components;\r
 \r
-import java.util.Arrays;\r
-import java.util.Collections;\r
 import java.util.List;\r
 \r
 import era.mi.logic.Bit;\r
@@ -73,12 +71,12 @@ public class Merger implements WireArrayObserver, Component
        @Override\r
        public List<WireArray> getAllInputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList(inputs));\r
+               return List.of(inputs);\r
        }\r
 \r
        @Override\r
        public List<WireArray> getAllOutputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList(outI.owner));\r
+               return List.of(outI.owner);\r
        }\r
 }\r
index 6003723..6ac32c6 100644 (file)
@@ -88,6 +88,6 @@ public class Mux extends BasicComponent
        @Override\r
        public List<WireArray> getAllOutputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList(outI.owner));\r
+               return List.of(outI.owner);\r
        }\r
 }\r
index 8a41424..f672a2d 100644 (file)
@@ -1,7 +1,5 @@
 package era.mi.logic.components;\r
 \r
-import java.util.Arrays;\r
-import java.util.Collections;\r
 import java.util.List;\r
 \r
 import era.mi.logic.Bit;\r
@@ -40,13 +38,13 @@ public class TriStateBuffer extends BasicComponent
        @Override\r
        public List<WireArray> getAllInputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList(in, enable));\r
+               return List.of(in, enable);\r
        }\r
 \r
        @Override\r
        public List<WireArray> getAllOutputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList(outI.owner));\r
+               return List.of(outI.owner);\r
        }\r
 \r
 }\r
index faf0eb6..2094b84 100644 (file)
@@ -1,7 +1,5 @@
 package era.mi.logic.components.gates;\r
 \r
-import java.util.Arrays;\r
-import java.util.Collections;\r
 import java.util.List;\r
 \r
 import era.mi.logic.Bit;\r
@@ -38,15 +36,16 @@ public abstract class MultiInputGate extends BasicComponent
        @Override\r
        public List<WireArray> getAllInputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList(in));\r
+               return List.of(in);\r
        }\r
 \r
        @Override\r
        public List<WireArray> getAllOutputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList(out));\r
+               return List.of(out);\r
        }\r
 \r
+       @Override\r
        protected void compute()\r
        {\r
                Bit[] result = in[0].getValues();\r
index 79c7f3e..eaa9aec 100644 (file)
@@ -1,7 +1,5 @@
 package era.mi.logic.components.gates;\r
 \r
-import java.util.Arrays;\r
-import java.util.Collections;\r
 import java.util.List;\r
 \r
 import era.mi.logic.Util;\r
@@ -23,6 +21,7 @@ public class NotGate extends BasicComponent
                outI = out.createInput();\r
        }\r
 \r
+       @Override\r
        public void compute()\r
        {\r
                outI.feedSignals(Util.not(in.getValues()));\r
@@ -41,12 +40,12 @@ public class NotGate extends BasicComponent
        @Override\r
        public List<WireArray> getAllInputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList(in));\r
+               return List.of(in);\r
        }\r
 \r
        @Override\r
        public List<WireArray> getAllOutputs()\r
        {\r
-               return Collections.unmodifiableList(Arrays.asList(out));\r
+               return List.of(out);\r
        }\r
 }\r
diff --git a/era.mi/src/era/mi/logic/tests/Connector.java b/era.mi/src/era/mi/logic/tests/Connector.java
deleted file mode 100644 (file)
index 5e9c576..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-package era.mi.logic.tests;\r
-\r
-import era.mi.logic.Bit;\r
-import era.mi.logic.Simulation;\r
-import era.mi.logic.wires.WireArray;\r
-import era.mi.logic.wires.WireArray.WireArrayEnd;\r
-import era.mi.logic.wires.WireArrayObserver;\r
-\r
-public class Connector implements WireArrayObserver\r
-{\r
-       private final WireArray a;\r
-//     private final WireArray b;\r
-       private final WireArrayEnd aI;\r
-       private final WireArrayEnd bI;\r
-\r
-       public Connector(WireArray a, WireArray b)\r
-       {\r
-               if (a.length != b.length)\r
-                       throw new IllegalArgumentException(String.format("WireArray width does not match: %d, %d", a.length, b.length));\r
-               this.a = a;\r
-//             this.b = b;\r
-               a.addObserver(this);\r
-               b.addObserver(this);\r
-               aI = a.createInput();\r
-               bI = b.createInput();\r
-       }\r
-\r
-       @Override\r
-       public void update(WireArray initiator, Bit[] oldValues)\r
-       {\r
-               Simulation.TIMELINE.addEvent((e) ->\r
-               {\r
-                       if (initiator == a)\r
-                               bI.feedSignals(aI.wireValuesExcludingMe());\r
-                       else\r
-                               aI.feedSignals(bI.wireValuesExcludingMe());\r
-               }, 1);\r
-       }\r
-}\r
index 28dd67a..54f9269 100644 (file)
@@ -170,7 +170,7 @@ public class GUITest extends JPanel
                g.setFont(g.getFont().deriveFont(Math.min(height, width) / 40f));\r
        }\r
 \r
-       private void drawString(Graphics g, String s, int x, int y, double anchorX, double anchorY)\r
+       private static void drawString(Graphics g, String s, int x, int y, double anchorX, double anchorY)\r
        {\r
                int h = g.getFontMetrics().getAscent();\r
                int w = g.getFontMetrics().stringWidth(s);\r
index 1055d1c..9e64938 100644 (file)
@@ -90,8 +90,7 @@ public class Timeline
        {\r
                if (!hasNext())\r
                        return -1;\r
-               else\r
-                       return events.peek().timing;\r
+               return events.peek().timing;\r
        }\r
 \r
        public void reset()\r
@@ -127,7 +126,7 @@ public class Timeline
        private class InnerEvent\r
        {\r
 \r
-               private final long timing;\r
+               final long timing;\r
                private final TimelineEventHandler function;\r
                private final TimelineEvent event;\r
 \r
index 26799a3..cc37c99 100644 (file)
@@ -22,6 +22,7 @@ public class TimelineEvent
                return timing;\r
        }\r
 \r
+       @Override\r
        public String toString()\r
        {\r
                return "timestamp: " + timing;\r
index 033d1d4..ca93fcb 100644 (file)
@@ -22,7 +22,7 @@ public class WireArray
        public final int travelTime;\r
        private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();\r
        public final int length;\r
-       private List<WireArrayEnd> inputs = new ArrayList<WireArrayEnd>();\r
+       List<WireArrayEnd> inputs = new ArrayList<WireArrayEnd>();\r
 \r
        public WireArray(int length, int travelTime)\r
        {\r
@@ -73,7 +73,7 @@ public class WireArray
                }\r
        }\r
 \r
-       private void recalculate()\r
+       void recalculate()\r
        {\r
                switch (inputs.size())\r
                {\r
@@ -223,7 +223,7 @@ public class WireArray
                return new WireArrayEnd(this);\r
        }\r
 \r
-       private void registerInput(WireArrayEnd toRegister)\r
+       void registerInput(WireArrayEnd toRegister)\r
        {\r
                inputs.add(toRegister);\r
        }\r
@@ -239,9 +239,9 @@ public class WireArray
        {\r
                public final WireArray owner;\r
                private boolean open;\r
-               private Bit[] inputValues;\r
+               Bit[] inputValues;\r
 \r
-               private WireArrayEnd(WireArray owner)\r
+               WireArrayEnd(WireArray owner)\r
                {\r
                        super();\r
                        this.owner = owner;\r
@@ -264,12 +264,11 @@ public class WireArray
                 */\r
                public void feedSignals(Bit... newValues)\r
                {\r
-                       if (newValues.length == length)\r
-                       {\r
-                               feedSignals(0, newValues);\r
-                       } else\r
+                       if (newValues.length != length)\r
                                throw new IllegalArgumentException(\r
                                                String.format("Attempted to input %d bits instead of %d bits.", newValues.length, length));\r
+                       feedSignals(0, newValues);\r
+\r
                }\r
 \r
                /**\r