Package net.groboclown.retval
Class Ret
- java.lang.Object
-
- net.groboclown.retval.Ret
-
public class Ret extends java.lang.ObjectUtility class for working with collections of return values.
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.List<Problem>NO_PROBLEMS
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> ValueAccumulator<T>accumulateValues()Convenience function to create aValueAccumulator.static <T> ValueBuilder<T>buildValue(T value)Convenience function to create aWarningValinstance for the given value.static <T extends java.lang.AutoCloseable,R>
RetVal<R>closeWith(T value, NonnullThrowsFunction<T,RetVal<R>> func)Runs a function with a closable value.static <T extends java.lang.AutoCloseable,R>
RetNullable<R>closeWithNullable(T value, NonnullThrowsFunction<T,RetNullable<R>> func)Runs a function with a closable value.static <T extends java.lang.AutoCloseable>
RetVoidcloseWithVoid(T value, NonnullThrowsFunction<T,RetVoid> func)Runs a function with a closable value.static ProblemCollectorcollectProblems()Convenience function to create aProblemCollector.static java.util.Collection<Problem>enforceHasProblems(java.util.Collection<Problem> problems)Enforce that the list of problems is not empty by throwing an IllegalStateException if it does not contain problems.static voidenforceNoProblems(java.util.Collection<Problem> problems)Enforce that the list of problems is empty by throwing an IllegalStateException if it contains problems.static java.lang.StringjoinProblemMessages(java.lang.String joinText, java.util.Collection<Problem> problems)Join the problem'sProblem.localMessage()text into a single string, joined by the joinText.static java.util.List<Problem>joinProblems(Problem problem, Problem... problems)Joins problems into a list of problems.static java.util.List<Problem>joinProblemSets(java.util.Collection<Problem> problemSet, java.util.Collection<Problem>... problemSets)Join the array of lists of problems into a single list.static java.util.List<Problem>joinRetProblems(ProblemContainer ret, ProblemContainer... rets)JoinsProblemContainerinstances into a list of problems.static java.util.List<Problem>joinRetProblemSets(java.util.Collection<ProblemContainer> retSet, java.util.Collection<ProblemContainer>... retSets)Joins problems in collections ofProblemContainerinstances.
-
-
-
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 aValueAccumulator.- Type Parameters:
T- type of the accumulator- Returns:
- a value accumulator
-
collectProblems
@Nonnull public static ProblemCollector collectProblems()
Convenience function to create aProblemCollector.- Returns:
- a new problem collector.
-
buildValue
@Nonnull public static <T> ValueBuilder<T> buildValue(@Nonnull T value)
Convenience function to create aWarningValinstance 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 problemsproblemSets- 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 ofProblemContainerinstances.- 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)
JoinsProblemContainerinstances 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'sProblem.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 functionR- 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 functionR- 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.
-
-