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
  • 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
    • checkArgumentNotNull

      @Contract("null -> fail; !null -> param1") @Nonnull public static <T> T checkArgumentNotNull(@Nullable T object)
      Ensures that an object is not null, throwing an IllegalArgumentException if it is null.
      Type Parameters:
      T - the type of the object
      Parameters:
      object - the object to check
      Returns:
      the object if it is not null
    • 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
    • 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.