Class ProblemCollector
- java.lang.Object
-
- net.groboclown.retval.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 Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(Problem problem)Adds a single problem to the collection.voidaddAll(java.lang.Iterable<Problem> problems)Adds a single problem to the collection.java.util.Collection<Problem>anyProblems()Return all the problems in this object, even if the container is "ok".<T> WarningVal<T>asWarning(T value)Convert this collection to a warning, containing all this collector's problems along with the argument value.<T> RetVal<T>complete(T value)Return a non-null value, or, if the collector has problems, the problems.<T> RetNullable<T>completeNullable(T value)Return a nullable value, unless this collector has problems, in which case aRetNullableis returned with the problems.RetVoidcompleteVoid()Return the collection of problems as aRetVoid.java.lang.StringdebugProblems(java.lang.String joinedWith)Return the problems as a single string, which combines theObject.toString()output of each problem with the given joining string parameter.static ProblemCollectorfrom()Create an empty problem collector.static ProblemCollectorfrom(java.util.List<Problem> problems, java.util.List<Problem>... problemLists)Populate this collector from lists of problems.static ProblemCollectorfrom(Problem problem, Problem... problems)Populate this collector from problems.static <T> ProblemCollectorfrom(RetNullable<T> value, java.util.function.Consumer<T> setter)Create a new RetCollector and invokewith(RetNullable, Consumer).static <T> ProblemCollectorfrom(RetVal<T> value, NonnullConsumer<T> setter)Create a new RetCollector and invokewith(RetVal, NonnullConsumer).static ProblemCollectorfrom(RetVoid value, RetVoid... values)Create a new RetCollector and invokewith(RetVal, NonnullConsumer).booleanhasProblems()Returns whether this object contains 1 or more problems.booleanisOk()Returns whether this object contains zero problems.booleanisProblem()Returns whether this object contains 1 or more problems.voidjoinProblemsWith(java.util.Collection<Problem> problemList)Add all problems in this container into the argument.<T> RetVal<T>then(NonnullSupplier<RetVal<T>> getter)Generate a non-null value if this collector contains no problems, otherwise return the problems.<T> RetNullable<T>thenNullable(NonnullSupplier<RetNullable<T>> getter)Generate a nullable value if this collector contains no problems, otherwise return the problems.<T> RetNullable<T>thenNullableValue(java.util.function.Supplier<T> getter)Generate a nullable value if this collector contains no problems, otherwise return the problems.RetVoidthenRun(java.lang.Runnable runner)If this collector has no problems, then run the runner and return a void value.<T> RetVal<T>thenValue(NonnullSupplier<T> getter)Generate a non-null value if this collector contains no problems, otherwise return the problems.<T> ProblemCollectorvalidateEach(java.util.Collection<T> values, 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.java.util.Collection<Problem>validProblems()Returns all contained problems in this object, but only if this object contains 1 or more problems.<T> ProblemCollectorwith(RetNullable<T> value, 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.<T> ProblemCollectorwith(RetVal<T> value, NonnullConsumer<T> setter)If the value has problems, load them into this collector, otherwise pass the value as an argument to the setter.ProblemCollectorwith(RetVoid value)Add in any problems from the value into this collector.<T> ProblemCollectorwith(WarningVal<T> value, NonnullConsumer<T> setter)If the value has problems, load them into this collector.ProblemCollectorwithAll(java.lang.Iterable<RetVoid> values)Add into this collector any problems from each of the values.ProblemCollectorwithAll(RetVoid... values)Add into this collector any problems from each of the values.ProblemCollectorwithProblem(Problem problem, Problem... problems)Adds all the problems in the argument into this collector.ProblemCollectorwithProblems(java.util.Collection<Problem> problems, java.util.Collection<Problem>... problemList)Adds all the problems in the argument into this collector.<T> ProblemCollectorwithValuedProblemContainer(ValuedProblemContainer<T> value, 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.
-
-
-
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 invokewith(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 invokewith(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 invokewith(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 addproblems- 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 containersproblemList- 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 valuesetter- 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 valuesetter- 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 valuesetter- 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 valuesetter- 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 aRetNullableis 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 aRetVoid. 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:ProblemContainerReturns whether this object contains 1 or more problems.- Specified by:
isProblemin interfaceProblemContainer- Returns:
- true if there is a problem, false if there is no problem.
-
hasProblems
public boolean hasProblems()
Description copied from interface:ProblemContainerReturns whether this object contains 1 or more problems. Duplicate ofProblemContainer.isProblem()for English readability.- Specified by:
hasProblemsin interfaceProblemContainer- Returns:
- true if there is a problem, false if there is no problem.
-
isOk
public boolean isOk()
Description copied from interface:ProblemContainerReturns whether this object contains zero problems.- Specified by:
isOkin interfaceProblemContainer- 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:ProblemContainerReturn 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:
anyProblemsin interfaceProblemContainer- Returns:
- the problems contained in this container, even if there are none.
-
validProblems
@Nonnull public java.util.Collection<Problem> validProblems()
Description copied from interface:ProblemContainerReturns all contained problems in this object, but only if this object contains 1 or more problems. If it contains 0, then this throws anIllegalStateException.The returned collection is read-only, and contains no null values.
- Specified by:
validProblemsin interfaceProblemContainer- 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:ProblemContainerReturn the problems as a single string, which combines theObject.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:
debugProblemsin interfaceProblemContainer- 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:ProblemContainerAdd 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:
joinProblemsWithin interfaceProblemContainer- Parameters:
problemList- a modifiable collection of zero or more problems.
-
-