Class TroubleToggle
java.lang.Object
com.kosdev.kos.commons.core.service.trouble.TroubleToggle
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface to create a new instance of a trouble. -
Constructor Summary
ConstructorsConstructorDescriptionTroubleToggle(TroubleService service, TroubleToggle.TroubleFactory factory) Create a newTroubleTogglethat will use the specified factory to create a new trouble instance when required. -
Method Summary
-
Constructor Details
-
TroubleToggle
Create a newTroubleTogglethat 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 toTroubleService. 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 callsadd()and false callsremove(). 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
-