Class AbstractGenericEvent<G,T>

Type Parameters:
G - The generic type of the event onto which to distinguish
T - The entity type for AbstractEntityChangedEvent.
All Implemented Interfaces:
DESEvent, SlingshotEvent, ReifiedEvent<G>
Direct Known Subclasses:
ModelVisited

public abstract class AbstractGenericEvent<G,T> extends AbstractEntityChangedEvent<T> implements ReifiedEvent<G>
This abstract event is used for the special case that an event uses generics which need to be distinguished by it. For example, if SampleEvent<TypeA> and SampleEvent<TypeB> are published, then the event dispatcher must be able to distinguish them, since otherwise the generic types are erased at runtime and the dispatcher would call all event handlers listening to SampleEvent regardless of their specific type.

To distinguish the events, a TypeToken is used which can be accessed by the event dispatcher.

An example event could be:

 
 public final class SomeGenericEvent<T> extends AbstractGenericEvent<T, EntityType>
 
 
  • Constructor Details

    • AbstractGenericEvent

      protected AbstractGenericEvent(T entity, double delay)
      Constructs a AbstractGenericEvent without a concrete Class instance. Here, a TypeToken is generated.

      Note that if the type is not known at compile-time, the TypeToken will contain the type of the known super-class. For example, if a class A has three sub-classes B, C, D, but the concrete class is not known and this constructor is used (i.e. new GenericEvent<A, A>(b, 0);), then getTypeToken() will return A, even though b is of instance B. In this case, use #AbstractGenericEvent(Class, T, double) instead.

      Parameters:
      entity - The entity for AbstractEntityChangedEvent.
      delay - The delay of the event.
    • AbstractGenericEvent

      protected AbstractGenericEvent(Class<G> concreteClass, T entity, double delay)
      Constructs an AbstractGenericEvent with a concrete Class information. The TypeToken is generated by using concreteClass, hence getTypeToken() will return the type from concreteClass.

      This constructor is useful if the concrete class information is not known at compile time. Otherwise, if it is possible to instantiate as follows: new GenericEvent<A, Entity>(entity, 0);, then use AbstractGenericEvent(T, double) instead.

      Parameters:
      concreteClass - The concrete class of the type G.
      entity - The entity for AbstractEntityChangedEvent.
      delay - the delay of the event.
  • Method Details

    • getTypeToken

      public com.google.common.reflect.TypeToken<G> getTypeToken()
      Specified by:
      getTypeToken in interface ReifiedEvent<G>
    • getGenericType

      public Class<?> getGenericType()