java.lang.Object
com.blackhillsoftware.smf.Utils

public class Utils extends Object
Data manipulation utility class
  • Constructor Details

    • Utils

      public Utils()
  • Method Details

    • divideDurations

      public static double divideDurations(Duration d1, Duration d2)
      Divide 2 durations to show one duration as a proportion of another
       
       double result = Utils.divideDurations(Duration.ofSeconds(5), Duration.ofSeconds(10))
       
       
      result = 0.5
      Parameters:
      d1 - Duration 1
      d2 - Duration 2 the divisor
      Returns:
      double d1 divided by d2
    • ToSeconds

      public static double ToSeconds(Duration d)
      Convert a Duration into whole and fractional seconds
      Parameters:
      d - Duration
      Returns:
      double the duration in seconds
    • oneOrNull

      public static <T> T oneOrNull(List<T> list)
      Return the item from a single item list, or null if the list is empty. Throw an exception if there is more than one item in the list.

      SMF has many instances of triplets that specify the number of sections present. in most cases this API provides the sections in a List<SectionType>. In some cases there can be a maximum of one section and the number in the triplet simply indicates whether it is present or not. This is not always clearly documented in the SMF documentation.

      This method is a convenience method that performs 2 functions:

      • Returns a single item or null, so you don't need to check if the item is present and then get the item.
      • Throws an IllegalArgumentException if there is more than one item in the list. If you are assuming that there can be a maximum of one item, more than one item almost certainly indicates a logic error. Throwing the exception allows the error to be discovered instead of going undetected.
      Parameters:
      list - A list expected to have 1 or 0 members
      Returns:
      The single member in the list, or null
      Throws:
      IllegalArgumentException - if there is more than 1 item in the list