package com.jscape.util.event;

import java.util.List;

/**
 * @author Alex Usun
 */
public abstract class AbstractEvent {

    /**
     * Event context.
     */
    protected final EventContext context;
    /**
     * Handled.
     */
    protected boolean handled;

    /**
     * @param context EventContext instance
     */
    public AbstractEvent(EventContext context) {
        this.context = context;
    }

    /**
     * @return EventContext instance
     */
    public EventContext getContext() {
        return this.context;
    }

    /**
     * @return handled
     */
    protected boolean isHandled() {
        return this.handled;
    }

    /** */
    protected void setHandled() {
        this.handled = true;
    }

    /** */
    protected void assertHandled() {
        if (!this.handled) {
            throw new HandleException(this);
        }
    }

    /** */
    protected void iterateHandlers() {
        try {
            List<EventHandler> handlers = this.context.getHandlers(getClass());

            if (handlers == null) {
                return;
            }

            for (int i = 0, size = handlers.size(); i < size; ++i) {
                accept(handlers.get(i));
            }
        } catch (Exception e) {
            // todo: implement
        }
    }

    /**
     * @param handler handler
     */
    protected abstract void accept(EventHandler handler);
}
