Class Preconditions

java.lang.Object
au.csiro.pathling.utilities.Preconditions

public abstract class Preconditions extends Object
Static convenience methods for checking the validity of inputs.
Author:
John Grimes
  • Constructor Details

    • Preconditions

      public Preconditions()
  • Method Details

    • check

      public static void check(boolean expression)
      Ensures the truth of an expression, throwing an AssertionError with the supplied message if it does not evaluate as true.
      Parameters:
      expression - the expression that should be true
    • check

      public static void check(boolean expression, @Nonnull String messageTemplate, @Nonnull Object... params)
      Ensures the truth of an expression, throwing an AssertionError with the supplied message if it does not evaluate as true.
      Parameters:
      expression - the expression that should be true
      messageTemplate - the message template in the String.format(java.lang.String, java.lang.Object...) format
      params - the parameters to the message template
    • checkArgument

      public static void checkArgument(boolean expression, @Nonnull String errorMessage)
      Ensures the truth of an expression, throwing an IllegalArgumentException with the supplied message if it does not evaluate as true.
      Parameters:
      expression - the expression that should be true
      errorMessage - the message to use if an error is thrown
    • checkPresent

      @Nonnull public static <T> T checkPresent(@Nonnull Optional<T> object)
      Ensures that an Optional value is present, throwing a AssertionError if it is not.
      Type Parameters:
      T - the type of the object within the Optional
      Parameters:
      object - the object to check
      Returns:
      the unwrapped object
    • checkPresent

      @Nonnull public static <T> T checkPresent(@Nonnull Optional<T> object, @Nullable String message)
      Ensures that an Optional value is present, throwing a AssertionError if it is not.
      Type Parameters:
      T - the type of the object within the Optional
      Parameters:
      object - the object to check
      message - an error message
      Returns:
      the unwrapped object
    • checkUserInput

      public static void checkUserInput(boolean expression, @Nonnull String errorMessage)
      Ensures the truth of an expression, throwing an InvalidUserInputError with the supplied message if it does not evaluate as true.
      Parameters:
      expression - the expression that should be true
      errorMessage - the message to use if an error is thrown
    • checkState

      public static void checkState(boolean expression, @Nonnull String errorMessage)
      Ensures the truth of an expression, throwing an IllegalStateException with the supplied message if it does not evaluate as true.
      Parameters:
      expression - the expression that should be true
      errorMessage - the message to use if an error is thrown
    • requireNonBlank

      @Nonnull public static String requireNonBlank(@Nullable String string, @Nonnull String msg)
      Ensures that an String value is not blank, throwing a IllegalArgumentException if it is.
      Parameters:
      string - the object to check
      msg - the message to use if an error is thrown
      Returns:
      non blank string
    • checkResponse

      public static void checkResponse(boolean expression, @Nonnull String messageTemplate, @Nonnull Object... params)
      Ensures the truth of an expression, throwing an UnexpectedResponseException with the supplied formatted message if it does not evaluate as true.
      Parameters:
      expression - the expression that should be true
      messageTemplate - the message template in the String.format(java.lang.String, java.lang.Object...) format
      params - the parameters to the message template
    • wrapInUserInputError

      public static <T, R> Function<T,R> wrapInUserInputError(@Nonnull Function<T,R> func)
      Converts a function which throws a FHIRException to a function that throws an InvalidUserInputError in the same situation.
      Type Parameters:
      T - the type of the function argument.
      R - the type of the function result.
      Parameters:
      func - the function throwing FHIRException
      Returns:
      the wrapped function.