This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/common/src/main/java/envoy/event/Event.java

65 lines
1.5 KiB
Java

package envoy.event;
import java.io.Serializable;
import java.util.Objects;
/**
* This class serves as a convenience base class for all events. It provides a generic value. For
* events without a value there also is {@link envoy.event.Event.Valueless}.
*
* @author Kai S. K. Engelbart
* @param <T> the type of the Event
* @since Envoy v0.2-alpha
*/
public abstract class Event<T> implements Serializable {
protected final T value;
private static final long serialVersionUID = 0L;
protected Event(T value) {
this(value, false);
}
/**
* This constructor is reserved for {@link Valueless} events. No other event should contain null
* values. Only use if really necessary. Using this constructor with {@code true} implies that
* the user has to manually check if the value of the event is null.
*/
protected Event(T value, boolean canBeNull) {
this.value = canBeNull ? value : Objects.requireNonNull(value);
}
/**
* @return the data associated with this event
*/
public T get() {
return value;
}
@Override
public String toString() {
return String.format("%s[value=%s]", this.getClass().getSimpleName(), value);
}
/**
* Serves as a super class for events that do not carry a value.
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public static abstract class Valueless extends Event<Void> {
private static final long serialVersionUID = 0L;
protected Valueless() {
super(null, true);
}
@Override
public String toString() {
return this.getClass().getSimpleName();
}
}
}