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.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.*;

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

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

    @SuppressWarnings(NON_UNCHECKED)
    @SneakyThrows
    public static , TEvent> 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 eventObj) throws Throwable {
        invoke((TSender) eventObj.getSource(), (TEvent) eventObj);
    }

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

    boolean innerInvoke(TripleAction delegate, TSender target, TEvent event) throws Throwable {
        if (delegate == null) {
            return true;
        }
        try {
            delegate.invoke(target, event);
            EventArgs args = as(event, EventArgs.class);
            return args == null || !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