From: Christian Femers Date: Sun, 19 May 2019 20:39:19 +0000 (+0200) Subject: Did some clean up X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=c18c04011cab0040c2287608eeefc9c3cc4536c2;hp=18cf2f85ed378005aa93c8b88fe5fa055a108fad;p=Mograsim.git Did some clean up 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 --- diff --git a/era.mi/src/era/mi/logic/Bit.java b/era.mi/src/era/mi/logic/Bit.java index da9a227f..992c7295 100644 --- a/era.mi/src/era/mi/logic/Bit.java +++ b/era.mi/src/era/mi/logic/Bit.java @@ -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 }, diff --git a/era.mi/src/era/mi/logic/Simulation.java b/era.mi/src/era/mi/logic/Simulation.java index f4b25d2d..0468ec1e 100644 --- a/era.mi/src/era/mi/logic/Simulation.java +++ b/era.mi/src/era/mi/logic/Simulation.java @@ -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 diff --git a/era.mi/src/era/mi/logic/components/BasicComponent.java b/era.mi/src/era/mi/logic/components/BasicComponent.java index 509c662b..2956fe01 100644 --- a/era.mi/src/era/mi/logic/components/BasicComponent.java +++ b/era.mi/src/era/mi/logic/components/BasicComponent.java @@ -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(); diff --git a/era.mi/src/era/mi/logic/components/BitDisplay.java b/era.mi/src/era/mi/logic/components/BitDisplay.java index 051010b7..bd9714ae 100644 --- a/era.mi/src/era/mi/logic/components/BitDisplay.java +++ b/era.mi/src/era/mi/logic/components/BitDisplay.java @@ -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 getAllInputs() { - return Collections.unmodifiableList(Arrays.asList(in)); + return List.of(in); } @Override public List getAllOutputs() { - return Collections.unmodifiableList(new ArrayList()); + return List.of(); } } diff --git a/era.mi/src/era/mi/logic/components/Clock.java b/era.mi/src/era/mi/logic/components/Clock.java index 56f11100..d95cbc7b 100644 --- a/era.mi/src/era/mi/logic/components/Clock.java +++ b/era.mi/src/era/mi/logic/components/Clock.java @@ -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 getAllInputs() { - return Collections.unmodifiableList(Arrays.asList()); + return List.of(); } @Override public List getAllOutputs() { - return Collections.unmodifiableList(Arrays.asList(outI.owner)); + return List.of(outI.owner); } } diff --git a/era.mi/src/era/mi/logic/components/Demux.java b/era.mi/src/era/mi/logic/components/Demux.java index b7ed0ff8..762d8067 100644 --- a/era.mi/src/era/mi/logic/components/Demux.java +++ b/era.mi/src/era/mi/logic/components/Demux.java @@ -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 getAllInputs() { - return Collections.unmodifiableList(Arrays.asList(in, select)); + return List.of(in, select); } @Override public List getAllOutputs() { - return Collections.unmodifiableList(Arrays.asList(outputs)); + return List.of(outputs); } } diff --git a/era.mi/src/era/mi/logic/components/Merger.java b/era.mi/src/era/mi/logic/components/Merger.java index 17d1b57d..da9b55e3 100644 --- a/era.mi/src/era/mi/logic/components/Merger.java +++ b/era.mi/src/era/mi/logic/components/Merger.java @@ -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 getAllInputs() { - return Collections.unmodifiableList(Arrays.asList(inputs)); + return List.of(inputs); } @Override public List getAllOutputs() { - return Collections.unmodifiableList(Arrays.asList(outI.owner)); + return List.of(outI.owner); } } diff --git a/era.mi/src/era/mi/logic/components/Mux.java b/era.mi/src/era/mi/logic/components/Mux.java index d6307f9a..17680969 100644 --- a/era.mi/src/era/mi/logic/components/Mux.java +++ b/era.mi/src/era/mi/logic/components/Mux.java @@ -88,6 +88,6 @@ public class Mux extends BasicComponent @Override public List getAllOutputs() { - return Collections.unmodifiableList(Arrays.asList(outI.owner)); + return List.of(outI.owner); } } diff --git a/era.mi/src/era/mi/logic/components/TriStateBuffer.java b/era.mi/src/era/mi/logic/components/TriStateBuffer.java index 5c02a4e3..0a7133f2 100644 --- a/era.mi/src/era/mi/logic/components/TriStateBuffer.java +++ b/era.mi/src/era/mi/logic/components/TriStateBuffer.java @@ -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 getAllInputs() { - return Collections.unmodifiableList(Arrays.asList(in, enable)); + return List.of(in, enable); } @Override public List getAllOutputs() { - return Collections.unmodifiableList(Arrays.asList(outI.owner)); + return List.of(outI.owner); } } diff --git a/era.mi/src/era/mi/logic/components/gates/MultiInputGate.java b/era.mi/src/era/mi/logic/components/gates/MultiInputGate.java index d26297be..fac267a2 100644 --- a/era.mi/src/era/mi/logic/components/gates/MultiInputGate.java +++ b/era.mi/src/era/mi/logic/components/gates/MultiInputGate.java @@ -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 getAllInputs() { - return Collections.unmodifiableList(Arrays.asList(in)); + return List.of(in); } @Override public List getAllOutputs() { - return Collections.unmodifiableList(Arrays.asList(out)); + return List.of(out); } + @Override protected void compute() { Bit[] result = in[0].getValues(); diff --git a/era.mi/src/era/mi/logic/components/gates/NotGate.java b/era.mi/src/era/mi/logic/components/gates/NotGate.java index c4821159..f6644714 100644 --- a/era.mi/src/era/mi/logic/components/gates/NotGate.java +++ b/era.mi/src/era/mi/logic/components/gates/NotGate.java @@ -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 getAllInputs() { - return Collections.unmodifiableList(Arrays.asList(in)); + return List.of(in); } @Override public List 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 index 9a8d3fc0..00000000 --- a/era.mi/src/era/mi/logic/tests/Connector.java +++ /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); - } -} diff --git a/era.mi/src/era/mi/logic/tests/GUITest.java b/era.mi/src/era/mi/logic/tests/GUITest.java index 9db2df66..36a4886a 100644 --- a/era.mi/src/era/mi/logic/tests/GUITest.java +++ b/era.mi/src/era/mi/logic/tests/GUITest.java @@ -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); diff --git a/era.mi/src/era/mi/logic/timeline/Timeline.java b/era.mi/src/era/mi/logic/timeline/Timeline.java index 63d73950..8107db96 100644 --- a/era.mi/src/era/mi/logic/timeline/Timeline.java +++ b/era.mi/src/era/mi/logic/timeline/Timeline.java @@ -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; diff --git a/era.mi/src/era/mi/logic/timeline/TimelineEvent.java b/era.mi/src/era/mi/logic/timeline/TimelineEvent.java index b3eec8ca..46decf5f 100644 --- a/era.mi/src/era/mi/logic/timeline/TimelineEvent.java +++ b/era.mi/src/era/mi/logic/timeline/TimelineEvent.java @@ -22,6 +22,7 @@ public class TimelineEvent return timing; } + @Override public String toString() { return "timestamp: " + timing; diff --git a/era.mi/src/era/mi/logic/wires/WireArray.java b/era.mi/src/era/mi/logic/wires/WireArray.java index a0bd7f8b..58f27bc2 100644 --- a/era.mi/src/era/mi/logic/wires/WireArray.java +++ b/era.mi/src/era/mi/logic/wires/WireArray.java @@ -22,7 +22,7 @@ public class WireArray public final int travelTime; private List observers = new ArrayList(); public final int length; - private List inputs = new ArrayList(); + List inputs = new ArrayList(); 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); + } /**