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, Jan N. Klug - Added time series support
  • Field Details

  • Constructor Details

    • GenericItem

      public GenericItem(String type, String name)
  • Method Details

    • getState

      public State getState()
      Description copied from interface: Item
      returns the current state of the item
      Specified by:
      getState in interface Item
      Returns:
      the current state
    • 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 interface Item
      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 interface Identifiable<String>
      Returns:
      the unique identifier
    • getName

      public String getName()
      Description copied from interface: Item
      returns the name of the item
      Specified by:
      getName in interface Item
      Returns:
      the name of the item
    • getType

      public String getType()
      Description copied from interface: Item
      returns the item type as defined by ItemFactorys
      Specified by:
      getType in interface Item
      Returns:
      the item type
    • 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 interface Item
      Returns:
      list of item group names
    • addGroupName

      public void addGroupName(String groupItemName)
      Adds a group name to the GenericItem.
      Specified by:
      addGroupName in interface ActiveItem
      Parameters:
      groupItemName - group item name to add
      Throws:
      IllegalArgumentException - if groupItemName is null
    • addGroupNames

      public void addGroupNames(String... groupItemNames)
      Description copied from interface: ActiveItem
      Assigns the according item to the given groups.
      Specified by:
      addGroupNames in interface ActiveItem
      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 interface ActiveItem
      Parameters:
      groupItemNames - names of the groups (must not be null)
    • removeGroupName

      public void removeGroupName(String groupItemName)
      Removes a group item name from the GenericItem.
      Specified by:
      removeGroupName in interface ActiveItem
      Parameters:
      groupItemName - group item name to remove
      Throws:
      IllegalArgumentException - if groupItemName is null
    • 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)
    • 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 the setState(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
    • setTimeSeries

      public void setTimeSeries(TimeSeries timeSeries)
      Set a new time series.

      Subclasses may override this method in order to do necessary conversions upfront. Afterwards, applyTimeSeries(TimeSeries) should be called by classes overriding this method.

      A time series may only contain events that are compatible with the item's internal state.

      Parameters:
      timeSeries - new time series of this item
    • applyTimeSeries

      protected final void applyTimeSeries(TimeSeries timeSeries)
      Sets new time series, notifies listeners and sends events.

      Classes overriding the setTimeSeries(TimeSeries) method should call this method in order to actually set the time series, inform listeners and send the event.

      A time series may only contain events that are compatible with the item's internal state.

      Parameters:
      timeSeries - new time series of this item
    • send

      public void send(RefreshType command)
    • notifyListeners

      protected void notifyListeners(State oldState, State newState)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • addStateChangeListener

      public void addStateChangeListener(StateChangeListener listener)
    • removeStateChangeListener

      public void removeStateChangeListener(StateChangeListener listener)
    • addTimeSeriesListener

      public void addTimeSeriesListener(TimeSeriesListener listener)
    • removeTimeSeriesListener

      public void removeTimeSeriesListener(TimeSeriesListener listener)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(@Nullable Object obj)
      Overrides:
      equals in class Object
    • 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.
      Specified by:
      getTags in interface Item
      Returns:
      set of tags.
    • hasTag

      public boolean hasTag(String tag)
      Description copied from interface: Item
      Returns true if the item's tags contains the specific tag, otherwise false.
      Specified by:
      hasTag in interface Item
      Parameters:
      tag - a tag whose presence in the item's tags is to be tested.
      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 interface ActiveItem
      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 interface ActiveItem
      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 interface ActiveItem
      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 interface ActiveItem
      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 interface ActiveItem
    • getLabel

      public @Nullable String getLabel()
      Description copied from interface: Item
      Returns the label of the item or null if no label is set.
      Specified by:
      getLabel in interface Item
      Returns:
      item label or null
    • setLabel

      public void setLabel(@Nullable String label)
      Description copied from interface: ActiveItem
      Sets the label of an item
      Specified by:
      setLabel in interface ActiveItem
      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 interface Item
      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 interface ActiveItem
      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 interface Item
      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 interface Item
      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 the CommandDescription for the given locale. In case no dedicated CommandDescription is provided the StateOptions from the StateDescription will be served as valid CommandOptions.
      Specified by:
      getCommandDescription in interface Item
      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 state
      state - to be tested
      Returns:
      true if state is an acceptedDataType or subclass thereof
    • logSetTypeError

      protected void logSetTypeError(State state)
    • logSetTypeError

      protected void logSetTypeError(TimeSeries timeSeries)
    • getCommandOptions

      protected @Nullable CommandDescription getCommandOptions(@Nullable Locale locale)