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 com.google.common.eventbus.EventBus;
import lombok.*;
import org.rx.exception.ExceptionHandler;
import org.rx.exception.InvalidException;
import org.rx.util.function.TripleAction;

import java.lang.reflect.Field;
import java.util.*;
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 {
    static {
        Container.register(EventBus.class, new EventBus());
    }

    public static void register(Object eventListener) {
        Container.get(EventBus.class).register(eventListener);
    }

    public static void post(Object eventObject) {
        Container.get(EventBus.class).post(eventObject);
    }

    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 EventTarget 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(EventTarget.EventFlags.DYNAMIC_ATTACH)) {
                throw new InvalidException("Event {} not defined", fnName);
            }
            d = Extends.>weakMap(target).computeIfAbsent(fnName, k -> new Delegate<>());
        }
        return d;
    }

    final Set> invocations = new CopyOnWriteArraySet<>();
    volatile TripleAction headInvocation;
    volatile TripleAction tailInvocation;

    public boolean isEmpty() {
        return invocations.isEmpty() && headInvocation == null && tailInvocation == null;
    }

    public Delegate head(TripleAction delegate) {
        headInvocation = delegate;
        return this;
    }

    public Delegate tail(TripleAction delegate) {
        tailInvocation = 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(headInvocation);
        for (TripleAction invocation : invocations) {
            tryClose(invocation);
        }
        tryClose(tailInvocation);
        return purge();
    }

    public Delegate purge() {
        invocations.clear();
        headInvocation = tailInvocation = null;
        return this;
    }

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy