All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rx.core.Delegate Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.core;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.SneakyThrows;
import org.rx.exception.InvalidException;
import org.rx.exception.TraceHandler;
import org.rx.util.function.TripleAction;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.EventObject;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import static org.rx.core.Constants.NON_UNCHECKED;
import static org.rx.core.Extends.tryAs;
import static org.rx.core.Extends.tryClose;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Delegate, TArgs extends EventArgs> implements TripleAction {
    public static , TArgs extends EventArgs> Delegate create() {
        return create((TripleAction[]) null);
    }

    @SafeVarargs
    public static , TArgs extends EventArgs> Delegate create(TripleAction... delegates) {
        return new Delegate().combine(delegates);
    }

    @SuppressWarnings(NON_UNCHECKED)
    @SneakyThrows
    public static , TArgs extends EventArgs> Delegate wrap(@NonNull EventPublisher target, @NonNull String fnName) {
        Delegate d;
        Field field = Reflects.getFieldMap(target.getClass()).get(fnName);
        if (field != null) {
            d = (Delegate) field.get(target);
            if (d == null) {
                throw new InvalidException("Event {} not defined", fnName);
            }
        } else {
            if (!target.eventFlags().has(EventPublisher.EventFlags.DYNAMIC_ATTACH)) {
                throw new InvalidException("Event {} not defined", fnName);
            }
            d = IOC.>weakMap(target, false).computeIfAbsent(fnName, k -> new Delegate<>());
        }
        return d;
    }

    final Set> invocations = new CopyOnWriteArraySet<>();
    volatile TripleAction firstInvocation;
    volatile TripleAction lastInvocation;

    public boolean isEmpty() {
        return invocations.isEmpty() && firstInvocation == null && lastInvocation == null;
    }

    public Delegate first(TripleAction delegate) {
        firstInvocation = delegate;
        return this;
    }

    public Delegate last(TripleAction delegate) {
        lastInvocation = delegate;
        return this;
    }

    @SafeVarargs
    public final Delegate replace(TripleAction... delegates) {
        invocations.clear();
        return combine(delegates);
    }

    @SafeVarargs
    public final Delegate combine(TripleAction... delegates) {
        if (delegates == null) {
            return this;
        }
        for (TripleAction delegate : delegates) {
            if (delegate == null) {
                continue;
            }
            if (!tryAs(delegate, Delegate.class, d -> invocations.addAll(d.invocations))) {
                invocations.add(delegate);
            }
        }
        return this;
    }

    @SafeVarargs
    public final Delegate remove(TripleAction... delegates) {
        if (delegates == null) {
            return this;
        }
        for (TripleAction delegate : delegates) {
            if (delegate == null) {
                continue;
            }
            if (!tryAs(delegate, Delegate.class, d -> invocations.removeAll(d.invocations))) {
                invocations.remove(delegate);
            }
        }
        return this;
    }

    public Delegate close() {
        tryClose(firstInvocation);
        for (TripleAction invocation : invocations) {
            tryClose(invocation);
        }
        tryClose(lastInvocation);
        return purge();
    }

    public Delegate purge() {
        invocations.clear();
        firstInvocation = lastInvocation = null;
        return this;
    }

    public  void invoke(T event) throws Throwable {
        invoke((TSender) event.getSource(), (TArgs) (Object) event);
    }

    @Override
    public void invoke(@NonNull TSender target, @NonNull TArgs args) throws Throwable {
        if (!innerInvoke(firstInvocation, target, args)) {
            return;
        }
        for (TripleAction delegate : invocations) {
            if (!innerInvoke(delegate, target, args)) {
                break;
            }
        }
        innerInvoke(lastInvocation, target, args);
    }

    boolean innerInvoke(TripleAction delegate, TSender target, TArgs args) throws Throwable {
        if (delegate == null) {
            return true;
        }
        try {
            delegate.invoke(target, args);
            return !args.isCancel() && !args.isHandled();
        } catch (Throwable e) {
            if (!target.eventFlags().has(EventPublisher.EventFlags.QUIETLY)) {
                throw e;
            }
            TraceHandler.INSTANCE.log(e);
        }
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy