2019-12-29 09:53:48 +01:00
|
|
|
package envoy.event;
|
|
|
|
|
2020-01-28 17:50:09 +01:00
|
|
|
import java.io.Serializable;
|
2020-10-22 23:58:55 +02:00
|
|
|
import java.util.Objects;
|
2020-01-28 17:50:09 +01:00
|
|
|
|
2020-09-08 20:41:01 +02:00
|
|
|
import dev.kske.eventbus.IEvent;
|
|
|
|
|
2019-12-29 09:53:48 +01:00
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* This class serves as a convenience base class for all events. It implements the {@link IEvent}
|
|
|
|
* interface and provides a generic value. For events without a value there also is
|
|
|
|
* {@link envoy.event.Event.Valueless}.
|
2019-12-29 09:53:48 +01:00
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @param <T> the type of the Event
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2020-09-08 20:41:01 +02:00
|
|
|
public abstract class Event<T> implements IEvent, Serializable {
|
2020-02-11 16:53:41 +01:00
|
|
|
|
|
|
|
protected final T value;
|
|
|
|
|
2020-03-24 19:06:56 +01:00
|
|
|
private static final long serialVersionUID = 0L;
|
2020-02-11 16:53:41 +01:00
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
protected Event(T value) {
|
2020-10-22 23:58:55 +02:00
|
|
|
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);
|
2020-10-19 18:17:51 +02:00
|
|
|
}
|
2019-12-29 09:53:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the data associated with this event
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public T get() {
|
|
|
|
return value;
|
|
|
|
}
|
2020-02-11 16:53:41 +01:00
|
|
|
|
|
|
|
@Override
|
2020-10-19 18:17:51 +02:00
|
|
|
public String toString() {
|
|
|
|
return String.format("%s[value=%s]", this.getClass().getSimpleName(), value);
|
|
|
|
}
|
2020-02-11 16:53:41 +01:00
|
|
|
|
|
|
|
/**
|
2020-09-25 14:29:23 +02:00
|
|
|
* Serves as a super class for events that do not carry a value.
|
2020-02-11 16:53:41 +01:00
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public static abstract class Valueless extends Event<Void> {
|
|
|
|
|
2020-03-24 19:06:56 +01:00
|
|
|
private static final long serialVersionUID = 0L;
|
2020-02-11 16:53:41 +01:00
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
protected Valueless() {
|
2020-10-22 23:58:55 +02:00
|
|
|
super(null, true);
|
2020-10-19 18:17:51 +02:00
|
|
|
}
|
2020-02-12 22:15:42 +01:00
|
|
|
|
|
|
|
@Override
|
2020-10-19 18:17:51 +02:00
|
|
|
public String toString() {
|
|
|
|
return this.getClass().getSimpleName();
|
|
|
|
}
|
2020-02-11 16:53:41 +01:00
|
|
|
}
|
2019-12-29 09:53:48 +01:00
|
|
|
}
|