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

com.github.frontear.efkolia.impl.events.EventMethod Maven / Gradle / Ivy

There is a newer version: 0.2.8
Show newest version
package com.github.frontear.efkolia.impl.events;

import com.github.frontear.efkolia.api.events.*;
import java.lang.reflect.Method;
import java.util.function.Consumer;
import lombok.*;

@EqualsAndHashCode
final class EventMethod implements Comparable> {
    final int hash;
    private final Consumer listener;
    private final Priority priority;

    public EventMethod(@NonNull final Object instance, @NonNull final Method method) {
        this.hash = instance.hashCode();
        this.listener = new Consumer() {
            @Override
            @SneakyThrows(ReflectiveOperationException.class)
            public void accept(final Event event) {
                method.invoke(instance, event);
            }
        };
        this.priority = method.getAnnotation(Listener.class).value();

        method.setAccessible(true);
    }

    public EventMethod(@NonNull final Consumer listener) {
        this.hash = listener.hashCode();
        //noinspection unchecked
        this.listener = x -> listener.accept((E) x);
        this.priority = Priority.NORMAL;
    }

    public void invoke(@NonNull final Event event) {
        listener.accept(event);
    }

    @Override
    public int compareTo(@NonNull final EventMethod other) {
        return other.priority.compareTo(this.priority);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy