Class ProblemCollector

  • All Implemented Interfaces:
    ProblemContainer


    public class ProblemCollector
    extends java.lang.Object
    implements ProblemContainer
    Collects problems from multiple requests to gather data.

    This has subtle differences in usage with the ValueBuilder. The builder collects information about one non-null value under construction, while this collects problems for one or more values that generally take all arguments in a constructor.

    • Method Detail

      • from

        @Nonnull
        public static ProblemCollector from​()
        Create an empty problem collector.
        Returns:
        an empty problem collector.
      • from

        @SafeVarargs
        @Nonnull
        public static ProblemCollector from​(@Nonnull
                                            java.util.List<Problem> problems,
                                            @Nonnull
                                            java.util.List<Problem>... problemLists)
        Populate this collector from lists of problems.
        Parameters:
        problems - list of problems.
        problemLists - vararg optional lists of problems.
        Returns:
        a problem collector containing all the problems.
      • from

        @Nonnull
        public static ProblemCollector from​(@Nonnull
                                            Problem problem,
                                            @Nonnull
                                            Problem... problems)
        Populate this collector from problems.
        Parameters:
        problem - problem.
        problems - vararg optional problems.
        Returns:
        a problem collector containing all the problems.
      • from

        @Nonnull
        public static ProblemCollector from​(@Nonnull
                                            RetVoid value,
                                            RetVoid... values)
        Create a new RetCollector and invoke with(RetVal, NonnullConsumer).
        Parameters:
        value - problems to include in the collection.
        values - vararg optional additional values.
        Returns:
        a new collector
      • from

        @Nonnull
        public static <T> ProblemCollector from​(@Nonnull
                                                RetVal<T> value,
                                                @Nonnull
                                                NonnullConsumer<T> setter)
        Create a new RetCollector and invoke with(RetVal, NonnullConsumer).
        Type Parameters:
        T - type of the value
        Parameters:
        value - value to pass to the setter, or problems to include in the collection.
        setter - passed the value.
        Returns:
        a new collector
      • from

        @Nonnull
        public static <T> ProblemCollector from​(@Nonnull
                                                RetNullable<T> value,
                                                @Nonnull
                                                java.util.function.Consumer<T> setter)
        Create a new RetCollector and invoke with(RetNullable, Consumer).
        Type Parameters:
        T - type of the value
        Parameters:
        value - value to pass to the setter, or problems to include in the collection.
        setter - passed the value.
        Returns:
        a new collector
      • add

        public void add​(@Nonnull
                        Problem problem)
        Adds a single problem to the collection. Reflects a Collection "add" call.
        Parameters:
        problem - problem object
        Since:
        2.1
      • addAll

        public void addAll​(@Nonnull
                           java.lang.Iterable<Problem> problems)
        Adds a single problem to the collection. Reflects a Collection "add" call.
        Parameters:
        problems - problem object
        Since:
        2.1
      • withProblem

        @Nonnull
        public ProblemCollector withProblem​(@Nonnull
                                            Problem problem,
                                            @Nonnull
                                            Problem... problems)
        Adds all the problems in the argument into this collector.
        Parameters:
        problem - a problem to add
        problems - optional additional problems.
        Returns:
        this instance
      • withProblems

        @SafeVarargs
        @Nonnull
        public final ProblemCollector withProblems​(@Nonnull
                                                   java.util.Collection<Problem> problems,
                                                   @Nonnull
                                                   java.util.Collection<Problem>... problemList)
        Adds all the problems in the argument into this collector.
        Parameters:
        problems - problem containers
        problemList - optional additional problem containers.
        Returns:
        this instance
      • with

        @Nonnull
        public <T> ProblemCollector with​(@Nonnull
                                         RetVal<T> value,
                                         @Nonnull
                                         NonnullConsumer<T> setter)
        If the value has problems, load them into this collector, otherwise pass the value as an argument to the setter.
        Type Parameters:
        T - type of the value
        Parameters:
        value - non-null value
        setter - function that handles the non-null value if it has no problems.
        Returns:
        this collector
      • with

        @Nonnull
        public <T> ProblemCollector with​(@Nonnull
                                         RetNullable<T> value,
                                         @Nonnull
                                         java.util.function.Consumer<T> setter)
        If the value has problems, load them into this collector, otherwise pass the value as an argument to the setter.
        Type Parameters:
        T - type of the value
        Parameters:
        value - nullable value
        setter - function that handles the nullable value if it has no problems.
        Returns:
        this collector
      • with

        @Nonnull
        public <T> ProblemCollector with​(@Nonnull
                                         WarningVal<T> value,
                                         @Nonnull
                                         NonnullConsumer<T> setter)
        If the value has problems, load them into this collector. Always pass the value to the setter.
        Type Parameters:
        T - type of the value
        Parameters:
        value - nullable value
        setter - function that handles the nullable value if it has no problems.
        Returns:
        this collector
        Since:
        2.1
      • with

        @Nonnull
        public ProblemCollector with​(@Nonnull
                                     RetVoid value)
        Add in any problems from the value into this collector.
        Parameters:
        value - value that may have problems.
        Returns:
        this collector.
      • withValuedProblemContainer

        @Nonnull
        public <T> ProblemCollector withValuedProblemContainer​(@Nonnull
                                                               ValuedProblemContainer<T> value,
                                                               @Nonnull
                                                               java.util.function.Consumer<T> setter)
        If the value has problems, load them into this collector, otherwise pass the value as an argument to the setter.
        Type Parameters:
        T - type of the value
        Parameters:
        value - nullable value
        setter - function that handles the nullable value if it has no problems.
        Returns:
        this collector
        Since:
        2.1
      • withAll

        @Nonnull
        public ProblemCollector withAll​(@Nonnull
                                        RetVoid... values)
        Add into this collector any problems from each of the values.
        Parameters:
        values - collection of values that may have problems.
        Returns:
        this collector.
        Since:
        2.1
      • withAll

        @Nonnull
        public ProblemCollector withAll​(@Nonnull
                                        java.lang.Iterable<RetVoid> values)
        Add into this collector any problems from each of the values.
        Parameters:
        values - collection of values that may have problems.
        Returns:
        this collector.
        Since:
        2.1
      • validateEach

        @Nonnull
        public <T> ProblemCollector validateEach​(@Nonnull
                                                 java.util.Collection<T> values,
                                                 @Nonnull
                                                 java.util.function.Function<T,ProblemContainer> validateFunc)
        For each item in the values collection, the function is called, giving it a chance to return problems associated with the value. The function may return null to indicate no problems.

        This function does not have a requirement on nullability, as that is entirely within the domain of the caller to track.

        Type Parameters:
        T - collection value type
        Parameters:
        values - collection of values to call into the validation function. This will be done for each item, irrespective of the active problem state.
        validateFunc - function called for each entry to validate.
        Returns:
        this instance.
      • then

        @Nonnull
        public <T> RetVal<T> then​(@Nonnull
                                  NonnullSupplier<RetVal<T>> getter)
        Generate a non-null value if this collector contains no problems, otherwise return the problems.
        Type Parameters:
        T - type of the value
        Parameters:
        getter - the non-null value generator
        Returns:
        the non-null value or problems
      • thenValue

        @Nonnull
        public <T> RetVal<T> thenValue​(@Nonnull
                                       NonnullSupplier<T> getter)
        Generate a non-null value if this collector contains no problems, otherwise return the problems.
        Type Parameters:
        T - type of the value
        Parameters:
        getter - the non-null value generator
        Returns:
        the non-null value or problems
      • thenNullable

        @Nonnull
        public <T> RetNullable<T> thenNullable​(@Nonnull
                                               NonnullSupplier<RetNullable<T>> getter)
        Generate a nullable value if this collector contains no problems, otherwise return the problems.
        Type Parameters:
        T - type of the value
        Parameters:
        getter - the nullable value generator
        Returns:
        the nullable value or problems
      • thenNullableValue

        @Nonnull
        public <T> RetNullable<T> thenNullableValue​(@Nonnull
                                                    java.util.function.Supplier<T> getter)
        Generate a nullable value if this collector contains no problems, otherwise return the problems.
        Type Parameters:
        T - type of the value
        Parameters:
        getter - the nullable value generator
        Returns:
        the nullable value or problems
      • thenRun

        @Nonnull
        public RetVoid thenRun​(@Nonnull
                               java.lang.Runnable runner)
        If this collector has no problems, then run the runner and return a void value. If this has problems, then return the problems.
        Parameters:
        runner - runner to execute if there are no problems.
        Returns:
        the problems, if any exist before running.
      • complete

        @Nonnull
        public <T> RetVal<T> complete​(@Nonnull
                                      T value)
        Return a non-null value, or, if the collector has problems, the problems.
        Type Parameters:
        T - type of the value
        Parameters:
        value - the returned non-null value.
        Returns:
        the problems or the value
      • completeNullable

        @Nonnull
        public <T> RetNullable<T> completeNullable​(@Nullable
                                                   T value)
        Return a nullable value, unless this collector has problems, in which case a RetNullable is returned with the problems.
        Type Parameters:
        T - type of the value
        Parameters:
        value - nullable value to include in the return value
        Returns:
        problems or the value
      • completeVoid

        @Nonnull
        public RetVoid completeVoid​()
        Return the collection of problems as a RetVoid. If there are no problems, then the returned value is ok.

        In general, it's better to use RetVoid.fromProblems(ProblemContainer, ProblemContainer...), taking this collector as the argument, but it's provided here for symmetry with the other complete methods.

        Returns:
        problems or an ok value
        Since:
        2.1
      • asWarning

        @Nonnull
        public <T> WarningVal<T> asWarning​(@Nonnull
                                           T value)
        Convert this collection to a warning, containing all this collector's problems along with the argument value.
        Type Parameters:
        T - type of the value.
        Parameters:
        value - value to include in the warning.
        Returns:
        the warning with this collector's problems and the value.
      • isProblem

        public boolean isProblem​()
        Description copied from interface: ProblemContainer
        Returns whether this object contains 1 or more problems.
        Specified by:
        isProblem in interface ProblemContainer
        Returns:
        true if there is a problem, false if there is no problem.
      • isOk

        public boolean isOk​()
        Description copied from interface: ProblemContainer
        Returns whether this object contains zero problems.
        Specified by:
        isOk in interface ProblemContainer
        Returns:
        true if there are no problems, false if there is a problem.
      • anyProblems

        @Nonnull
        public java.util.Collection<Problem> anyProblems​()
        Description copied from interface: ProblemContainer
        Return all the problems in this object, even if the container is "ok".

        Generally, this combines the problems in this instance with a larger collection, which can itself be used to check if any of the values had problems.

        The returned collection is read-only, and contains no null values.

        Specified by:
        anyProblems in interface ProblemContainer
        Returns:
        the problems contained in this container, even if there are none.
      • validProblems

        @Nonnull
        public java.util.Collection<Problem> validProblems​()
        Description copied from interface: ProblemContainer
        Returns all contained problems in this object, but only if this object contains 1 or more problems. If it contains 0, then this throws an IllegalStateException.

        The returned collection is read-only, and contains no null values.

        Specified by:
        validProblems in interface ProblemContainer
        Returns:
        the problems in this container, and the collection will contain at least 1 item.
      • debugProblems

        @Nonnull
        public java.lang.String debugProblems​(@Nonnull
                                              java.lang.String joinedWith)
        Description copied from interface: ProblemContainer
        Return the problems as a single string, which combines the Object.toString() output of each problem with the given joining string parameter. If this object contains no problems, then an empty string is returned instead.
        Specified by:
        debugProblems in interface ProblemContainer
        Parameters:
        joinedWith - the text to join multiple problem strings together.
        Returns:
        the combined text of the problems, or an empty string if there are no problems.
      • joinProblemsWith

        public void joinProblemsWith​(@Nonnull
                                     java.util.Collection<Problem> problemList)
        Description copied from interface: ProblemContainer
        Add all problems in this container into the argument. This has a very specific usage to indicate that this container, even if it has no problems, is part of a bigger issue. Therefore, this is fine to call even if there are no problems in this container.
        Specified by:
        joinProblemsWith in interface ProblemContainer
        Parameters:
        problemList - a modifiable collection of zero or more problems.