Class TroubleToggle

java.lang.Object
com.kosdev.kos.commons.core.service.trouble.TroubleToggle

public class TroubleToggle extends Object
Convenience class that adds and removes a single instance of a trouble from TroubleService. When add() is called, if the specified trouble hasn't been added, one will be created and added. If a trouble has already been added, calling add() will do nothing. Similarly, if remove() is called, any previously added trouble will be removed. If no trouble has been added, remove() does nothing.

This is designed to simplify the common pattern where a particular condition requires a trouble to be added and when that condition is not true, the trouble is removed. This eliminates the boilerplate code of adding and removing, avoiding duplicate add calls, and so on.

Since:
9
Version:
9
  • Constructor Details

    • TroubleToggle

      public TroubleToggle(TroubleService service, TroubleToggle.TroubleFactory factory)
      Create a new TroubleToggle that will use the specified factory to create a new trouble instance when required.
      Parameters:
      factory - factory to create new trouble instances
      Since:
      9
  • Method Details

    • add

      public void add()
      Add a trouble. If the trouble doesn't exist yet, it will be created using the associated factory and added to TroubleService . If the trouble already exists, this will do nothing.
      Since:
      9
    • remove

      public void remove()
      Remove the trouble if it exists.
      Since:
      9
    • toggle

      public void toggle(boolean state)
      Toggle the trouble if needed based on the value. A value of true simply calls add() and false calls remove() . Just a convenience method if add and remove are driven by a single condition.
      Parameters:
      state - if true, call add, otherwise call remove
    • hasTrouble

      public boolean hasTrouble()
      Return true if a trouble is currently active.
      Since:
      9