Class ResultElement

java.lang.Object
com.amalgamasimulation.studies.result.ResultElement
Direct Known Subclasses:
Result, ResultDateTimeValue, ResultDoubleValue, ResultGroup, ResultPiecewiseFunctionValue, ResultStringValue

public abstract class ResultElement extends Object
An abstract base class representing an element in a hierarchical result structure. Provides functionality for building and navigating a tree of result elements, including values, groups, and other specialized result types.

This class maintains parent-child relationships and provides methods for element creation, hierarchical navigation and searching.

  • Constructor Details

    • ResultElement

      public ResultElement()
  • Method Details

    • id

      public ResultElement id(String id)
      Sets the identifier for this element. The ID must be unique among siblings.
      Parameters:
      id - the unique identifier for this element
      Returns:
      this element for method chaining
      Throws:
      IllegalStateException - if a sibling already exists with the same ID
    • id

      public ResultElement id(String id, boolean resolveDuplicates)
      Sets the identifier for this element. The ID must be unique among siblings.
      Parameters:
      id - the unique identifier for this element
      resolveDuplicates - if true, if duplicates are found, "(n)" will be added to the ID
      Returns:
      this element for method chaining
      Throws:
      IllegalStateException - if a sibling already exists with the same ID
    • hasSiblingWithSameId

      public boolean hasSiblingWithSameId(String id)
      Checks whether any sibling element has the same ID as the given one.
      Parameters:
      id - The ID to check for duplicates among siblings.
      Returns:
      true if at least one sibling has the same ID, false otherwise. Returns false if the parent is not present (i.e., the element is a root).
    • id

      public String id()
      Gets the identifier of this element.
      Returns:
      the element's identifier
    • path

      public ResultPath path()
      Gets the hierarchical path to this element from the root. The path is constructed by joining all parent IDs with the delimiter.
      Returns:
      the path to this element
    • parent

      public Optional<ResultElement> parent()
      Gets the parent element of this element, if it exists.
      Returns:
      an Optional containing the parent element, or empty if this is the root
    • children

      public List<ResultElement> children()
      Gets an unmodifiable view of the direct children of this element.
      Returns:
      the list of child elements
    • descendants

      public List<ResultElement> descendants()
      Gets all descendants of this element (children, grandchildren, etc.).
      Returns:
      a list of all descendant elements
    • element

      public ResultDoubleValue element(double value)
      Creates and adds a new double value element as a child of this element.
      Parameters:
      value - the double value
      Returns:
      the newly created ResultDoubleValue
    • element

      public ResultDoubleValue element(String id, double value, DoubleFormats format)
      Creates and adds a new double value element with specified ID, value, and format.
      Parameters:
      id - the element ID
      value - the double value
      format - the display format for the value
      Returns:
      the newly created ResultDoubleValue
    • element

      public ResultStringValue element(String id, String value)
      Creates and adds a new string value element with specified ID and value.
      Parameters:
      id - the element ID
      value - the string value
      Returns:
      the newly created ResultStringValue
    • element

      public ResultDoubleValue element(String id, double value)
      Creates and adds a new double value element with specified ID and value.
      Parameters:
      id - the element ID
      value - the double value
      Returns:
      the newly created ResultDoubleValue
    • element

      public ResultDoubleValue element(String id, Supplier<Double> valueSupplier, DoubleFormats format)
      Creates and adds a new double value element with specified ID, value supplier, and format.
      Parameters:
      id - the element ID
      valueSupplier - a supplier for the double value
      format - the display format for the value
      Returns:
      the newly created ResultDoubleValue
    • element

      Creates and adds a new piecewise function value element with specified ID and value.
      Parameters:
      id - the element ID
      value - the piecewise function value
      Returns:
      the newly created ResultPiecewiseFunctionValue
    • element

      public ResultDoubleValue element(Supplier<Double> valueSupplier)
      Creates and adds a new double value element with a value supplier.
      Parameters:
      valueSupplier - a supplier for the double value
      Returns:
      the newly created ResultDoubleValue
    • element

      Creates and adds a new piecewise function value element.
      Parameters:
      value - the piecewise function value
      Returns:
      the newly created ResultPiecewiseFunctionValue
    • element

      public ResultStringValue element(String value)
      Creates and adds a new string value element.
      Parameters:
      value - the string value
      Returns:
      the newly created ResultStringValue
    • element

      public ResultDateTimeValue element(LocalDateTime value)
      Creates and adds a new date-time value element.
      Parameters:
      value - the date-time value
      Returns:
      the newly created ResultDateTimeValue
    • group

      public ResultGroup group(String id, boolean resolveDuplicates)
      Creates and adds a new group element with the specified ID.
      Parameters:
      id - the group ID
      resolveDuplicates - if true, if duplicates are found, "(n)" will be added to the ID
      Returns:
      the newly created ResultGroup
    • group

      public ResultGroup group(String id)
      Creates and adds a new group element with the specified ID.
      Parameters:
      id - the group ID
      Returns:
      the newly created ResultGroup
    • summaryResultGroup

      public SummaryResultGroup summaryResultGroup(String id, DoubleFormats format)
      Creates and adds a new summary group element with the specified ID and format.
      Parameters:
      id - the group ID
      format - the display format for the value
      Returns:
      the newly created SummaryResultGroup
    • summaryResultGroup

      public SummaryResultGroup summaryResultGroup(String id)
      Creates and adds a new summary group element with the specified ID.
      Parameters:
      id - the group ID
      Returns:
      the newly created SummaryResultGroup
    • find

      public Optional<ResultElement> find(ResultPath path)
      Finds an element by its path relative to this element.
      Parameters:
      path - the path to the element
      Returns:
      an Optional containing the found element, or empty if not found
    • findOrThrow

      public ResultElement findOrThrow(ResultPath path)
      Finds an element by path or throws an exception if not found.
      Parameters:
      path - the path to the element
      Returns:
      the found element
      Throws:
      NoSuchElementException - if the element is not found
    • findOrThrow

      public ResultElement findOrThrow(String... path)
      Finds an element by path (specified as varargs) or throws an exception if not found.
      Parameters:
      path - the path elements
      Returns:
      the found element
      Throws:
      NoSuchElementException - if the element is not found
    • getOrThrow

      public double getOrThrow(String... pathElements)
      Gets a double value by path or throws an exception if not found or not a double value.
      Parameters:
      pathElements - the path elements as varargs
      Returns:
      the double value
      Throws:
      NoSuchElementException - if the element is not found or not a double value
    • getOrThrow

      public double getOrThrow(ResultPath path)
      Gets a double value by path or throws an exception if not found or not a double value.
      Parameters:
      path - the path to the element
      Returns:
      the double value
      Throws:
      NoSuchElementException - if the element is not found or not a double value
    • getFunctionOrThrow

      public PiecewiseFunction getFunctionOrThrow(ResultPath path)
      Gets a piecewise function by path or throws an exception if not found or not a function value.
      Parameters:
      path - the path to the element
      Returns:
      the piecewise function
      Throws:
      NoSuchElementException - if the element is not found or not a function value
    • getFunctionOrDefault

      public PiecewiseFunction getFunctionOrDefault(ResultPath path, PiecewiseFunction defaultValue)
      Gets a piecewise function by path or returns a default value if not found or not a function value.
      Parameters:
      path - the path to the element
      defaultValue - the default value to return if not found
      Returns:
      the piecewise function or default value
    • getOrDefault

      public double getOrDefault(ResultPath path, double defaultValue)
      Gets a double value by path or returns a default value if not found or not a double value.
      Parameters:
      path - the path to the element
      defaultValue - the default value to return if not found
      Returns:
      the double value or default value
    • getStringOrThrow

      public String getStringOrThrow(String... pathElements)
      Gets a string value by path or throws an exception if not found or not a string value.
      Parameters:
      pathElements - the path elements as varargs
      Returns:
      the string value
      Throws:
      NoSuchElementException - if the element is not found or not a string value
    • getStringOrThrow

      public String getStringOrThrow(ResultPath path)
      Gets a string value by path or throws an exception if not found or not a string value.
      Parameters:
      path - the path to the element
      Returns:
      the string value
      Throws:
      NoSuchElementException - if the element is not found or not a string value
    • getGroupOrThrow

      public ResultGroup getGroupOrThrow(ResultPath path)
      Gets a group element by path or throws an exception if not found or not a group.
      Parameters:
      path - the path to the element
      Returns:
      the group element
      Throws:
      NoSuchElementException - if the element is not found or not a group
    • getDateTimeOrThrow

      public LocalDateTime getDateTimeOrThrow(String... pathElements)
      Gets a date-time value by path or throws an exception if not found or not a date-time value.
      Parameters:
      pathElements - the path elements as varargs
      Returns:
      the date-time value
      Throws:
      NoSuchElementException - if the element is not found or not a date-time value
    • getDateTimeOrThrow

      public LocalDateTime getDateTimeOrThrow(ResultPath path)
      Gets a date-time value by path or throws an exception if not found or not a date-time value.
      Parameters:
      path - the path to the element
      Returns:
      the date-time value
      Throws:
      NoSuchElementException - if the element is not found or not a date-time value
    • root

      public ResultElement root()
      Gets the root element of the hierarchy containing this element.
      Returns:
      the root element
    • setParent

      protected void setParent(ResultElement parent)
    • getChildrenDoubleResults

      public List<ResultDoubleValue> getChildrenDoubleResults(String... path)
      Gets all child double value elements under the specified path.
      Parameters:
      path - the path to the parent element
      Returns:
      a list of child double values
    • getChildrenDoubleResults

      public List<ResultDoubleValue> getChildrenDoubleResults(ResultPath path)
      Gets all child double value elements under the specified path.
      Parameters:
      path - the path to the parent element
      Returns:
      a list of child double values
    • getChildrenGroups

      public List<ResultGroup> getChildrenGroups(String... path)
      Gets all child group elements under the specified path.
      Parameters:
      path - the path elements
      Returns:
      a list of child groups
    • getChildrenGroups

      public List<ResultGroup> getChildrenGroups(ResultPath path)
      Gets all child group elements under the specified path.
      Parameters:
      path - the path to the parent element
      Returns:
      a list of child groups
    • tags

      public Set<String> tags()
      Gets an unmodifiable view of the tags associated with this element.
      Returns:
      the set of tags
    • findByTags

      public List<ResultElement> findByTags(String... byTags)
      Finds all elements in the hierarchy that have all the specified tags.
      Parameters:
      byTags - the tags to match
      Returns:
      a list of matching elements
    • tag

      public ResultElement tag(String tag)
      Adds a tag to this element.
      Parameters:
      tag - the tag to add
      Returns:
      this element for method chaining