Class AbstractEventFactory

java.lang.Object
org.openhab.core.events.AbstractEventFactory
All Implemented Interfaces:
EventFactory
Direct Known Subclasses:
AddonEventFactory, AutomationEventFactory, ConfigStatusEventFactory, FirmwareEventFactory, InboxEventFactory, ItemEventFactory, LinkEventFactory, SystemEventFactory, ThingEventFactory

@NonNullByDefault public abstract class AbstractEventFactory extends Object implements EventFactory
The AbstractEventFactory defines an abstract implementation of the EventFactory interface. Subclasses must implement the abstract method createEventByType(String, String, String, String) in order to create event instances based on the event type.
Author:
Stefan Bußweiler - Initial contribution
  • Constructor Details

    • AbstractEventFactory

      protected AbstractEventFactory(Set<String> supportedEventTypes)
      Must be called in subclass constructor to define the supported event types.
      Parameters:
      supportedEventTypes - the supported event types
  • Method Details

    • createEvent

      public Event createEvent(String eventType, String topic, String payload, @Nullable String source) throws Exception
      Description copied from interface: EventFactory
      Create a new event instance of a specific event type.
      Specified by:
      createEvent in interface EventFactory
      Parameters:
      eventType - the event type
      topic - the topic
      payload - the payload
      source - the source (can be null)
      Returns:
      the created event instance (not null)
      Throws:
      IllegalArgumentException - if eventType, topic or payload is null or empty
      Exception - if the creation of the event has failed
    • getSupportedEventTypes

      public Set<String> getSupportedEventTypes()
      Description copied from interface: EventFactory
      Returns a list of all supported event types of this factory.
      Specified by:
      getSupportedEventTypes in interface EventFactory
      Returns:
      the supported event types (not null)
    • createEventByType

      protected abstract Event createEventByType(String eventType, String topic, String payload, @Nullable String source) throws Exception
      Create a new event instance based on the event type.
      Parameters:
      eventType - the event type
      topic - the topic
      payload - the payload
      source - the source, can be null
      Returns:
      the created event instance
      Throws:
      Exception - if the creation of the event fails
    • serializePayload

      protected static String serializePayload(Object payloadObject)
      Serializes the payload object into its equivalent Json representation.
      Parameters:
      payloadObject - the payload object to serialize
      Returns:
      a serialized Json representation
    • deserializePayload

      protected static <T> T deserializePayload(String payload, Class<T> classOfPayload)
      Deserializes the Json-payload into an object of the specified class.
      Type Parameters:
      T - the type of the returned object
      Parameters:
      payload - the payload from which the object is to be deserialized
      classOfPayload - the class T of the payload object
      Returns:
      an object of type T from the payload
    • getTopicElements

      protected String[] getTopicElements(String topic)
      Gets the elements of the topic (splitted by '/').
      Parameters:
      topic - the topic
      Returns:
      the topic elements
    • checkNotNull

      protected static void checkNotNull(@Nullable Object object, String argumentName)
    • checkNotNullOrEmpty

      protected static void checkNotNullOrEmpty(@Nullable String string, String argumentName)