Cleanup; Cleared warnings in the logic core
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / wires / Wire.java
index d73ee61..35addff 100644 (file)
@@ -6,6 +6,8 @@ import static net.mograsim.logic.core.types.Bit.Z;
 import java.util.ArrayList;
 import java.util.List;
 
+import net.mograsim.logic.core.LogicObservable;
+import net.mograsim.logic.core.LogicObserver;
 import net.mograsim.logic.core.timeline.Timeline;
 import net.mograsim.logic.core.types.Bit;
 import net.mograsim.logic.core.types.BitVector;
@@ -21,10 +23,10 @@ public class Wire
 {
        private BitVector values;
        public final int travelTime;
-       private List<ReadEnd> attached = new ArrayList<ReadEnd>();
+       private List<ReadEnd> attached = new ArrayList<>();
        public final int length;
-       private List<ReadWriteEnd> inputs = new ArrayList<ReadWriteEnd>();
-       private Timeline timeline;
+       List<ReadWriteEnd> inputs = new ArrayList<>();
+       Timeline timeline;
 
        public Wire(Timeline timeline, int length, int travelTime)
        {
@@ -59,12 +61,12 @@ public class Wire
        {
                if (values.equals(newValues))
                        return;
-               BitVector oldValues = values;
+//             BitVector oldValues = values;
                values = newValues;
-               notifyObservers(oldValues);
+               notifyObservers();
        }
 
-       private void recalculate()
+       void recalculate()
        {
                switch (inputs.size())
                {
@@ -165,27 +167,27 @@ public class Wire
        }
 
        /**
-        * Adds an {@link WireObserver}, who will be notified when the value of the {@link Wire} is updated.
+        * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated.
         * 
-        * @param ob The {@link WireObserver} to be notified of changes.
-        * @return true if the given {@link WireObserver} was not already registered, false otherwise
+        * @param ob The {@link LogicObserver} to be notified of changes.
+        * @return true if the given {@link LogicObserver} was not already registered, false otherwise
         * 
         * @author Fabian Stemmler
         */
-       private void attachEnd(ReadEnd end)
+       void attachEnd(ReadEnd end)
        {
                attached.add(end);
        }
 
-       private void detachEnd(ReadEnd end)
+       void detachEnd(ReadEnd end)
        {
                attached.remove(end);
        }
 
-       private void notifyObservers(BitVector oldValues)
+       private void notifyObservers()
        {
                for (ReadEnd o : attached)
-                       o.update(oldValues);
+                       o.update();
        }
 
        /**
@@ -204,7 +206,7 @@ public class Wire
                return new ReadEnd();
        }
 
-       private void registerInput(ReadWriteEnd toRegister)
+       void registerInput(ReadWriteEnd toRegister)
        {
                inputs.add(toRegister);
        }
@@ -216,20 +218,19 @@ public class Wire
         * 
         * @author Fabian Stemmler
         */
-       public class ReadEnd
+       public class ReadEnd implements LogicObservable
        {
-               private List<WireObserver> observers = new ArrayList<WireObserver>();
+               private List<LogicObserver> observers = new ArrayList<>();
 
-               private ReadEnd()
+               ReadEnd()
                {
                        super();
                        Wire.this.attachEnd(this);
                }
 
-               public void update(BitVector oldValues)
+               public void update()
                {
-                       for (WireObserver ob : observers)
-                               ob.update(this, oldValues);
+                       notifyObservers();
                }
 
                /**
@@ -333,14 +334,28 @@ public class Wire
                        return length;
                }
 
-               public boolean addObserver(WireObserver ob)
+               public Wire getWire()
                {
-                       return observers.add(ob);
+                       return Wire.this;
                }
 
-               public Wire getWire()
+               @Override
+               public void registerObserver(LogicObserver ob)
                {
-                       return Wire.this;
+                       observers.add(ob);
+               }
+
+               @Override
+               public void deregisterObserver(LogicObserver ob)
+               {
+                       observers.remove(ob);
+               }
+
+               @Override
+               public void notifyObservers()
+               {
+                       for (LogicObserver ob : observers)
+                               ob.update(this);
                }
        }
 
@@ -349,7 +364,7 @@ public class Wire
                private boolean open;
                private BitVector inputValues;
 
-               private ReadWriteEnd()
+               ReadWriteEnd()
                {
                        super();
                        open = true;
@@ -479,7 +494,6 @@ public class Wire
        public String toString()
        {
                return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), values, inputs);
-               // Arrays.toString(values), inputs.stream().map(i -> Arrays.toString(i.inputValues)).reduce((s1, s2) -> s1 + s2)
        }
 
        public static ReadEnd[] extractEnds(Wire[] w)