Package org.openhab.core.items
Class GenericItem
- java.lang.Object
-
- org.openhab.core.items.GenericItem
-
- All Implemented Interfaces:
Identifiable<String>
,ActiveItem
,Item
- Direct Known Subclasses:
CallItem
,ContactItem
,DateTimeItem
,GroupItem
,ImageItem
,LocationItem
,NumberItem
,PlayerItem
,RollershutterItem
,StringItem
,SwitchItem
@NonNullByDefault public abstract class GenericItem extends Object implements ActiveItem
The abstract base class for all items. It provides all relevant logic for the infrastructure, such as publishing updates to the event bus or notifying listeners.- Author:
- Kai Kreuzer - Initial contribution, Andre Fuechsel - Added tags, Stefan Bußweiler - Migration to new ESH event concept
-
-
Field Summary
Fields Modifier and Type Field Description protected @Nullable String
category
protected @Nullable EventPublisher
eventPublisher
protected List<String>
groupNames
protected @Nullable ItemStateConverter
itemStateConverter
protected @Nullable String
label
protected Set<StateChangeListener>
listeners
protected String
name
protected State
state
protected Set<String>
tags
protected String
type
protected @Nullable UnitProvider
unitProvider
-
Constructor Summary
Constructors Constructor Description GenericItem(String type, String name)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addGroupName(String groupItemName)
Adds a group name to theGenericItem
.void
addGroupNames(String... groupItemNames)
Assigns the according item to the given groups.void
addGroupNames(List<String> groupItemNames)
Assigns the according item to the given groups.void
addStateChangeListener(StateChangeListener listener)
void
addTag(String tag)
Adds a tag to the item.void
addTags(String... tags)
Adds tags to the item.void
addTags(Collection<String> tags)
Adds tags to the item.protected void
applyState(State state)
Sets new state, notifies listeners and sends events.void
dispose()
Disposes this item.boolean
equals(@Nullable Object obj)
@Nullable String
getCategory()
Returns the category of the item or null if no category is set.@Nullable CommandDescription
getCommandDescription(@Nullable Locale locale)
Returns theCommandDescription
for the given locale.List<String>
getGroupNames()
Returns a list of the names of the groups this item belongs to.@Nullable String
getLabel()
Returns the label of the item or null if no label is set.String
getName()
returns the name of the itemState
getState()
returns the current state of the item<T extends State>
@Nullable TgetStateAs(Class<T> typeClass)
returns the current state of the item as a specific type@Nullable StateDescription
getStateDescription()
Returns the first provided state description (uses the default locale).@Nullable StateDescription
getStateDescription(@Nullable Locale locale)
Returns the first provided state description for a given locale.Set<String>
getTags()
Returns a set of tags.String
getType()
returns the item type as defined byItemFactory
sString
getUID()
Get the unique identifier.int
hashCode()
boolean
hasTag(String tag)
Returns true if the item's tags contains the specific tag, otherwise false.protected void
internalSend(Command command)
boolean
isAcceptedState(List<Class<? extends State>> acceptedDataTypes, State state)
Tests if state is within acceptedDataTypes list or a subclass of one of themprotected void
logSetTypeError(State state)
protected void
notifyListeners(State oldState, State newState)
void
removeAllTags()
Clears all tags of this item.void
removeGroupName(String groupItemName)
Removes a group item name from theGenericItem
.void
removeStateChangeListener(StateChangeListener listener)
void
removeTag(String tag)
Removes a tag from the item.void
send(RefreshType command)
void
setCategory(@Nullable String category)
Sets the category of the item (can be null)void
setCommandDescriptionService(@Nullable CommandDescriptionService commandDescriptionService)
void
setEventPublisher(@Nullable EventPublisher eventPublisher)
void
setItemStateConverter(@Nullable ItemStateConverter itemStateConverter)
void
setLabel(@Nullable String label)
Sets the label of an itemvoid
setState(State state)
Set a new state.void
setStateDescriptionService(@Nullable StateDescriptionService stateDescriptionService)
void
setUnitProvider(@Nullable UnitProvider unitProvider)
String
toString()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.openhab.core.items.Item
getAcceptedCommandTypes, getAcceptedDataTypes, getCommandDescription
-
-
-
-
Field Detail
-
eventPublisher
protected @Nullable EventPublisher eventPublisher
-
listeners
protected Set<StateChangeListener> listeners
-
name
protected final String name
-
type
protected final String type
-
state
protected State state
-
label
protected @Nullable String label
-
category
protected @Nullable String category
-
unitProvider
protected @Nullable UnitProvider unitProvider
-
itemStateConverter
protected @Nullable ItemStateConverter itemStateConverter
-
-
Method Detail
-
getState
public State getState()
Description copied from interface:Item
returns the current state of the item
-
getStateAs
public <T extends State> @Nullable T getStateAs(Class<T> typeClass)
Description copied from interface:Item
returns the current state of the item as a specific type- Specified by:
getStateAs
in interfaceItem
- Returns:
- the current state in the requested type or null, if state cannot be provided as the requested type
-
getUID
public String getUID()
Description copied from interface:Identifiable
Get the unique identifier.- Specified by:
getUID
in interfaceIdentifiable<String>
- Returns:
- the unique identifier
-
getName
public String getName()
Description copied from interface:Item
returns the name of the item
-
getType
public String getType()
Description copied from interface:Item
returns the item type as defined byItemFactory
s
-
getGroupNames
public List<String> getGroupNames()
Description copied from interface:Item
Returns a list of the names of the groups this item belongs to.- Specified by:
getGroupNames
in interfaceItem
- Returns:
- list of item group names
-
addGroupName
public void addGroupName(String groupItemName)
Adds a group name to theGenericItem
.- Specified by:
addGroupName
in interfaceActiveItem
- Parameters:
groupItemName
- group item name to add- Throws:
IllegalArgumentException
- if groupItemName isnull
-
addGroupNames
public void addGroupNames(String... groupItemNames)
Description copied from interface:ActiveItem
Assigns the according item to the given groups.- Specified by:
addGroupNames
in interfaceActiveItem
- Parameters:
groupItemNames
- names of the groups (must not be null)
-
addGroupNames
public void addGroupNames(List<String> groupItemNames)
Description copied from interface:ActiveItem
Assigns the according item to the given groups.- Specified by:
addGroupNames
in interfaceActiveItem
- Parameters:
groupItemNames
- names of the groups (must not be null)
-
removeGroupName
public void removeGroupName(String groupItemName)
Removes a group item name from theGenericItem
.- Specified by:
removeGroupName
in interfaceActiveItem
- Parameters:
groupItemName
- group item name to remove- Throws:
IllegalArgumentException
- if groupItemName isnull
-
dispose
public void dispose()
Disposes this item. Clears all injected services and unregisters all change listeners. This does not remove this item from its groups. Removing from groups should be done externally to retain the member order in case this item is exchanged in a group.
-
setEventPublisher
public void setEventPublisher(@Nullable EventPublisher eventPublisher)
-
setStateDescriptionService
public void setStateDescriptionService(@Nullable StateDescriptionService stateDescriptionService)
-
setCommandDescriptionService
public void setCommandDescriptionService(@Nullable CommandDescriptionService commandDescriptionService)
-
setUnitProvider
public void setUnitProvider(@Nullable UnitProvider unitProvider)
-
setItemStateConverter
public void setItemStateConverter(@Nullable ItemStateConverter itemStateConverter)
-
internalSend
protected void internalSend(Command command)
-
setState
public void setState(State state)
Set a new state. Subclasses may override this method in order to do necessary conversions upfront. Afterwards,applyState(State)
should be called by classes overriding this method.- Parameters:
state
- new state of this item
-
applyState
protected final void applyState(State state)
Sets new state, notifies listeners and sends events. Classes overriding thesetState(State)
method should call this method in order to actually set the state, inform listeners and send the event.- Parameters:
state
- new state of this item
-
send
public void send(RefreshType command)
-
addStateChangeListener
public void addStateChangeListener(StateChangeListener listener)
-
removeStateChangeListener
public void removeStateChangeListener(StateChangeListener listener)
-
getTags
public Set<String> getTags()
Description copied from interface:Item
Returns a set of tags. If the item is not tagged, an empty set is returned.
-
hasTag
public boolean hasTag(String tag)
Description copied from interface:Item
Returns true if the item's tags contains the specific tag, otherwise false.
-
addTag
public void addTag(String tag)
Description copied from interface:ActiveItem
Adds a tag to the item.- Specified by:
addTag
in interfaceActiveItem
- Parameters:
tag
- a tag that is to be added to item's tags.
-
addTags
public void addTags(Collection<String> tags)
Description copied from interface:ActiveItem
Adds tags to the item.- Specified by:
addTags
in interfaceActiveItem
- Parameters:
tags
- tags that are to be added to item's tags.
-
addTags
public void addTags(String... tags)
Description copied from interface:ActiveItem
Adds tags to the item.- Specified by:
addTags
in interfaceActiveItem
- Parameters:
tags
- tags that are to be added to item's tags.
-
removeTag
public void removeTag(String tag)
Description copied from interface:ActiveItem
Removes a tag from the item.- Specified by:
removeTag
in interfaceActiveItem
- Parameters:
tag
- a tag that is to be removed from item's tags.
-
removeAllTags
public void removeAllTags()
Description copied from interface:ActiveItem
Clears all tags of this item.- Specified by:
removeAllTags
in interfaceActiveItem
-
getLabel
public @Nullable String getLabel()
Description copied from interface:Item
Returns the label of the item or null if no label is set.
-
setLabel
public void setLabel(@Nullable String label)
Description copied from interface:ActiveItem
Sets the label of an item- Specified by:
setLabel
in interfaceActiveItem
- Parameters:
label
- label (can be null)
-
getCategory
public @Nullable String getCategory()
Description copied from interface:Item
Returns the category of the item or null if no category is set.- Specified by:
getCategory
in interfaceItem
- Returns:
- category or null
-
setCategory
public void setCategory(@Nullable String category)
Description copied from interface:ActiveItem
Sets the category of the item (can be null)- Specified by:
setCategory
in interfaceActiveItem
- Parameters:
category
- category
-
getStateDescription
public @Nullable StateDescription getStateDescription()
Description copied from interface:Item
Returns the first provided state description (uses the default locale). If options are defined on the channel, they are included in the returned state description.- Specified by:
getStateDescription
in interfaceItem
- Returns:
- state description (can be null)
-
getStateDescription
public @Nullable StateDescription getStateDescription(@Nullable Locale locale)
Description copied from interface:Item
Returns the first provided state description for a given locale. If options are defined on the channel, they are included in the returned state description.- Specified by:
getStateDescription
in interfaceItem
- Parameters:
locale
- locale (can be null)- Returns:
- state description (can be null)
-
getCommandDescription
public @Nullable CommandDescription getCommandDescription(@Nullable Locale locale)
Description copied from interface:Item
Returns theCommandDescription
for the given locale. In case no dedicatedCommandDescription
is provided theStateOption
s from theStateDescription
will be served as validCommandOption
s.- Specified by:
getCommandDescription
in interfaceItem
- Parameters:
locale
- locale (can be null)- Returns:
- command description (can be null)
-
isAcceptedState
public boolean isAcceptedState(List<Class<? extends State>> acceptedDataTypes, State state)
Tests if state is within acceptedDataTypes list or a subclass of one of them- Parameters:
acceptedDataTypes
- list of datatypes this items accepts as a statestate
- to be tested- Returns:
- true if state is an acceptedDataType or subclass thereof
-
logSetTypeError
protected void logSetTypeError(State state)
-
-