[DEV] reduce code with generic class

This commit is contained in:
Edouard DUPIN 2021-06-04 15:12:18 +02:00
parent e43388d091
commit 0708bf47dc
5 changed files with 138 additions and 241 deletions

View File

@ -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<T> {
protected final WeakReference<Consumer<T>> consumer;
public ConnectedElement(final Consumer<T> consumer) {
this.consumer = new WeakReference<Consumer<T>>(consumer);
}
public Consumer<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() {
}
}
class ConnectedElementDynamic<T> extends ConnectedElement<T> {
protected final WeakReference<Object> linkedObject;
public ConnectedElementDynamic(final Object linkedObject, final Consumer<T> consumer) {
super(consumer);
this.linkedObject = new WeakReference<Object>(linkedObject);
}
@Override
public Consumer<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();
}
}
}
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<T> extends ConnectedElement<T> {
*
*/
public class Signal<T> implements ConnectionRemoveInterface {
List<ConnectedElement<T>> data = new ArrayList<>();
List<ConnectedElement<Consumer<T>>> data = new ArrayList<>();
public void clear() {
List<ConnectedElement<T>> data2 = this.data;
List<ConnectedElement<Consumer<T>>> data2 = this.data;
synchronized(this.data) {
this.data = new ArrayList<>();
}
final Iterator<ConnectedElement<T>> iterator = data2.iterator();
final Iterator<ConnectedElement<Consumer<T>>> iterator = data2.iterator();
while (iterator.hasNext()) {
final ConnectedElement<T> elem = iterator.next();
final ConnectedElement<Consumer<T>> elem = iterator.next();
elem.disconnect();
}
}
public void connect(final Consumer<T> function) {
synchronized(this.data) {
this.data.add(new ConnectedElement<T>(function));
this.data.add(new ConnectedElement<Consumer<T>>(function));
}
}
public void disconnect(final Consumer<T> obj) {
synchronized(this.data) {
final Iterator<ConnectedElement<T>> iterator = this.data.iterator();
final Iterator<ConnectedElement<Consumer<T>>> iterator = this.data.iterator();
while (iterator.hasNext()) {
final ConnectedElement<T> elem = iterator.next();
final ConnectedElement<Consumer<T>> elem = iterator.next();
if (elem.isCompatibleWith(obj)) {
iterator.remove();
}
@ -148,22 +86,22 @@ public class Signal<T> implements ConnectionRemoveInterface {
public Connection connectDynamic(final Consumer<T> function) {
Connection out = new Connection(this);
synchronized(this.data) {
this.data.add(new ConnectedElementDynamic<T>(out, function));
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<T>(reference, function));
this.data.add(new ConnectedElementDynamic<Consumer<T>>(reference, function));
}
}
@Override
public void disconnect(final Connection connection) {
synchronized(this.data) {
final Iterator<ConnectedElement<T>> iterator = this.data.iterator();
final Iterator<ConnectedElement<Consumer<T>>> iterator = this.data.iterator();
while (iterator.hasNext()) {
final ConnectedElement<T> elem = iterator.next();
final ConnectedElement<Consumer<T>> elem = iterator.next();
if (elem.isCompatibleWith(connection)) {
elem.disconnect();
iterator.remove();
@ -173,12 +111,12 @@ public class Signal<T> implements ConnectionRemoveInterface {
}
public void emit(final T value) {
List<ConnectedElement<T>> tmp;
List<ConnectedElement<Consumer<T>>> tmp;
// clean the list:
synchronized(this.data) {
final Iterator<ConnectedElement<T>> iterator = this.data.iterator();
final Iterator<ConnectedElement<Consumer<T>>> iterator = this.data.iterator();
while (iterator.hasNext()) {
final ConnectedElement<T> elem = iterator.next();
final ConnectedElement<Consumer<T>> elem = iterator.next();
Object tmpObject = elem.getConsumer();
if (tmpObject == null) {
elem.disconnect();
@ -194,9 +132,9 @@ public class Signal<T> implements ConnectionRemoveInterface {
}
// real call elements
{
final Iterator<ConnectedElement<T>> iterator = tmp.iterator();
final Iterator<ConnectedElement<Consumer<T>>> iterator = tmp.iterator();
while (iterator.hasNext()) {
final ConnectedElement<T> elem = iterator.next();
final ConnectedElement<Consumer<T>> elem = iterator.next();
Consumer<T> tmpObject = elem.getConsumer();
if (tmpObject == null) {
continue;

View File

@ -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<T, U> {
protected final WeakReference<BiConsumer<T, U>> consumer;
public BiConnectedElement(final BiConsumer<T, U> consumer) {
this.consumer = new WeakReference<BiConsumer<T, U>>(consumer);
}
public BiConsumer<T, U> 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<T, U> extends BiConnectedElement<T, U> {
protected final WeakReference<Object> linkedObject;
public BiConnectedElementDynamic(final Object linkedObject, final BiConsumer<T, U> consumer) {
super(consumer);
this.linkedObject = new WeakReference<Object>(linkedObject);
}
@Override
public BiConsumer<T, U> 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<T, U> extends BiConnectedElement<T, U> {
*
*/
public class Signal2<T, U> implements ConnectionRemoveInterface {
List<BiConnectedElement<T, U>> data = new ArrayList<>();
List<ConnectedElement<BiConsumer<T, U>>> data = new ArrayList<>();
public void clear() {
List<BiConnectedElement<T, U>> data2 = this.data;
List<ConnectedElement<BiConsumer<T, U>>> data2 = this.data;
synchronized(this.data) {
this.data = new ArrayList<>();
}
final Iterator<BiConnectedElement<T, U>> iterator = data2.iterator();
final Iterator<ConnectedElement<BiConsumer<T, U>>> iterator = data2.iterator();
while (iterator.hasNext()) {
final BiConnectedElement<T, U> elem = iterator.next();
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 BiConnectedElement<T, U>(function));
this.data.add(new ConnectedElement<BiConsumer<T, U>>(function));
}
}
public void disconnect(final BiConsumer<T, U> obj) {
synchronized(this.data) {
final Iterator<BiConnectedElement<T, U>> iterator = this.data.iterator();
final Iterator<ConnectedElement<BiConsumer<T, U>>> iterator = this.data.iterator();
while (iterator.hasNext()) {
final BiConnectedElement<T, U> elem = iterator.next();
final ConnectedElement<BiConsumer<T, U>> elem = iterator.next();
if (elem.isCompatibleWith(obj)) {
iterator.remove();
}
@ -149,22 +88,22 @@ public class Signal2<T, U> implements ConnectionRemoveInterface {
public Connection connectDynamic(final BiConsumer<T, U> function) {
Connection out = new Connection(this);
synchronized(this.data) {
this.data.add(new BiConnectedElementDynamic<T, U>(out, function));
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 BiConnectedElementDynamic<T, U>(reference, function));
this.data.add(new ConnectedElementDynamic<BiConsumer<T, U>>(reference, function));
}
}
@Override
public void disconnect(final Connection connection) {
synchronized(this.data) {
final Iterator<BiConnectedElement<T, U>> iterator = this.data.iterator();
final Iterator<ConnectedElement<BiConsumer<T, U>>> iterator = this.data.iterator();
while (iterator.hasNext()) {
final BiConnectedElement<T, U> elem = iterator.next();
final ConnectedElement<BiConsumer<T, U>> elem = iterator.next();
if (elem.isCompatibleWith(connection)) {
elem.disconnect();
iterator.remove();
@ -174,12 +113,12 @@ public class Signal2<T, U> implements ConnectionRemoveInterface {
}
public void emit(final T valueT, final U valueU) {
List<BiConnectedElement<T, U>> tmp;
List<ConnectedElement<BiConsumer<T, U>>> tmp;
// clean the list:
synchronized(this.data) {
final Iterator<BiConnectedElement<T, U>> iterator = this.data.iterator();
final Iterator<ConnectedElement<BiConsumer<T, U>>> iterator = this.data.iterator();
while (iterator.hasNext()) {
final BiConnectedElement<T, U> elem = iterator.next();
final ConnectedElement<BiConsumer<T, U>> elem = iterator.next();
Object tmpObject = elem.getConsumer();
if (tmpObject == null) {
elem.disconnect();
@ -195,9 +134,9 @@ public class Signal2<T, U> implements ConnectionRemoveInterface {
}
// real call elements
{
final Iterator<BiConnectedElement<T, U>> iterator = tmp.iterator();
final Iterator<ConnectedElement<BiConsumer<T, U>>> iterator = tmp.iterator();
while (iterator.hasNext()) {
final BiConnectedElement<T, U> elem = iterator.next();
final ConnectedElement<BiConsumer<T, U>> elem = iterator.next();
BiConsumer<T, U> tmpObject = elem.getConsumer();
if (tmpObject == null) {
continue;

View File

@ -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<Runnable> runner;
public ConnectedElementEmpty(final Runnable runner) {
this.runner = new WeakReference<Runnable>(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<Object> linkedObject;
public ConnectedElementDynamicEmpty(final Object linkedObject, final Runnable runner) {
super(runner);
this.linkedObject = new WeakReference<Object>(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<ConnectedElementEmpty> data = new ArrayList<>();
List<ConnectedElement<Runnable>> data = new ArrayList<>();
public void clear() {
List<ConnectedElementEmpty> data2 = this.data;
List<ConnectedElement<Runnable>> data2 = this.data;
synchronized(this.data) {
this.data = new ArrayList<>();
}
final Iterator<ConnectedElementEmpty> iterator = data2.iterator();
final Iterator<ConnectedElement<Runnable>> iterator = data2.iterator();
while (iterator.hasNext()) {
final ConnectedElementEmpty elem = iterator.next();
final ConnectedElement<Runnable> 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<Runnable>(function));
}
}
public void disconnect(final Runnable obj) {
synchronized(this.data) {
final Iterator<ConnectedElementEmpty> iterator = this.data.iterator();
final Iterator<ConnectedElement<Runnable>> iterator = this.data.iterator();
while (iterator.hasNext()) {
final ConnectedElementEmpty elem = iterator.next();
final ConnectedElement<Runnable> 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<Runnable>(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<Runnable>(reference, function));
}
}
@Override
public void disconnect(final Connection connection) {
synchronized(this.data) {
final Iterator<ConnectedElementEmpty> iterator = this.data.iterator();
final Iterator<ConnectedElement<Runnable>> iterator = this.data.iterator();
while (iterator.hasNext()) {
final ConnectedElementEmpty elem = iterator.next();
final ConnectedElement<Runnable> elem = iterator.next();
if (elem.isCompatibleWith(connection)) {
elem.disconnect();
iterator.remove();
@ -170,13 +108,13 @@ public class SignalEmpty implements ConnectionRemoveInterface {
}
public void emit() {
List<ConnectedElementEmpty> tmp;
List<ConnectedElement<Runnable>> tmp;
// clean the list:
synchronized(this.data) {
final Iterator<ConnectedElementEmpty> iterator = this.data.iterator();
final Iterator<ConnectedElement<Runnable>> iterator = this.data.iterator();
while (iterator.hasNext()) {
final ConnectedElementEmpty elem = iterator.next();
Object tmpObject = elem.getRunner();
final ConnectedElement<Runnable> 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<ConnectedElementEmpty> iterator = tmp.iterator();
final Iterator<ConnectedElement<Runnable>> iterator = tmp.iterator();
while (iterator.hasNext()) {
final ConnectedElementEmpty elem = iterator.next();
Runnable tmpObject = elem.getRunner();
final ConnectedElement<Runnable> elem = iterator.next();
Runnable tmpObject = elem.getConsumer();
if (tmpObject == null) {
continue;
}

View File

@ -0,0 +1,32 @@
package org.atriasoft.esignal.internal;
import java.lang.ref.WeakReference;
/**
* Connected element on a consumer to Manage event
*
* @param <T> Type Of the Signal
*/
public class ConnectedElement<T> {
protected final WeakReference<T> consumer;
public ConnectedElement(final T consumer) {
this.consumer = new WeakReference<T>(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() {
}
}

View File

@ -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 <T> Type Of the Signal
*/
public class ConnectedElementDynamic<T> extends ConnectedElement<T> {
protected final WeakReference<Object> linkedObject;
public ConnectedElementDynamic(final Object linkedObject, final T consumer) {
super(consumer);
this.linkedObject = new WeakReference<Object>(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();
}
}
}