Class Ret



  • public class Ret
    extends java.lang.Object
    Utility class for working with collections of return values.
    • Field Detail

      • NO_PROBLEMS

        public static final java.util.List<Problem> NO_PROBLEMS
    • Method Detail

      • accumulateValues

        @Nonnull
        public static <T> ValueAccumulator<T> accumulateValues​()
        Convenience function to create a ValueAccumulator.
        Type Parameters:
        T - type of the accumulator
        Returns:
        a value accumulator
      • collectProblems

        @Nonnull
        public static ProblemCollector collectProblems​()
        Convenience function to create a ProblemCollector.
        Returns:
        a new problem collector.
      • buildValue

        @Nonnull
        public static <T> ValueBuilder<T> buildValue​(@Nonnull
                                                     T value)
        Convenience function to create a WarningVal instance for the given value.
        Type Parameters:
        T - type of the value
        Parameters:
        value - value used to initialize the return value
        Returns:
        an instance that collects problems associated with the value.
      • joinProblemSets

        @SafeVarargs
        @Nonnull
        public static java.util.List<Problem> joinProblemSets​(@Nullable
                                                              java.util.Collection<Problem> problemSet,
                                                              @Nonnull
                                                              java.util.Collection<Problem>... problemSets)
        Join the array of lists of problems into a single list.
        Parameters:
        problemSet - collection of problems
        problemSets - varargs of optional collections of problems.
        Returns:
        all the problems combined into a single, immutable list, with no null values.
      • joinRetProblemSets

        @SafeVarargs
        @Nonnull
        public static java.util.List<Problem> joinRetProblemSets​(@Nullable
                                                                 java.util.Collection<ProblemContainer> retSet,
                                                                 java.util.Collection<ProblemContainer>... retSets)
        Joins problems in collections of ProblemContainer instances.
        Parameters:
        retSet - collection of Ret values.
        retSets - vararg of optional collections of Ret values.
        Returns:
        all the problems in a single, immutable list.
      • joinRetProblems

        @Nonnull
        public static java.util.List<Problem> joinRetProblems​(@Nullable
                                                              ProblemContainer ret,
                                                              ProblemContainer... rets)
        Joins ProblemContainer instances into a list of problems.
        Parameters:
        ret - Ret value.
        rets - vararg of optional Ret values.
        Returns:
        all the problems in a single, immutable list.
      • joinProblems

        @Nonnull
        public static java.util.List<Problem> joinProblems​(@Nullable
                                                           Problem problem,
                                                           Problem... problems)
        Joins problems into a list of problems. This requires at least one problem.
        Parameters:
        problem - the first problem.
        problems - vararg of optional problem values.
        Returns:
        all the problems in a single, immutable list, with no null values.
      • enforceNoProblems

        public static void enforceNoProblems​(@Nonnull
                                             java.util.Collection<Problem> problems)
        Enforce that the list of problems is empty by throwing an IllegalStateException if it contains problems.
        Parameters:
        problems - list of problems to check, which must contain non-null values.
        Throws:
        java.lang.IllegalStateException - if the problem list is not empty.
      • enforceHasProblems

        @Nonnull
        public static java.util.Collection<Problem> enforceHasProblems​(@Nonnull
                                                                       java.util.Collection<Problem> problems)
        Enforce that the list of problems is not empty by throwing an IllegalStateException if it does not contain problems.
        Parameters:
        problems - list of problems to check, which must contain non-null values.
        Returns:
        the argument
        Throws:
        java.lang.IllegalStateException - if the problem list is not empty.
      • joinProblemMessages

        @Nonnull
        public static java.lang.String joinProblemMessages​(@Nonnull
                                                           java.lang.String joinText,
                                                           @Nonnull
                                                           java.util.Collection<Problem> problems)
        Join the problem's Problem.localMessage() text into a single string, joined by the joinText. If the problem list is empty, then an empty string is returned.
        Parameters:
        joinText - text used to join together the problems.
        problems - list of problems to join together.
        Returns:
        the joined text, or an empty string if the list of problems is empty.
      • closeWith

        @WillClose
        @Nonnull
        public static <T extends java.lang.AutoCloseable,R> RetVal<R> closeWith​(@Nonnull
                                                                                T value,
                                                                                @Nonnull
                                                                                NonnullThrowsFunction<T,RetVal<R>> func)
        Runs a function with a closable value. When the function completes execution, the closable value is closed. If either the function or the close action causes an exception, that is wrapped in a problem and returned. Note that a problem is returned for any kind of exception, not just expected ones; if the function is expected to throw an IOException, but a NullPointerException is thrown, that is still returned as a problem.
        Type Parameters:
        T - type of the value passed to the function
        R - return type
        Parameters:
        value - value passed to the function. This will be closed after the function is called.
        func - function to run
        Returns:
        the function's return value, or the exception wrapped in a problem.
      • closeWithNullable

        @WillClose
        @Nonnull
        public static <T extends java.lang.AutoCloseable,R> RetNullable<R> closeWithNullable​(@Nonnull
                                                                                             T value,
                                                                                             @Nonnull
                                                                                             NonnullThrowsFunction<T,RetNullable<R>> func)
        Runs a function with a closable value. When the function completes execution, the closable value is closed. If either the function or the close action causes an exception, that is wrapped in a problem and returned. Note that a problem is returned for any kind of exception, not just expected ones; if the function is expected to throw an IOException, but a NullPointerException is thrown, that is still returned as a problem.
        Type Parameters:
        T - type of the value passed to the function
        R - return type
        Parameters:
        value - value passed to the function. This will be closed after the function is called.
        func - function to run
        Returns:
        the function's return value, or the exception wrapped in a problem.
      • closeWithVoid

        @WillClose
        @Nonnull
        public static <T extends java.lang.AutoCloseable> RetVoid closeWithVoid​(@Nonnull
                                                                                T value,
                                                                                @Nonnull
                                                                                NonnullThrowsFunction<T,RetVoid> func)
        Runs a function with a closable value. When the function completes execution, the closable value is closed. If either the function or the close action causes an exception, that is wrapped in a problem and returned. Note that a problem is returned for any kind of exception, not just expected ones; if the function is expected to throw an IOException, but a NullPointerException is thrown, that is still returned as a problem.
        Type Parameters:
        T - type of the value passed to the function
        Parameters:
        value - value passed to the function. This will be closed after the function is called.
        func - function to run
        Returns:
        the function's return value, or the exception wrapped in a problem.