[DEV] simplify the code
This commit is contained in:
parent
0708bf47dc
commit
722e6fe5cf
77
src/org/atriasoft/esignal/GenericSignal.java
Normal file
77
src/org/atriasoft/esignal/GenericSignal.java
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package org.atriasoft.esignal;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.atriasoft.esignal.internal.ConnectedElement;
|
||||||
|
import org.atriasoft.esignal.internal.ConnectedElementDynamic;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic interface for a signaling model...
|
||||||
|
*
|
||||||
|
* @param <T> generic Runnable, Consumer, or BiConsumer template...
|
||||||
|
*/
|
||||||
|
public class GenericSignal<T> implements ConnectionRemoveInterface {
|
||||||
|
List<ConnectedElement<T>> data = new ArrayList<>();
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
List<ConnectedElement<T>> data2 = this.data;
|
||||||
|
synchronized(this.data) {
|
||||||
|
this.data = new ArrayList<>();
|
||||||
|
}
|
||||||
|
final Iterator<ConnectedElement<T>> iterator = data2.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
final ConnectedElement<T> elem = iterator.next();
|
||||||
|
elem.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connect(final T function) {
|
||||||
|
synchronized(this.data) {
|
||||||
|
this.data.add(new ConnectedElement<T>(function));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void disconnect(final T obj) {
|
||||||
|
synchronized(this.data) {
|
||||||
|
final Iterator<ConnectedElement<T>> iterator = this.data.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
final ConnectedElement<T> elem = iterator.next();
|
||||||
|
if (elem.isCompatibleWith(obj)) {
|
||||||
|
iterator.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public Connection connectDynamic(final T function) {
|
||||||
|
Connection out = new Connection(this);
|
||||||
|
synchronized(this.data) {
|
||||||
|
this.data.add(new ConnectedElementDynamic<T>(out, function));
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
public void connectAutoRemoveObject(final Object reference, final T function) {
|
||||||
|
synchronized(this.data) {
|
||||||
|
this.data.add(new ConnectedElementDynamic<T>(reference, function));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void disconnect(final Connection connection) {
|
||||||
|
synchronized(this.data) {
|
||||||
|
final Iterator<ConnectedElement<T>> iterator = this.data.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
final ConnectedElement<T> elem = iterator.next();
|
||||||
|
if (elem.isCompatibleWith(connection)) {
|
||||||
|
elem.disconnect();
|
||||||
|
iterator.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return this.data.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -6,7 +6,6 @@ import java.util.List;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.atriasoft.esignal.internal.ConnectedElement;
|
import org.atriasoft.esignal.internal.ConnectedElement;
|
||||||
import org.atriasoft.esignal.internal.ConnectedElementDynamic;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple interface to manage signal connection and disconnection
|
* Simple interface to manage signal connection and disconnection
|
||||||
@ -52,64 +51,7 @@ import org.atriasoft.esignal.internal.ConnectedElementDynamic;
|
|||||||
* @param <T> Type of the signal
|
* @param <T> Type of the signal
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Signal<T> implements ConnectionRemoveInterface {
|
public class Signal<T> extends GenericSignal<Consumer<T>> {
|
||||||
List<ConnectedElement<Consumer<T>>> data = new ArrayList<>();
|
|
||||||
|
|
||||||
public void clear() {
|
|
||||||
List<ConnectedElement<Consumer<T>>> data2 = this.data;
|
|
||||||
synchronized(this.data) {
|
|
||||||
this.data = new ArrayList<>();
|
|
||||||
}
|
|
||||||
final Iterator<ConnectedElement<Consumer<T>>> iterator = data2.iterator();
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
final ConnectedElement<Consumer<T>> elem = iterator.next();
|
|
||||||
elem.disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void connect(final Consumer<T> function) {
|
|
||||||
synchronized(this.data) {
|
|
||||||
this.data.add(new ConnectedElement<Consumer<T>>(function));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void disconnect(final Consumer<T> obj) {
|
|
||||||
synchronized(this.data) {
|
|
||||||
final Iterator<ConnectedElement<Consumer<T>>> iterator = this.data.iterator();
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
final ConnectedElement<Consumer<T>> elem = iterator.next();
|
|
||||||
if (elem.isCompatibleWith(obj)) {
|
|
||||||
iterator.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public Connection connectDynamic(final Consumer<T> function) {
|
|
||||||
Connection out = new Connection(this);
|
|
||||||
synchronized(this.data) {
|
|
||||||
this.data.add(new ConnectedElementDynamic<Consumer<T>>(out, function));
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
public void connectAutoRemoveObject(final Object reference, final Consumer<T> function) {
|
|
||||||
synchronized(this.data) {
|
|
||||||
this.data.add(new ConnectedElementDynamic<Consumer<T>>(reference, function));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void disconnect(final Connection connection) {
|
|
||||||
synchronized(this.data) {
|
|
||||||
final Iterator<ConnectedElement<Consumer<T>>> iterator = this.data.iterator();
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
final ConnectedElement<Consumer<T>> elem = iterator.next();
|
|
||||||
if (elem.isCompatibleWith(connection)) {
|
|
||||||
elem.disconnect();
|
|
||||||
iterator.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void emit(final T value) {
|
public void emit(final T value) {
|
||||||
List<ConnectedElement<Consumer<T>>> tmp;
|
List<ConnectedElement<Consumer<T>>> tmp;
|
||||||
// clean the list:
|
// clean the list:
|
||||||
@ -144,6 +86,7 @@ public class Signal<T> implements ConnectionRemoveInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return this.data.size();
|
return this.data.size();
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import java.util.List;
|
|||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
import org.atriasoft.esignal.internal.ConnectedElement;
|
import org.atriasoft.esignal.internal.ConnectedElement;
|
||||||
import org.atriasoft.esignal.internal.ConnectedElementDynamic;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,64 +53,7 @@ import org.atriasoft.esignal.internal.ConnectedElementDynamic;
|
|||||||
* @param <U> Second type of the signal
|
* @param <U> Second type of the signal
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Signal2<T, U> implements ConnectionRemoveInterface {
|
public class Signal2<T, U> extends GenericSignal<BiConsumer<T, U>> {
|
||||||
List<ConnectedElement<BiConsumer<T, U>>> data = new ArrayList<>();
|
|
||||||
|
|
||||||
public void clear() {
|
|
||||||
List<ConnectedElement<BiConsumer<T, U>>> data2 = this.data;
|
|
||||||
synchronized(this.data) {
|
|
||||||
this.data = new ArrayList<>();
|
|
||||||
}
|
|
||||||
final Iterator<ConnectedElement<BiConsumer<T, U>>> iterator = data2.iterator();
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
final ConnectedElement<BiConsumer<T, U>> elem = iterator.next();
|
|
||||||
elem.disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void connect(final BiConsumer<T, U> function) {
|
|
||||||
synchronized(this.data) {
|
|
||||||
this.data.add(new ConnectedElement<BiConsumer<T, U>>(function));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void disconnect(final BiConsumer<T, U> obj) {
|
|
||||||
synchronized(this.data) {
|
|
||||||
final Iterator<ConnectedElement<BiConsumer<T, U>>> iterator = this.data.iterator();
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
final ConnectedElement<BiConsumer<T, U>> elem = iterator.next();
|
|
||||||
if (elem.isCompatibleWith(obj)) {
|
|
||||||
iterator.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public Connection connectDynamic(final BiConsumer<T, U> function) {
|
|
||||||
Connection out = new Connection(this);
|
|
||||||
synchronized(this.data) {
|
|
||||||
this.data.add(new ConnectedElementDynamic<BiConsumer<T, U>>(out, function));
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
public void connectAutoRemoveObject(final Object reference, final BiConsumer<T, U> function) {
|
|
||||||
synchronized(this.data) {
|
|
||||||
this.data.add(new ConnectedElementDynamic<BiConsumer<T, U>>(reference, function));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void disconnect(final Connection connection) {
|
|
||||||
synchronized(this.data) {
|
|
||||||
final Iterator<ConnectedElement<BiConsumer<T, U>>> iterator = this.data.iterator();
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
final ConnectedElement<BiConsumer<T, U>> elem = iterator.next();
|
|
||||||
if (elem.isCompatibleWith(connection)) {
|
|
||||||
elem.disconnect();
|
|
||||||
iterator.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void emit(final T valueT, final U valueU) {
|
public void emit(final T valueT, final U valueU) {
|
||||||
List<ConnectedElement<BiConsumer<T, U>>> tmp;
|
List<ConnectedElement<BiConsumer<T, U>>> tmp;
|
||||||
// clean the list:
|
// clean the list:
|
||||||
@ -145,9 +87,5 @@ public class Signal2<T, U> implements ConnectionRemoveInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size() {
|
|
||||||
return this.data.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ import java.util.Iterator;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.atriasoft.esignal.internal.ConnectedElement;
|
import org.atriasoft.esignal.internal.ConnectedElement;
|
||||||
import org.atriasoft.esignal.internal.ConnectedElementDynamic;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple interface to manage signal connection and disconnection
|
* Simple interface to manage signal connection and disconnection
|
||||||
@ -49,63 +48,7 @@ import org.atriasoft.esignal.internal.ConnectedElementDynamic;
|
|||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class SignalEmpty implements ConnectionRemoveInterface {
|
public class SignalEmpty extends GenericSignal<Runnable> {
|
||||||
List<ConnectedElement<Runnable>> data = new ArrayList<>();
|
|
||||||
|
|
||||||
public void clear() {
|
|
||||||
List<ConnectedElement<Runnable>> data2 = this.data;
|
|
||||||
synchronized(this.data) {
|
|
||||||
this.data = new ArrayList<>();
|
|
||||||
}
|
|
||||||
final Iterator<ConnectedElement<Runnable>> iterator = data2.iterator();
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
final ConnectedElement<Runnable> elem = iterator.next();
|
|
||||||
elem.disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void connect(final Runnable function) {
|
|
||||||
synchronized(this.data) {
|
|
||||||
this.data.add(new ConnectedElement<Runnable>(function));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void disconnect(final Runnable obj) {
|
|
||||||
synchronized(this.data) {
|
|
||||||
final Iterator<ConnectedElement<Runnable>> iterator = this.data.iterator();
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
final ConnectedElement<Runnable> elem = iterator.next();
|
|
||||||
if (elem.isCompatibleWith(obj)) {
|
|
||||||
iterator.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public Connection connectDynamic(final Runnable function) {
|
|
||||||
Connection out = new Connection(this);
|
|
||||||
synchronized(this.data) {
|
|
||||||
this.data.add(new ConnectedElementDynamic<Runnable>(out, function));
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
public void connectAutoRemoveObject(final Object reference, final Runnable function) {
|
|
||||||
synchronized(this.data) {
|
|
||||||
this.data.add(new ConnectedElementDynamic<Runnable>(reference, function));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void disconnect(final Connection connection) {
|
|
||||||
synchronized(this.data) {
|
|
||||||
final Iterator<ConnectedElement<Runnable>> iterator = this.data.iterator();
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
final ConnectedElement<Runnable> elem = iterator.next();
|
|
||||||
if (elem.isCompatibleWith(connection)) {
|
|
||||||
elem.disconnect();
|
|
||||||
iterator.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void emit() {
|
public void emit() {
|
||||||
List<ConnectedElement<Runnable>> tmp;
|
List<ConnectedElement<Runnable>> tmp;
|
||||||
@ -141,8 +84,4 @@ public class SignalEmpty implements ConnectionRemoveInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size() {
|
|
||||||
return this.data.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user