java.lang.Object
com.thecoderscorner.menu.domain.util.MenuItemHelper

public class MenuItemHelper extends Object
A helper class for dealing with MenuItem objects. This class provides helpers for visiting menu items and returning a result. It also provides other helpers for dealing with items.
  • Constructor Details

    • MenuItemHelper

      public MenuItemHelper()
  • Method Details

    • visitWithResult

      public static <T> Optional<T> visitWithResult(MenuItem item, AbstractMenuItemVisitor<T> visitor)
      Visits a menu item calling the appropriate function for the type and collects the result that is set by calling your visitor's `setResult` method.
      Type Parameters:
      T - the return type
      Parameters:
      item - the item to be visited
      visitor - the visitor that will be used
      Returns:
      an optional of the return type, set to empty unless setResult was called.
    • asSubMenu

      public static SubMenuItem asSubMenu(MenuItem item)
      Returns the menu item as a sub menu or null
      Parameters:
      item - the possible sub menu
      Returns:
      the sub menu, or null.
    • isRuntimeStructureNeeded

      public static boolean isRuntimeStructureNeeded(MenuItem item)
      Check if the item is based on a runtime item
      Parameters:
      item - the item to check
      Returns:
      true if runtime based, otherwise false.
    • builderWithExisting

      public static MenuItemBuilder builderWithExisting(MenuItem item)
    • createFromExistingWithId

      public static MenuItem createFromExistingWithId(MenuItem selected, int newId)
      creates a copy of the menu item chosen, with the ID changed to newId
      Parameters:
      selected - the item to copy
      newId - the ID for the copy
      Returns:
      the newly created item
    • eepromSizeForItem

      public static int eepromSizeForItem(MenuItem item)
      Gets the size of the eeprom storage for a given element type
      Parameters:
      item - the item to determine eeprom size for
      Returns:
      the eeprom storage needed.
    • stateForMenuItem

      public static AnyMenuState stateForMenuItem(AnyMenuState existingState, MenuItem item, Object val)
      Get a new state object based on an existing state with a new value keeping all exising other values
      Parameters:
      existingState - the existing state object
      item - the item
      val - the value
      Returns:
      a new menu state object based on the parameters
    • stateForMenuItem

      public static AnyMenuState stateForMenuItem(AnyMenuState existingState, MenuItem item, Object val, boolean changed)
      Get the state for an existing state with a new value, changing the changed state
      Parameters:
      existingState - the existing state object
      item - the item
      val - the changed value
      changed - the new change status
      Returns:
      a new state object based on the parameters
    • applyIncrementalValueChange

      public static Optional<AnyMenuState> applyIncrementalValueChange(MenuItem item, int delta, MenuTree tree)
      Try and apply an incremental delta value update to a menu tree. This works for integer, enum and scroll items, it loads the existing value and tries to apply the delta offset, if the min/max would not be exceeded.
      Parameters:
      item - the item to change
      delta - the delta amount
      tree - the tree the item belongs to
      Returns:
      a new item if the operation was possible, otherwise empty
    • stateForMenuItem

      public static AnyMenuState stateForMenuItem(MenuItem item, Object v, boolean changed, boolean active)
      Create a menu state for a given item with a value update. We try pretty hard to convert whatever comes in for the value into a new state.
      Parameters:
      item - the item to create the state for
      v - the value
      changed - the changed status
      active - the active status
      Returns:
      the new menu state
    • setMenuState

      public static void setMenuState(MenuItem item, Object value, MenuTree tree)
      Set the state in the tree for an item with a new value, setting it changed if it genuinely has.
      Parameters:
      item - the item
      value - the replacement value
      tree - the tree to change
    • getValueFor

      public static <T> T getValueFor(MenuItem item, MenuTree tree, T def)
      Gets the value from the tree or the default provided
      Type Parameters:
      T - the type is inferred from the default parameter
      Parameters:
      item - the item
      tree - the tree to lookup in
      def - the default item (getDefaultFor can get the default automatically)
      Returns:
      the item looked up, or the default.
    • getBootMsgForItem

      public static Optional<BootItemMenuCommand<?,?>> getBootMsgForItem(MenuItem item, SubMenuItem parent, MenuTree tree)
      Can be used during boot sequences to get a suitable boot item for a menu item
      Parameters:
      item - the item
      parent - the parent
      tree - the tree it belongs to
      Returns:
      either a boot item or empty
    • getValueFor

      public static Object getValueFor(MenuItem item, MenuTree tree)
      This gets the value from the tree state, if it is not available calls getDefaultValue Same as getValueFor(item, tree, defVal) but this just calls getDefaultFor(..) to get the default.
      Parameters:
      item - the item to get the state of
      tree - the tree holding the state
      Returns:
      the items current value, or the default.
    • getDefaultFor

      public static Object getDefaultFor(MenuItem item)
      Gets the default item value for a menu item, such that the value could be used in call to set state.
      Parameters:
      item - the item
      Returns:
      the default value