diff --git a/src/org/atriasoft/esignal/ConnectionRemoveInterface.java b/src/org/atriasoft/esignal/ConnectionRemoveInterface.java index fe80970..499767f 100644 --- a/src/org/atriasoft/esignal/ConnectionRemoveInterface.java +++ b/src/org/atriasoft/esignal/ConnectionRemoveInterface.java @@ -1,5 +1,12 @@ package org.atriasoft.esignal; +/** + * Interface to permit to the connection to unlink itself. + */ public interface ConnectionRemoveInterface { + /** + * Request the removing on the specify connection + * @param connection Connection to removed. + */ void disconnect(final Connection connection); } diff --git a/src/org/atriasoft/esignal/GenericSignal.java b/src/org/atriasoft/esignal/GenericSignal.java index e14bb2a..e085711 100644 --- a/src/org/atriasoft/esignal/GenericSignal.java +++ b/src/org/atriasoft/esignal/GenericSignal.java @@ -13,8 +13,11 @@ import org.atriasoft.esignal.internal.ConnectedElementDynamic; * @param generic Runnable, Consumer, or BiConsumer template... */ public class GenericSignal implements ConnectionRemoveInterface { - + // List of all connected Links protected List> data = new ArrayList<>(); + /** + * Clear all connection on this signal. + */ public void clear() { List> data2 = this.data; synchronized(this.data) { @@ -26,12 +29,21 @@ public class GenericSignal implements ConnectionRemoveInterface { elem.disconnect(); } } - + /** + * Connect a Function to this signal + * @param function Function to connect (Keep a WeakReference on it only) + * @apiNote Make some attention when you connect a global lamba, nothing can pertmit to remove it @see connectAutoRemoveObject() + */ public void connect(final T function) { synchronized(this.data) { this.data.add(new ConnectedElement(function)); } } + /** + * Disconnect all connection that have this Object/function in reference + * @param obj Object to check the compatibility. + * @apiNote if you add a direct connection like {code connect(this::onEvent(...)) } you can not disconnect it + */ public void disconnect(final T obj) { synchronized(this.data) { final Iterator> iterator = this.data.iterator(); @@ -43,6 +55,11 @@ public class GenericSignal implements ConnectionRemoveInterface { } } } + /** + * Connect to the signal with a @see Connection object that permit to remove the connection to the signal. + * @param function Function to connect (Keep a WeakReference on it only) + * @return The connection interface. + */ public Connection connectDynamic(final T function) { Connection out = new Connection(this); synchronized(this.data) { @@ -50,6 +67,11 @@ public class GenericSignal implements ConnectionRemoveInterface { } return out; } + /** + * 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) + */ public void connectAutoRemoveObject(final Object object, final T function) { synchronized(this.data) { this.data.add(new ConnectedElementDynamic(object, function)); @@ -70,6 +92,10 @@ public class GenericSignal implements ConnectionRemoveInterface { } } + /** + * Get a copy on the List of current connection and remove all the deprecated connection. + * @return The copy of the available connection (Note: the connection can be removed when return) + */ protected List> getACleanedList() { // first clean the list cleanedList(); @@ -86,6 +112,9 @@ public class GenericSignal implements ConnectionRemoveInterface { } return out; } + /** + * Clean all the deprecated list of removed elements + */ protected void cleanedList() { // clean the list: synchronized(this.data) { @@ -100,9 +129,17 @@ public class GenericSignal implements ConnectionRemoveInterface { } } } + /** + * Get the Number of current connection (clean is not done). + * @return Number of connection + */ public int size() { return this.data.size(); } + /** + * Get the Number of current connection (clean is done). + * @return Number of connection + */ public int sizeCleaned() { cleanedList(); return this.data.size(); diff --git a/src/org/atriasoft/esignal/Signal.java b/src/org/atriasoft/esignal/Signal.java index e981e2c..839c403 100644 --- a/src/org/atriasoft/esignal/Signal.java +++ b/src/org/atriasoft/esignal/Signal.java @@ -1,6 +1,5 @@ package org.atriasoft.esignal; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.function.Consumer; @@ -52,6 +51,10 @@ import org.atriasoft.esignal.internal.ConnectedElement; * */ public class Signal extends GenericSignal> { + /** + * 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>> tmp = getACleanedList(); if (tmp == null) { diff --git a/src/org/atriasoft/esignal/Signal2.java b/src/org/atriasoft/esignal/Signal2.java index 3d3eeb4..82a6826 100644 --- a/src/org/atriasoft/esignal/Signal2.java +++ b/src/org/atriasoft/esignal/Signal2.java @@ -1,6 +1,5 @@ package org.atriasoft.esignal; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.function.BiConsumer; @@ -54,6 +53,11 @@ import org.atriasoft.esignal.internal.ConnectedElement; * */ public class Signal2 extends GenericSignal> { + /** + * Emit a signal on all element connect (and clean the list of unlinked elements). + * @param valueT First parameter value to emit. + * @param valueU Second parameter value to emit. + */ public void emit(final T valueT, final U valueU) { List>> tmp = getACleanedList(); if (tmp == null) { diff --git a/src/org/atriasoft/esignal/SignalEmpty.java b/src/org/atriasoft/esignal/SignalEmpty.java index e1e7e57..e19bbed 100644 --- a/src/org/atriasoft/esignal/SignalEmpty.java +++ b/src/org/atriasoft/esignal/SignalEmpty.java @@ -1,6 +1,5 @@ package org.atriasoft.esignal; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -49,7 +48,9 @@ import org.atriasoft.esignal.internal.ConnectedElement; * */ public class SignalEmpty extends GenericSignal { - + /** + * Emit a signal on all element connect (and clean the list of unlinked elements). + */ public void emit() { List> tmp = getACleanedList(); if (tmp == null) {