/*
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.events;

import java.util.function.Consumer;
import java.util.function.Function;

/**
 * Interface representing listener which is able to process event of specified type.
 *
 * Please note, that whole package is organized around idea, that events can be filtered out
 * or selected by type.
 *
 * @param <T> type of supported event
 *
 * @author ranides
 */
public interface EventListener<T extends Event> {

    /**
     * This method will be executed if some event is observed.
     *
     * Please note, that it is impossible to predict which thread will execute this method.
     * It is responsibility of {@link EventRouter} to decide where execution is scheduled.
     * If this listener is attached to many event routers, then it could be invoked in many,
     * completely different ways.
     *
     * @param event event
     */
    void handleEvent(T event);

    /**
     * Converts this listener to plain Consumer interface.
     * Method "handleEvent" will be executed in the same thread as "Consumer#accept".
     *
     * @param function function
     * @param <R> R
     * @return Consumer
     */
    default <R> Consumer<R> consumer(Function<? super R, ? extends T> function) {
        return r -> { handleEvent(function.apply(r)); };
    }
    
}
