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
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractEventFactory
(Set<String> supportedEventTypes) Must be called in subclass constructor to define the supported event types. -
Method Summary
Modifier and TypeMethodDescriptionprotected static void
checkNotNull
(@Nullable Object object, String argumentName) protected static void
checkNotNullOrEmpty
(@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 Event
createEventByType
(String eventType, String topic, String payload, @Nullable String source) Create a new event instance based on the event type.protected static <T> T
deserializePayload
(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 String
serializePayload
(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:EventFactory
Create a new event instance of a specific event type.- Specified by:
createEvent
in 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:EventFactory
Returns a list of all supported event types of this factory.- Specified by:
getSupportedEventTypes
in 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
-