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 da9a227..992c729 100644 (file)
@@ -77,28 +77,28 @@ public enum Bit
        }
 
        // @formatter:off
-       private static Bit[][] JOIN_TABLE = 
+       private static final Bit[][] JOIN_TABLE = 
                { { U, U, U,    U,   U    }, 
                  { U, X, X,    X,   X    }, 
                  { U, X, ZERO, X,   ZERO },
                  { U, X, X,    ONE, ONE  }, 
                  { U, X, ZERO, ONE, Z    } };
 
-       private static Bit[][] AND_TABLE = 
+       private static final Bit[][] AND_TABLE = 
                { { U,    U,    ZERO, U,    U    }, 
                  { U,    X,    ZERO, X,    X    },
                  { ZERO, ZERO, ZERO, ZERO, ZERO }, 
                  { U,    X,    ZERO, ONE,  X    }, 
                  { U,    X,    ZERO, X,    X    } };
 
-       private static Bit[][] OR_TABLE =
+       private static final Bit[][] OR_TABLE =
        { { U,   U,   U,    ONE, U    },    
          { U,   X,   X,    ONE, X    },    
          { U,   X,   ZERO, ONE, X    },    
          { ONE, ONE, ONE,  ONE, ONE  },    
          { U,   X,   X,    ONE, X    } };
        
-       private static Bit[][] XOR_TABLE =
+       private static final Bit[][] XOR_TABLE =
        { { U, U, U,    U,    U },    
          { U, X, X,    X,    X },    
          { U, X, ZERO, ONE,  X },    
index f4b25d2..0468ec1 100644 (file)
@@ -6,7 +6,4 @@ public class Simulation
 {
        public final static Timeline TIMELINE = new Timeline(11);
 
-       public static void main(String[] args)
-       {
-       }
 }
\ No newline at end of file
index 509c662..2956fe0 100644 (file)
@@ -28,10 +28,7 @@ public abstract class BasicComponent implements WireArrayObserver, Component
        @Override
        public void update(WireArray initiator, Bit[] oldValues)
        {
-               Simulation.TIMELINE.addEvent((e) ->
-               {
-                       compute();
-               }, processTime);
+               Simulation.TIMELINE.addEvent(e -> compute(), processTime);
        }
 
        protected abstract void compute();
index 051010b..bd9714a 100644 (file)
@@ -1,8 +1,6 @@
 package era.mi.logic.components;
 
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
 
 import era.mi.logic.Bit;
@@ -40,12 +38,12 @@ public class BitDisplay extends BasicComponent
        @Override
        public List<WireArray> getAllInputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(in));
+               return List.of(in);
        }
 
        @Override
        public List<WireArray> getAllOutputs()
        {
-               return Collections.unmodifiableList(new ArrayList<WireArray>());
+               return List.of();
        }
 }
index 56f1110..d95cbc7 100644 (file)
@@ -1,7 +1,5 @@
 package era.mi.logic.components;
 
-import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
 
 import era.mi.logic.Bit;
@@ -26,14 +24,14 @@ public class Clock implements TimelineEventHandler, Component
        {
                this.delta = delta;
                this.outI = out.createInput();
-               Simulation.TIMELINE.addEvent(this, 50);
+               Simulation.TIMELINE.addEvent(this, delta);
        }
 
        @Override
        public void handle(TimelineEvent e)
        {
                addToTimeline();
-               outI.feedSignals(new Bit[] { toggle ? Bit.ONE : Bit.ZERO });
+               outI.feedSignals(toggle ? Bit.ONE : Bit.ZERO);
                toggle = !toggle;
        }
 
@@ -50,12 +48,12 @@ public class Clock implements TimelineEventHandler, Component
        @Override
        public List<WireArray> getAllInputs()
        {
-               return Collections.unmodifiableList(Arrays.asList());
+               return List.of();
        }
 
        @Override
        public List<WireArray> getAllOutputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(outI.owner));
+               return List.of(outI.owner);
        }
 }
index b7ed0ff..762d806 100644 (file)
@@ -1,15 +1,13 @@
 package era.mi.logic.components;
 
-import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
 
 import era.mi.logic.wires.WireArray;
 import era.mi.logic.wires.WireArray.WireArrayEnd;
 
 /**
- * Models a multiplexer. Takes an arbitrary amount of input {@link WireArray}s, one of which, as determined by select, is put through to the
- * output.
+ * Models a multiplexer. Takes an arbitrary amount of outputs {@link WireArray}s, one of which, as determined by select, receives the input
+ * signal.
  * 
  * @author Fabian Stemmler
  *
@@ -23,11 +21,11 @@ public class Demux extends BasicComponent
        private int selected = -1;
 
        /**
-        * Input {@link WireArray}s and out must be of uniform length
+        * Output {@link WireArray}s and in must be of uniform length
         * 
-        * @param out     Must be of uniform length with all inputs.
-        * @param select  Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.
-        * @param outputs One of these inputs is mapped to the output, depending on the select bits
+        * @param in      Must be of uniform length with all outputs.
+        * @param select  Indexes the output array to which the input is mapped. Must have enough bits to index all outputs.
+        * @param outputs One of these outputs receives the input signal, depending on the select bits
         */
        public Demux(int processTime, WireArray in, WireArray select, WireArray... outputs)
        {
@@ -73,12 +71,12 @@ public class Demux extends BasicComponent
        @Override
        public List<WireArray> getAllInputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(in, select));
+               return List.of(in, select);
        }
 
        @Override
        public List<WireArray> getAllOutputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(outputs));
+               return List.of(outputs);
        }
 }
index 17d1b57..da9b55e 100644 (file)
@@ -1,7 +1,5 @@
 package era.mi.logic.components;
 
-import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
 
 import era.mi.logic.Bit;
@@ -73,12 +71,12 @@ public class Merger implements WireArrayObserver, Component
        @Override
        public List<WireArray> getAllInputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(inputs));
+               return List.of(inputs);
        }
 
        @Override
        public List<WireArray> getAllOutputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(outI.owner));
+               return List.of(outI.owner);
        }
 }
index d6307f9..1768096 100644 (file)
@@ -88,6 +88,6 @@ public class Mux extends BasicComponent
        @Override
        public List<WireArray> getAllOutputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(outI.owner));
+               return List.of(outI.owner);
        }
 }
index 5c02a4e..0a7133f 100644 (file)
@@ -1,7 +1,5 @@
 package era.mi.logic.components;
 
-import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
 
 import era.mi.logic.Bit;
@@ -40,13 +38,13 @@ public class TriStateBuffer extends BasicComponent
        @Override
        public List<WireArray> getAllInputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(in, enable));
+               return List.of(in, enable);
        }
 
        @Override
        public List<WireArray> getAllOutputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(outI.owner));
+               return List.of(outI.owner);
        }
 
 }
index d26297b..fac267a 100644 (file)
@@ -1,7 +1,5 @@
 package era.mi.logic.components.gates;
 
-import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
 
 import era.mi.logic.Bit;
@@ -38,15 +36,16 @@ public abstract class MultiInputGate extends BasicComponent
        @Override
        public List<WireArray> getAllInputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(in));
+               return List.of(in);
        }
 
        @Override
        public List<WireArray> getAllOutputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(out));
+               return List.of(out);
        }
 
+       @Override
        protected void compute()
        {
                Bit[] result = in[0].getValues();
index c482115..f664471 100644 (file)
@@ -1,7 +1,5 @@
 package era.mi.logic.components.gates;
 
-import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
 
 import era.mi.logic.Util;
@@ -23,6 +21,7 @@ public class NotGate extends BasicComponent
                outI = out.createInput();
        }
 
+       @Override
        public void compute()
        {
                outI.feedSignals(Util.not(in.getValues()));
@@ -41,12 +40,12 @@ public class NotGate extends BasicComponent
        @Override
        public List<WireArray> getAllInputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(in));
+               return List.of(in);
        }
 
        @Override
        public List<WireArray> getAllOutputs()
        {
-               return Collections.unmodifiableList(Arrays.asList(out));
+               return List.of(out);
        }
 }
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 9a8d3fc..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-package era.mi.logic.tests;
-
-import era.mi.logic.Bit;
-import era.mi.logic.Simulation;
-import era.mi.logic.wires.WireArray;
-import era.mi.logic.wires.WireArray.WireArrayEnd;
-import era.mi.logic.wires.WireArrayObserver;
-
-public class Connector implements WireArrayObserver
-{
-       private final WireArray a;
-//     private final WireArray b;
-       private final WireArrayEnd aI;
-       private final WireArrayEnd bI;
-
-       public Connector(WireArray a, WireArray b)
-       {
-               if (a.length != b.length)
-                       throw new IllegalArgumentException(String.format("WireArray width does not match: %d, %d", a.length, b.length));
-               this.a = a;
-//             this.b = b;
-               a.addObserver(this);
-               b.addObserver(this);
-               aI = a.createInput();
-               bI = b.createInput();
-       }
-
-       @Override
-       public void update(WireArray initiator, Bit[] oldValues)
-       {
-               Simulation.TIMELINE.addEvent((e) ->
-               {
-                       if (initiator == a)
-                               bI.feedSignals(aI.wireValuesExcludingMe());
-                       else
-                               aI.feedSignals(bI.wireValuesExcludingMe());
-               }, 1);
-       }
-}
index 9db2df6..36a4886 100644 (file)
@@ -170,7 +170,7 @@ public class GUITest extends JPanel
                g.setFont(g.getFont().deriveFont(Math.min(height, width) / 40f));
        }
 
-       private void drawString(Graphics g, String s, int x, int y, double anchorX, double anchorY)
+       private static void drawString(Graphics g, String s, int x, int y, double anchorX, double anchorY)
        {
                int h = g.getFontMetrics().getAscent();
                int w = g.getFontMetrics().stringWidth(s);
index 63d7395..8107db9 100644 (file)
@@ -90,8 +90,7 @@ public class Timeline
        {
                if (!hasNext())
                        return -1;
-               else
-                       return events.peek().timing;
+               return events.peek().timing;
        }
 
        public void reset()
@@ -127,7 +126,7 @@ public class Timeline
        private class InnerEvent
        {
 
-               private final long timing;
+               final long timing;
                private final TimelineEventHandler function;
                private final TimelineEvent event;
 
index b3eec8c..46decf5 100644 (file)
@@ -22,6 +22,7 @@ public class TimelineEvent
                return timing;
        }
 
+       @Override
        public String toString()
        {
                return "timestamp: " + timing;
index a0bd7f8..58f27bc 100644 (file)
@@ -22,7 +22,7 @@ public class WireArray
        public final int travelTime;
        private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
        public final int length;
-       private List<WireArrayEnd> inputs = new ArrayList<WireArrayEnd>();
+       List<WireArrayEnd> inputs = new ArrayList<WireArrayEnd>();
 
        public WireArray(int length, int travelTime)
        {
@@ -73,7 +73,7 @@ public class WireArray
                }
        }
 
-       private void recalculate()
+       void recalculate()
        {
                switch (inputs.size())
                {
@@ -223,7 +223,7 @@ public class WireArray
                return new WireArrayEnd(this);
        }
 
-       private void registerInput(WireArrayEnd toRegister)
+       void registerInput(WireArrayEnd toRegister)
        {
                inputs.add(toRegister);
        }
@@ -239,9 +239,9 @@ public class WireArray
        {
                public final WireArray owner;
                private boolean open;
-               private Bit[] inputValues;
+               Bit[] inputValues;
 
-               private WireArrayEnd(WireArray owner)
+               WireArrayEnd(WireArray owner)
                {
                        super();
                        this.owner = owner;
@@ -264,12 +264,11 @@ public class WireArray
                 */
                public void feedSignals(Bit... newValues)
                {
-                       if (newValues.length == length)
-                       {
-                               feedSignals(0, newValues);
-                       } else
+                       if (newValues.length != length)
                                throw new IllegalArgumentException(
                                                String.format("Attempted to input %d bits instead of %d bits.", newValues.length, length));
+                       feedSignals(0, newValues);
+
                }
 
                /**