From 0708bf47dc67d75a37f8eb859251f5b9ac347341 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 4 Jun 2021 15:12:18 +0200 Subject: [PATCH] [DEV] reduce code with generic class --- src/org/atriasoft/esignal/Signal.java | 98 ++++------------- src/org/atriasoft/esignal/Signal2.java | 97 ++++------------- src/org/atriasoft/esignal/SignalEmpty.java | 102 ++++-------------- .../esignal/internal/ConnectedElement.java | 32 ++++++ .../internal/ConnectedElementDynamic.java | 50 +++++++++ 5 files changed, 138 insertions(+), 241 deletions(-) create mode 100644 src/org/atriasoft/esignal/internal/ConnectedElement.java create mode 100644 src/org/atriasoft/esignal/internal/ConnectedElementDynamic.java diff --git a/src/org/atriasoft/esignal/Signal.java b/src/org/atriasoft/esignal/Signal.java index 64e9de6..614e947 100644 --- a/src/org/atriasoft/esignal/Signal.java +++ b/src/org/atriasoft/esignal/Signal.java @@ -1,74 +1,12 @@ package org.atriasoft.esignal; -import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.function.Consumer; -class ConnectedElement { - protected final WeakReference> consumer; - - public ConnectedElement(final Consumer consumer) { - this.consumer = new WeakReference>(consumer); - } - - public Consumer getConsumer() { - return this.consumer.get(); - } - - public boolean isCompatibleWith(final Object elem) { - Object out = this.consumer.get(); - if (out == elem) { - return true; - } - return false; - } - - public void disconnect() { - - } -} - -class ConnectedElementDynamic extends ConnectedElement { - protected final WeakReference linkedObject; - - public ConnectedElementDynamic(final Object linkedObject, final Consumer consumer) { - super(consumer); - this.linkedObject = new WeakReference(linkedObject); - } - - @Override - public Consumer getConsumer() { - if (this.linkedObject.get() == null) { - return null; - } - return this.consumer.get(); - } - - @Override - public boolean isCompatibleWith(final Object elem) { - if (super.isCompatibleWith(elem)) { - return true; - } - Object obj = this.linkedObject.get(); - if (obj == elem) { - return true; - } - return false; - } - - @Override - public void disconnect() { - Object obj = this.linkedObject.get(); - if (obj == null) { - return; - } - if (obj instanceof Connection tmp) { - tmp.connectionIsRemovedBySignal(); - } - } -} +import org.atriasoft.esignal.internal.ConnectedElement; +import org.atriasoft.esignal.internal.ConnectedElementDynamic; /** * Simple interface to manage signal connection and disconnection @@ -115,30 +53,30 @@ class ConnectedElementDynamic extends ConnectedElement { * */ public class Signal implements ConnectionRemoveInterface { - List> data = new ArrayList<>(); + List>> data = new ArrayList<>(); public void clear() { - List> data2 = this.data; + List>> data2 = this.data; synchronized(this.data) { this.data = new ArrayList<>(); } - final Iterator> iterator = data2.iterator(); + final Iterator>> iterator = data2.iterator(); while (iterator.hasNext()) { - final ConnectedElement elem = iterator.next(); + final ConnectedElement> elem = iterator.next(); elem.disconnect(); } } public void connect(final Consumer function) { synchronized(this.data) { - this.data.add(new ConnectedElement(function)); + this.data.add(new ConnectedElement>(function)); } } public void disconnect(final Consumer obj) { synchronized(this.data) { - final Iterator> iterator = this.data.iterator(); + final Iterator>> iterator = this.data.iterator(); while (iterator.hasNext()) { - final ConnectedElement elem = iterator.next(); + final ConnectedElement> elem = iterator.next(); if (elem.isCompatibleWith(obj)) { iterator.remove(); } @@ -148,22 +86,22 @@ public class Signal implements ConnectionRemoveInterface { public Connection connectDynamic(final Consumer function) { Connection out = new Connection(this); synchronized(this.data) { - this.data.add(new ConnectedElementDynamic(out, function)); + this.data.add(new ConnectedElementDynamic>(out, function)); } return out; } public void connectAutoRemoveObject(final Object reference, final Consumer function) { synchronized(this.data) { - this.data.add(new ConnectedElementDynamic(reference, function)); + this.data.add(new ConnectedElementDynamic>(reference, function)); } } @Override public void disconnect(final Connection connection) { synchronized(this.data) { - final Iterator> iterator = this.data.iterator(); + final Iterator>> iterator = this.data.iterator(); while (iterator.hasNext()) { - final ConnectedElement elem = iterator.next(); + final ConnectedElement> elem = iterator.next(); if (elem.isCompatibleWith(connection)) { elem.disconnect(); iterator.remove(); @@ -173,12 +111,12 @@ public class Signal implements ConnectionRemoveInterface { } public void emit(final T value) { - List> tmp; + List>> tmp; // clean the list: synchronized(this.data) { - final Iterator> iterator = this.data.iterator(); + final Iterator>> iterator = this.data.iterator(); while (iterator.hasNext()) { - final ConnectedElement elem = iterator.next(); + final ConnectedElement> elem = iterator.next(); Object tmpObject = elem.getConsumer(); if (tmpObject == null) { elem.disconnect(); @@ -194,9 +132,9 @@ public class Signal implements ConnectionRemoveInterface { } // real call elements { - final Iterator> iterator = tmp.iterator(); + final Iterator>> iterator = tmp.iterator(); while (iterator.hasNext()) { - final ConnectedElement elem = iterator.next(); + final ConnectedElement> elem = iterator.next(); Consumer tmpObject = elem.getConsumer(); if (tmpObject == null) { continue; diff --git a/src/org/atriasoft/esignal/Signal2.java b/src/org/atriasoft/esignal/Signal2.java index 38de5a1..f6ea8c8 100644 --- a/src/org/atriasoft/esignal/Signal2.java +++ b/src/org/atriasoft/esignal/Signal2.java @@ -1,74 +1,13 @@ package org.atriasoft.esignal; -import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.function.BiConsumer; -class BiConnectedElement { - protected final WeakReference> consumer; - - public BiConnectedElement(final BiConsumer consumer) { - this.consumer = new WeakReference>(consumer); - } - - public BiConsumer getConsumer() { - return this.consumer.get(); - } +import org.atriasoft.esignal.internal.ConnectedElement; +import org.atriasoft.esignal.internal.ConnectedElementDynamic; - public boolean isCompatibleWith(final Object elem) { - Object out = this.consumer.get(); - if (out == elem) { - return true; - } - return false; - } - - public void disconnect() { - - } -} - -class BiConnectedElementDynamic extends BiConnectedElement { - protected final WeakReference linkedObject; - - public BiConnectedElementDynamic(final Object linkedObject, final BiConsumer consumer) { - super(consumer); - this.linkedObject = new WeakReference(linkedObject); - } - - @Override - public BiConsumer getConsumer() { - if (this.linkedObject.get() == null) { - return null; - } - return this.consumer.get(); - } - - @Override - public boolean isCompatibleWith(final Object elem) { - if (super.isCompatibleWith(elem)) { - return true; - } - Object obj = this.linkedObject.get(); - if (obj == elem) { - return true; - } - return false; - } - - @Override - public void disconnect() { - Object obj = this.linkedObject.get(); - if (obj == null) { - return; - } - if (obj instanceof Connection tmp) { - tmp.connectionIsRemovedBySignal(); - } - } -} /** * Simple interface to manage signal connection and disconnection @@ -116,30 +55,30 @@ class BiConnectedElementDynamic extends BiConnectedElement { * */ public class Signal2 implements ConnectionRemoveInterface { - List> data = new ArrayList<>(); + List>> data = new ArrayList<>(); public void clear() { - List> data2 = this.data; + List>> data2 = this.data; synchronized(this.data) { this.data = new ArrayList<>(); } - final Iterator> iterator = data2.iterator(); + final Iterator>> iterator = data2.iterator(); while (iterator.hasNext()) { - final BiConnectedElement elem = iterator.next(); + final ConnectedElement> elem = iterator.next(); elem.disconnect(); } } public void connect(final BiConsumer function) { synchronized(this.data) { - this.data.add(new BiConnectedElement(function)); + this.data.add(new ConnectedElement>(function)); } } public void disconnect(final BiConsumer obj) { synchronized(this.data) { - final Iterator> iterator = this.data.iterator(); + final Iterator>> iterator = this.data.iterator(); while (iterator.hasNext()) { - final BiConnectedElement elem = iterator.next(); + final ConnectedElement> elem = iterator.next(); if (elem.isCompatibleWith(obj)) { iterator.remove(); } @@ -149,22 +88,22 @@ public class Signal2 implements ConnectionRemoveInterface { public Connection connectDynamic(final BiConsumer function) { Connection out = new Connection(this); synchronized(this.data) { - this.data.add(new BiConnectedElementDynamic(out, function)); + this.data.add(new ConnectedElementDynamic>(out, function)); } return out; } public void connectAutoRemoveObject(final Object reference, final BiConsumer function) { synchronized(this.data) { - this.data.add(new BiConnectedElementDynamic(reference, function)); + this.data.add(new ConnectedElementDynamic>(reference, function)); } } @Override public void disconnect(final Connection connection) { synchronized(this.data) { - final Iterator> iterator = this.data.iterator(); + final Iterator>> iterator = this.data.iterator(); while (iterator.hasNext()) { - final BiConnectedElement elem = iterator.next(); + final ConnectedElement> elem = iterator.next(); if (elem.isCompatibleWith(connection)) { elem.disconnect(); iterator.remove(); @@ -174,12 +113,12 @@ public class Signal2 implements ConnectionRemoveInterface { } public void emit(final T valueT, final U valueU) { - List> tmp; + List>> tmp; // clean the list: synchronized(this.data) { - final Iterator> iterator = this.data.iterator(); + final Iterator>> iterator = this.data.iterator(); while (iterator.hasNext()) { - final BiConnectedElement elem = iterator.next(); + final ConnectedElement> elem = iterator.next(); Object tmpObject = elem.getConsumer(); if (tmpObject == null) { elem.disconnect(); @@ -195,9 +134,9 @@ public class Signal2 implements ConnectionRemoveInterface { } // real call elements { - final Iterator> iterator = tmp.iterator(); + final Iterator>> iterator = tmp.iterator(); while (iterator.hasNext()) { - final BiConnectedElement elem = iterator.next(); + final ConnectedElement> elem = iterator.next(); BiConsumer tmpObject = elem.getConsumer(); if (tmpObject == null) { continue; diff --git a/src/org/atriasoft/esignal/SignalEmpty.java b/src/org/atriasoft/esignal/SignalEmpty.java index 914d155..526b3a5 100644 --- a/src/org/atriasoft/esignal/SignalEmpty.java +++ b/src/org/atriasoft/esignal/SignalEmpty.java @@ -1,73 +1,11 @@ package org.atriasoft.esignal; -import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Iterator; import java.util.List; -class ConnectedElementEmpty { - protected final WeakReference runner; - - public ConnectedElementEmpty(final Runnable runner) { - this.runner = new WeakReference(runner); - } - - public Runnable getRunner() { - return this.runner.get(); - } - - public boolean isCompatibleWith(final Object elem) { - Object out = this.runner.get(); - if (out == elem) { - return true; - } - return false; - } - - public void disconnect() { - - } -} - -class ConnectedElementDynamicEmpty extends ConnectedElementEmpty { - protected final WeakReference linkedObject; - - public ConnectedElementDynamicEmpty(final Object linkedObject, final Runnable runner) { - super(runner); - this.linkedObject = new WeakReference(linkedObject); - } - - @Override - public Runnable getRunner() { - if (this.linkedObject.get() == null) { - return null; - } - return this.runner.get(); - } - - @Override - public boolean isCompatibleWith(final Object elem) { - if (super.isCompatibleWith(elem)) { - return true; - } - Object obj = this.linkedObject.get(); - if (obj == elem) { - return true; - } - return false; - } - - @Override - public void disconnect() { - Object obj = this.linkedObject.get(); - if (obj == null) { - return; - } - if (obj instanceof Connection tmp) { - tmp.connectionIsRemovedBySignal(); - } - } -} +import org.atriasoft.esignal.internal.ConnectedElement; +import org.atriasoft.esignal.internal.ConnectedElementDynamic; /** * Simple interface to manage signal connection and disconnection @@ -112,30 +50,30 @@ class ConnectedElementDynamicEmpty extends ConnectedElementEmpty { * */ public class SignalEmpty implements ConnectionRemoveInterface { - List data = new ArrayList<>(); + List> data = new ArrayList<>(); public void clear() { - List data2 = this.data; + List> data2 = this.data; synchronized(this.data) { this.data = new ArrayList<>(); } - final Iterator iterator = data2.iterator(); + final Iterator> iterator = data2.iterator(); while (iterator.hasNext()) { - final ConnectedElementEmpty elem = iterator.next(); + final ConnectedElement elem = iterator.next(); elem.disconnect(); } } public void connect(final Runnable function) { synchronized(this.data) { - this.data.add(new ConnectedElementEmpty(function)); + this.data.add(new ConnectedElement(function)); } } public void disconnect(final Runnable obj) { synchronized(this.data) { - final Iterator iterator = this.data.iterator(); + final Iterator> iterator = this.data.iterator(); while (iterator.hasNext()) { - final ConnectedElementEmpty elem = iterator.next(); + final ConnectedElement elem = iterator.next(); if (elem.isCompatibleWith(obj)) { iterator.remove(); } @@ -145,22 +83,22 @@ public class SignalEmpty implements ConnectionRemoveInterface { public Connection connectDynamic(final Runnable function) { Connection out = new Connection(this); synchronized(this.data) { - this.data.add(new ConnectedElementDynamicEmpty(out, function)); + this.data.add(new ConnectedElementDynamic(out, function)); } return out; } public void connectAutoRemoveObject(final Object reference, final Runnable function) { synchronized(this.data) { - this.data.add(new ConnectedElementDynamicEmpty(reference, function)); + this.data.add(new ConnectedElementDynamic(reference, function)); } } @Override public void disconnect(final Connection connection) { synchronized(this.data) { - final Iterator iterator = this.data.iterator(); + final Iterator> iterator = this.data.iterator(); while (iterator.hasNext()) { - final ConnectedElementEmpty elem = iterator.next(); + final ConnectedElement elem = iterator.next(); if (elem.isCompatibleWith(connection)) { elem.disconnect(); iterator.remove(); @@ -170,13 +108,13 @@ public class SignalEmpty implements ConnectionRemoveInterface { } public void emit() { - List tmp; + List> tmp; // clean the list: synchronized(this.data) { - final Iterator iterator = this.data.iterator(); + final Iterator> iterator = this.data.iterator(); while (iterator.hasNext()) { - final ConnectedElementEmpty elem = iterator.next(); - Object tmpObject = elem.getRunner(); + final ConnectedElement elem = iterator.next(); + Object tmpObject = elem.getConsumer(); if (tmpObject == null) { elem.disconnect(); iterator.remove(); @@ -191,10 +129,10 @@ public class SignalEmpty implements ConnectionRemoveInterface { } // real call elements { - final Iterator iterator = tmp.iterator(); + final Iterator> iterator = tmp.iterator(); while (iterator.hasNext()) { - final ConnectedElementEmpty elem = iterator.next(); - Runnable tmpObject = elem.getRunner(); + final ConnectedElement elem = iterator.next(); + Runnable tmpObject = elem.getConsumer(); if (tmpObject == null) { continue; } diff --git a/src/org/atriasoft/esignal/internal/ConnectedElement.java b/src/org/atriasoft/esignal/internal/ConnectedElement.java new file mode 100644 index 0000000..a8d1f4f --- /dev/null +++ b/src/org/atriasoft/esignal/internal/ConnectedElement.java @@ -0,0 +1,32 @@ +package org.atriasoft.esignal.internal; + +import java.lang.ref.WeakReference; + +/** + * Connected element on a consumer to Manage event + * + * @param Type Of the Signal + */ +public class ConnectedElement { + protected final WeakReference consumer; + + public ConnectedElement(final T consumer) { + this.consumer = new WeakReference(consumer); + } + + public T getConsumer() { + return this.consumer.get(); + } + + public boolean isCompatibleWith(final Object elem) { + Object out = this.consumer.get(); + if (out == elem) { + return true; + } + return false; + } + + public void disconnect() { + + } +} \ No newline at end of file diff --git a/src/org/atriasoft/esignal/internal/ConnectedElementDynamic.java b/src/org/atriasoft/esignal/internal/ConnectedElementDynamic.java new file mode 100644 index 0000000..b7a3448 --- /dev/null +++ b/src/org/atriasoft/esignal/internal/ConnectedElementDynamic.java @@ -0,0 +1,50 @@ +package org.atriasoft.esignal.internal; + +import java.lang.ref.WeakReference; + +import org.atriasoft.esignal.Connection; + +/** + * Connected element with a dependency of an other object to auto remove the signals + * + * @param Type Of the Signal + */ +public class ConnectedElementDynamic extends ConnectedElement { + protected final WeakReference linkedObject; + + public ConnectedElementDynamic(final Object linkedObject, final T consumer) { + super(consumer); + this.linkedObject = new WeakReference(linkedObject); + } + + @Override + public T getConsumer() { + if (this.linkedObject.get() == null) { + return null; + } + return this.consumer.get(); + } + + @Override + public boolean isCompatibleWith(final Object elem) { + if (super.isCompatibleWith(elem)) { + return true; + } + Object obj = this.linkedObject.get(); + if (obj == elem) { + return true; + } + return false; + } + + @Override + public void disconnect() { + Object obj = this.linkedObject.get(); + if (obj == null) { + return; + } + if (obj instanceof Connection tmp) { + tmp.connectionIsRemovedBySignal(); + } + } +} \ No newline at end of file