Class StateBean

java.lang.Object
com.tccc.kos.commons.core.service.state.StateBean
All Implemented Interfaces:
HandleAware, Cloneable

public abstract class StateBean extends Object implements Cloneable, HandleAware
Base class for beans that hold state and can be broadcast via message broker when they change. This is typically a bean that holds specific state values that can be changed by hardware or control logic and needs to be replicated in an internal system such as a UI. For example, a carb tank may have states such as whether it's filling or not, high and low probe states and so on. This data may be broadcast via hardware and merged into this bean, which can then be broadcast to listeners.

The StateService is used to broadcast changes to the bean. It will only send the bean if it has changed from the previous broadcast. This is determined by cloning the bean and then comparing the new version to the previously cloned bean and only broadcasting / updating if it is not equal. This allows equality to determine what triggers the broadcast. For example, it's possible to have a state bean with data that does not trigger a broadcast, but will be included when equality related changes are made.

In general, StateBean instances must be registered with StateService. This allows clients to query the current state of beans before any changes have been made. Any StateBean instances placed in the BeanContext will automatically be registered. This also means it's possible to use BeanContext.connect() and BeanContext.disconnect() to manage registrations or simply register them manually. Once registered, the broadcast() method can be used to trigger the service to check for changes in the bean and send any changes.

Since:
1.0
Version:
2023-10-07
  • Constructor Details

    • StateBean

      public StateBean()
    • StateBean

      public StateBean(Handle handle)
  • Method Details

    • getPath

      public String getPath()
      Return the path of the bean. This will be used as part of the topic used to broadcast changes in bean state via the message broker. It should be a MessageBroker topic friendly string. If the bean is associated with a service or something that has a HandlePath then this can simply return the path of the associated object.

      This must be a unique string relative to all other state beans as the MessageBroker topic used to send state changes is the same base topic for all beans, with just the final segment of the topic containing this string.

      This string must not change over time.

      Specified by:
      getPath in interface HandleAware
      Returns:
      this bean's handle path
    • broadcast

      public boolean broadcast()
      Causes the bean to be examined for changes relative to the last broadcast attempt and if any changes are found the bean will be sent via the MessageBroker .
      Returns:
      true if the bean has changed
    • getView

      public Class<?> getView()
      Returns the optional view class to use when converting this bean to json before being broadcast.
    • setView

      public void setView(Class<?> view)
      Set the optional view class to use when converting this bean to json before being broadcast. This allows various parts of the bean to be filtered out when sending.
    • addListener

      public void addListener(StateBeanListener listener)
      Add a listener to the list.
      Parameters:
      listener - the listener to add
    • removeListener

      public void removeListener(StateBeanListener listener)
      Remove a listener from the list.
      Parameters:
      listener - the listener to remove
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

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

      public Handle getHandle()
      Description copied from interface: HandleAware
      Returns the handle for this bean. This is used by the context to compute the opaque handle for the object.
      Specified by:
      getHandle in interface HandleAware
      Returns:
      the handle node for the bean