Изменение делегата
This commit is contained in:
parent
b564eb8ff4
commit
37f392d98e
@ -1,24 +1,40 @@
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
|
||||
public class Delegator<T> {
|
||||
private HashMap<Method, Object> methods = new HashMap<>();
|
||||
private Queue<ArrayList<Object>> methods2 = new LinkedList<>();
|
||||
|
||||
public void add(Method method_ref, Object consumer) {
|
||||
methods.put(method_ref, consumer);
|
||||
ArrayList<Object> element = new ArrayList<>();
|
||||
element.add(method_ref);
|
||||
element.add(consumer);
|
||||
methods2.add(element);
|
||||
}
|
||||
|
||||
public void remove(Object method_ref) {
|
||||
methods.remove(method_ref);
|
||||
public void remove(Method method_ref) {
|
||||
var element = methods2.peek();
|
||||
while (element != null) {
|
||||
if (element.contains(method_ref)) {
|
||||
methods2.remove(element);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void invoke(T arg) {
|
||||
try {
|
||||
Method method = methods.keySet().stream().toList().get(0);
|
||||
FormMapWithArmoredCars consumer = (FormMapWithArmoredCars) methods.get(method);
|
||||
method.setAccessible(true);
|
||||
method.invoke(consumer, arg);
|
||||
// должны вызывать все добавленные ранее методы по очереди
|
||||
var element = methods2.poll();
|
||||
while (element != null) {
|
||||
Method method = (Method) element.get(0);
|
||||
method.setAccessible(true);
|
||||
method.invoke(element.get(1), arg);
|
||||
element = methods2.poll();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
Loading…
Reference in New Issue
Block a user