Package org.openhab.core.events
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
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
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractEventFactory(Set<String> supportedEventTypes) Must be called in subclass constructor to define the supported event types. -
Method Summary
Modifier and TypeMethodDescriptionprotected static voidcheckNotNull(@Nullable Object object, String argumentName) protected static voidcheckNotNullOrEmpty(@Nullable String string, String argumentName) createEvent(String eventType, String topic, String payload, @Nullable String source) Create a new event instance of a specific event type.protected abstract EventcreateEventByType(String eventType, String topic, String payload, @Nullable String source) Create a new event instance based on the event type.protected static <T> TdeserializePayload(String payload, Class<T> classOfPayload) Deserializes the Json-payload into an object of the specified class.Returns a list of all supported event types of this factory.protected String[]getTopicElements(String topic) Gets the elements of the topic (splitted by '/').protected static StringserializePayload(Object payloadObject) Serializes the payload object into its equivalent Json representation.
-
Constructor Details
-
AbstractEventFactory
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:EventFactoryCreate a new event instance of a specific event type.- Specified by:
createEventin interfaceEventFactory- Parameters:
eventType- the event typetopic- the topicpayload- the payloadsource- the source (can be null)- Returns:
- the created event instance (not null)
- Throws:
Exception- if the creation of the event has failed
-
getSupportedEventTypes
Description copied from interface:EventFactoryReturns a list of all supported event types of this factory.- Specified by:
getSupportedEventTypesin interfaceEventFactory- 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 typetopic- the topicpayload- the payloadsource- the source, can be null- Returns:
- the created event instance
- Throws:
Exception- if the creation of the event fails
-
serializePayload
Serializes the payload object into its equivalent Json representation.- Parameters:
payloadObject- the payload object to serialize- Returns:
- a serialized Json representation
-
deserializePayload
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 deserializedclassOfPayload- the class T of the payload object- Returns:
- an object of type T from the payload
-
getTopicElements
Gets the elements of the topic (splitted by '/').- Parameters:
topic- the topic- Returns:
- the topic elements
-
checkNotNull
-
checkNotNullOrEmpty
-