diff --git a/src/org/atriasoft/esignal/ISignal.java b/src/org/atriasoft/esignal/ISignal.java index 2286ca7..7265813 100644 --- a/src/org/atriasoft/esignal/ISignal.java +++ b/src/org/atriasoft/esignal/ISignal.java @@ -19,7 +19,7 @@ import edu.umd.cs.findbugs.annotations.CheckReturnValue; * signalEvent.setCallBackNotification(this::onConnectionChange); * } * public void onConnectionChange(final int currentNumberConnection, final int deltaConnection) { - * Log.info("Number of connection Change : " + currentNumberConnection + " delta=" + deltaConnection); + * Log.info("Number of connection Change : {} dalta={}", currentNumberConnection, deltaConnection); * } * } * } @@ -53,7 +53,7 @@ public class ISignal extends GenericSignalInstrumented, BiConsume * @param value Value to set in parameter. */ public void emit(final T value) { - List, BiConsumer>> tmp = getACleanedList(); + final List, BiConsumer>> tmp = getACleanedList(); if (tmp == null) { return; } @@ -62,17 +62,17 @@ public class ISignal extends GenericSignalInstrumented, BiConsume while (iterator.hasNext()) { final ConnectedElementInterface, BiConsumer> elem = iterator.next(); - Object remoteLockObject = elem.lockObjects(); + final Object remoteLockObject = elem.lockObjects(); if (elem.isObjectDependent() && remoteLockObject == null) { continue; } - Consumer tmpConsumer = elem.getConsumer(); + final Consumer tmpConsumer = elem.getConsumer(); if (tmpConsumer != null) { tmpConsumer.accept(value); continue; } // Not a dead code, but very hard to simply test it. - BiConsumer tmpConsumer2 = elem.getConsumer2(); + final BiConsumer tmpConsumer2 = elem.getConsumer2(); if (tmpConsumer2 != null) { tmpConsumer2.accept(elem.getObject(), value); } diff --git a/src/org/atriasoft/esignal/ISignalEmpty.java b/src/org/atriasoft/esignal/ISignalEmpty.java index e5e4d56..c4d2699 100644 --- a/src/org/atriasoft/esignal/ISignalEmpty.java +++ b/src/org/atriasoft/esignal/ISignalEmpty.java @@ -18,18 +18,38 @@ import edu.umd.cs.findbugs.annotations.CheckReturnValue; * signalEvent.setCallBackNotification(this::onConnectionChange); * } * public void onConnectionChange(final int currentNumberConnection, final int deltaConnection) { - * Log.info("Number of connection Change : " + currentNumberConnection + " delta=" + deltaConnection); + * Log.info("Number of connection Change : {} delta={}", currentNumberConnection, deltaConnection); * } * } * } * */ public class ISignalEmpty extends GenericSignalInstrumented> { + @CheckReturnValue + @SuppressWarnings("unchecked") + public Connection connect(final V object, final Consumer function) { + return connect(object, (final Object obj) -> { + function.accept((V) obj); + }); + } + + /** + * Connect to the signal and automatically disconnect when the object is removed + * @param object Object to check if remove to continue keeping the signal active (Keep a WeakReference on it only) + * @param function Function to connect (Keep a WeakReference on it only) + */ + @SuppressWarnings("unchecked") + public void connectAuto(final V object, final Consumer function) { + connectAuto(object, (final Object obj) -> { + function.accept((V) obj); + }); + } + /** * Emit a signal on all element connect (and clean the list of unlinked elements). */ public void emit() { - List>> tmp = getACleanedList(); + final List>> tmp = getACleanedList(); if (tmp == null) { return; } @@ -38,43 +58,21 @@ public class ISignalEmpty extends GenericSignalInstrumented> elem = iterator.next(); - Object remoteLockObject = elem.lockObjects(); + final Object remoteLockObject = elem.lockObjects(); if (elem.isObjectDependent() && remoteLockObject == null) { continue; } - Runnable tmpConsumer = elem.getConsumer(); + final Runnable tmpConsumer = elem.getConsumer(); if (tmpConsumer != null) { tmpConsumer.run(); continue; } // Not a dead code, but very hard to simply test it. - Consumer tmpConsumer2 = elem.getConsumer2(); + final Consumer tmpConsumer2 = elem.getConsumer2(); if (tmpConsumer2 != null) { tmpConsumer2.accept(elem.getObject()); } } } - - /** - * Connect to the signal and automatically disconnect when the object is removed - * @param object Object to check if remove to continue keeping the signal active (Keep a WeakReference on it only) - * @param function Function to connect (Keep a WeakReference on it only) - */ - @SuppressWarnings("unchecked") - public void connectAuto(final V object, final Consumer function) { - connectAuto(object, - (final Object obj) -> { - function.accept((V)obj); - }); - } - - @CheckReturnValue - @SuppressWarnings("unchecked") - public Connection connect(final V object, final Consumer function) { - return connect(object, - (final Object obj) -> { - function.accept((V)obj); - }); - } } diff --git a/src/org/atriasoft/esignal/Signal.java b/src/org/atriasoft/esignal/Signal.java index fef1cfd..4b1ee1b 100644 --- a/src/org/atriasoft/esignal/Signal.java +++ b/src/org/atriasoft/esignal/Signal.java @@ -15,24 +15,24 @@ import edu.umd.cs.findbugs.annotations.CheckReturnValue; * class EmiterSimple { * public Signal signalEvent = new Signal<>(); * } - * + * * class ReceiverSimple { * public void onEvent(String data) { - * Log.error("function receive: " + data); + * Log.error("function receive: {}", data); * } * public connectLambda(EmiterSimple other) { * // Note : this lambda is reference a a global, then it will never removed in the connection list ==> refer the local class or @see connectAutoRemoveObject * other.signalEvent.connect((data) -> { - * Log.error("lambda receive: " + data); + * Log.error("lambda receive: {}", data); * }); * } * } - * // use : + * // use : * EmiterSimple aaa = new EmiterSimple(); * ReceiverSimple bbb = new ReceiverSimple(); * // Emit a signal: * aaa.signalEvent.emit("My message ..."); - * // simple direct connection: + * // simple direct connection: * aaa.signalEvent.connect(bbb::onEvent); * //removable connection (2 possibilities:) * // First solution (best way ==> does not need to lock a reference on the current object and the remote) @@ -54,12 +54,32 @@ import edu.umd.cs.findbugs.annotations.CheckReturnValue; * */ public class Signal extends GenericSignal, BiConsumer> { + @CheckReturnValue + @SuppressWarnings("unchecked") + public Connection connect(final V object, final BiConsumer function) { + return connect(object, (final Object obj, final T value) -> { + function.accept((V) obj, value); + }); + } + + /** + * Connect to the signal and automatically disconnect when the object is removed + * @param object Object to check if remove to continue keeping the signal active (Keep a WeakReference on it only) + * @param function Function to connect (Keep a WeakReference on it only) + */ + @SuppressWarnings("unchecked") + public void connectAuto(final V object, final BiConsumer function) { + connectAuto(object, (final Object obj, final T value) -> { + function.accept((V) obj, value); + }); + } + /** * Emit a signal on all element connect (and clean the list of unlinked elements). * @param value Value to set in parameter. */ public void emit(final T value) { - List, BiConsumer>> tmp = getACleanedList(); + final List, BiConsumer>> tmp = getACleanedList(); if (tmp == null) { return; } @@ -68,41 +88,20 @@ public class Signal extends GenericSignal, BiConsumer> while (iterator.hasNext()) { final ConnectedElementInterface, BiConsumer> elem = iterator.next(); - Object remoteLockObject = elem.lockObjects(); + final Object remoteLockObject = elem.lockObjects(); if (elem.isObjectDependent() && remoteLockObject == null) { continue; } - Consumer tmpConsumer = elem.getConsumer(); + final Consumer tmpConsumer = elem.getConsumer(); if (tmpConsumer != null) { tmpConsumer.accept(value); continue; } // Not a dead code, but very hard to simply test it. - BiConsumer tmpConsumer2 = elem.getConsumer2(); + final BiConsumer tmpConsumer2 = elem.getConsumer2(); if (tmpConsumer2 != null) { tmpConsumer2.accept(elem.getObject(), value); } } } - - /** - * Connect to the signal and automatically disconnect when the object is removed - * @param object Object to check if remove to continue keeping the signal active (Keep a WeakReference on it only) - * @param function Function to connect (Keep a WeakReference on it only) - */ - @SuppressWarnings("unchecked") - public void connectAuto(final V object, final BiConsumer function) { - connectAuto(object, - (final Object obj, final T value) -> { - function.accept((V)obj, value); - }); - } - @CheckReturnValue - @SuppressWarnings("unchecked") - public Connection connect(final V object, final BiConsumer function) { - return connect(object, - (final Object obj, final T value) -> { - function.accept((V)obj, value); - }); - } }