
package org.citydb.util.event;

import java.lang.ref.WeakReference;
import java.util.Objects;

public abstract class Event {
	public static final Object GLOBAL_CHANNEL = new Object();
	
	private final Enum<?> eventType;
	private WeakReference<Object> channel;
	private boolean cancelled;

	public Event(Enum<?> eventType, Object channel) {
		this.eventType = Objects.requireNonNull(eventType, "The type of an event may not be null.");
		cancelled = false;
		setChannel(channel);
	}

	public boolean isCancelled() {
		return cancelled;
	}

	public void setCancelled(boolean cancelled) {
		this.cancelled = cancelled;
	}

	public Enum<?> getEventType() {
		return eventType;
	}

	public Object getChannel() {
		return channel.get();
	}

	public void setChannel(Object channel) {
		this.channel = new WeakReference<>(channel != null ? channel : GLOBAL_CHANNEL);
	}
}
