il.ac.bgu.cs.bp.bpjs.model.BEvent Maven / Gradle / Ivy
package il.ac.bgu.cs.bp.bpjs.model;
import il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSet;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
import org.mozilla.javascript.ConsString;
import org.mozilla.javascript.ScriptableObject;
/**
* A base class for events. Each event has a name and optional data, which is a
* Javascript object.
*
* For two events to be equal, their names and data have to match.
*
* Each event implicitly defines a singleton {@link EventSet}, which contains
* only itself.
*/
@SuppressWarnings("serial")
public class BEvent implements Comparable, EventSet, java.io.Serializable {
private static final AtomicInteger INSTANCE_ID_GEN = new AtomicInteger(0);
/**
* Name of the event. Public access, so that the Javascript code feels
* natural.
*/
public final String name;
/**
* Extra data for the event. Public access, so that the Javascript code
* feels natural.
*/
public final Object maybeData;
public static BEvent named(String aName) {
return new BEvent(aName);
}
public BEvent(String aName) {
this(aName, null);
}
public BEvent(String aName, Object someData) {
name = aName;
maybeData = someData;
}
public BEvent() {
this("BEvent-" + INSTANCE_ID_GEN.incrementAndGet());
}
@Override
public String toString() {
return "[BEvent name:" + name + (getDataField().map(v -> " data:" + v).orElse("")) + "]";
}
public String getName() {
return name;
}
/**
* @return The data field of the event.
*/
public Optional