Class Preconditions


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

      • Preconditions

        public Preconditions()
    • Method Detail

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

        @Nonnull
        public static <T> T checkNotNull​(@Nullable
                                         T object)
        Ensures that an object is not null, throwing a NullPointerException if it is.
        Type Parameters:
        T - the type of the object
        Parameters:
        object - the object to check
        Returns:
        the non-null object
      • 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
      • 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.