Package ch.codexs.util
Class Result<T,I>
- java.lang.Object
-
- ch.codexs.util.Result<T,I>
-
public class Result<T,I> extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classResult.BiCombiner<U>static classResult.Catcher<E extends java.lang.Throwable>classResult.ElseValueProcessorstatic classResult.IssueTransformer<T,I>classResult.ThenValueProcessorstatic interfaceResult.ThrowingSupplier<T,E extends java.lang.Throwable>classResult.ValueProcessorclassResult.ValueTransformer<R>
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <E extends java.lang.Throwable>
Result.Catcher<E>catching(java.lang.Class<E> throwableClass)static <T,I>
Result<java.util.List<T>,I>combineAllSuccessful(java.util.Collection<Result<T,I>> results)Combines the provided results into one singleResult<List<T>, I>.<U> Result.BiCombiner<U>combineWith(Result<U,I> other)Combines this result with another onebooleanequals(java.lang.Object o)static <U,I>
Result<U,I>failed(I firstIssue, I... issues)Builds a result without a content (failed).static <U,I>
Result<U,I>failed(java.util.List<I> issues)Builds a result without a content (failed).<U> Result<U,I>flatMap(java.util.function.Function<T,Result<U,I>> mapper)Map the content (if any) to anotherResult.booleanhasFailed()Indicates if the result has failed, in other words, is there a content in it.inthashCode()Result.ElseValueProcessorifFailedDo(java.util.function.Consumer<java.util.List<I>> issueConsumer)Give conditional access to the issues.<R> Result.ValueTransformer<R>ifFailedTransform(java.util.function.Function<java.util.List<I>,R> issueTransformer)Transforms the issues into an output in case of missing content, using the given function.Result.ThenValueProcessorifIssuesExistDo(java.util.function.Consumer<java.util.List<I>> issueConsumer)Give conditional access to the issues.java.util.List<I>issues()Get the issues.<U> Result<U,I>map(java.util.function.Function<T,U> mapper)Map the content (if any).<U,J>
Result<U,J>mapBoth(java.util.function.Function<T,U> contentMapper, java.util.function.Function<I,J> issueMapper)Map both the content and the issues to build a newResultwith them.<J> Result<T,J>mapIssues(java.util.function.Function<I,J> mapper)Map the issues to transform them.static <U,I>
Result<U,I>succeeded(U content)Builds a result with a content.static <U,I>
Result<U,I>succeeded(U content, I firstIssue, I... issues)Builds a result with a content and issues.<U> Result.IssueTransformer<U,I>unwrap(java.util.function.BiFunction<T,java.util.List<I>,U> mapper)Give access to both the content and the issues in order to build an output.Result<T,I>withAddedIssues(I... issues)Creates a newResult, copy of this one, but with added issues at the end.Result<T,I>withAddedIssues(java.lang.Iterable<I>... issueLists)Creates a newResult, copy of this one, but with added issues at the end.
-
-
-
Method Detail
-
succeeded
public static <U,I> Result<U,I> succeeded(U content)
Builds a result with a content.- Type Parameters:
U- The type of the content.I- The type of the issues (empty).- Parameters:
content- non-null reference to the content to hold the returnedResult.- Returns:
- Result wrapping the provided content.
-
succeeded
@SafeVarargs public static <U,I> Result<U,I> succeeded(U content, I firstIssue, I... issues)
Builds a result with a content and issues.- Type Parameters:
U- The type of the content.I- The type of the issues.- Parameters:
content- non-null reference to the content to hold the returnedResult.firstIssue- Issue to include.issues- Array of additional issues to include.- Returns:
- Result wrapping the provided content and issues.
-
failed
public static <U,I> Result<U,I> failed(java.util.List<I> issues)
Builds a result without a content (failed).- Type Parameters:
U- The type of the content.I- The type of the issues.- Parameters:
issues- The issues.- Returns:
- A new
Result
-
failed
@SafeVarargs public static <U,I> Result<U,I> failed(I firstIssue, I... issues)
Builds a result without a content (failed).- Type Parameters:
U- The type of the content.I- The type of the issues.- Parameters:
firstIssue- The first issue.issues- The following issues.- Returns:
- A new
Result
-
catching
public static <E extends java.lang.Throwable> Result.Catcher<E> catching(java.lang.Class<E> throwableClass)
- Type Parameters:
E- The type of throwable to catch.- Parameters:
throwableClass- Class of the expected throwable to catch.- Returns:
- Catcher to run the following supplier in a exception safe manner.
-
hasFailed
public boolean hasFailed()
Indicates if the result has failed, in other words, is there a content in it.- Returns:
- true if the content is missing.
-
issues
public java.util.List<I> issues()
Get the issues.- Returns:
- the issues.
-
map
public <U> Result<U,I> map(java.util.function.Function<T,U> mapper)
Map the content (if any).- Type Parameters:
U- The type of the mapped content.- Parameters:
mapper-Function- Returns:
- A new
Resultwith mapped content.
-
flatMap
public <U> Result<U,I> flatMap(java.util.function.Function<T,Result<U,I>> mapper)
Map the content (if any) to anotherResult.- Type Parameters:
U- The type of the mapped content.- Parameters:
mapper-Functionto transform the content (if any).- Returns:
- A new
Resultwith mapped content.
-
mapIssues
public <J> Result<T,J> mapIssues(java.util.function.Function<I,J> mapper)
Map the issues to transform them.- Type Parameters:
J- The type of the mapped issues.- Parameters:
mapper-Functionto transform the issues (if any).- Returns:
- A new
Resultwith mapped issues.
-
mapBoth
public <U,J> Result<U,J> mapBoth(java.util.function.Function<T,U> contentMapper, java.util.function.Function<I,J> issueMapper)
Map both the content and the issues to build a newResultwith them.- Type Parameters:
U- The type of the mapped content.J- The type of the mapped issues.- Parameters:
contentMapper-Functionto transform the content. Will only be called if the content is present.issueMapper-Functionto transform the issues. Will only be called if the issues are present.- Returns:
Result- a new result with the mapped content and issues.
-
withAddedIssues
@SafeVarargs public final Result<T,I> withAddedIssues(java.lang.Iterable<I>... issueLists)
Creates a newResult, copy of this one, but with added issues at the end.- Parameters:
issueLists- the issues to add.- Returns:
- The new
Resultincluding new issues.
-
withAddedIssues
@SafeVarargs public final Result<T,I> withAddedIssues(I... issues)
Creates a newResult, copy of this one, but with added issues at the end.- Parameters:
issues- the issues to add.- Returns:
- The new
Resultincluding new issues.
-
ifIssuesExistDo
public Result.ThenValueProcessor ifIssuesExistDo(java.util.function.Consumer<java.util.List<I>> issueConsumer)
Give conditional access to the issues. The consumer is called only if there are contained issues. Can be followed by the call to a getter returning the content, null if the result has no content.- Parameters:
issueConsumer- the consumer to process issues- Returns:
ThenValueProcessor- to get the content
-
ifFailedDo
public Result.ElseValueProcessor ifFailedDo(java.util.function.Consumer<java.util.List<I>> issueConsumer)
Give conditional access to the issues. The consumer is called only if there is no content in the result. Can be followed by the call to a getter returning the content, null if the result has no content.- Parameters:
issueConsumer-Consumerto process issues- Returns:
ElseValueProcessor- to get the content
-
ifFailedTransform
public <R> Result.ValueTransformer<R> ifFailedTransform(java.util.function.Function<java.util.List<I>,R> issueTransformer)
Transforms the issues into an output in case of missing content, using the given function. The output is not accessible straight away but is transmitted to the returnedValueTransformer.- Type Parameters:
R- The type of the output resulting from the transformation provided- Parameters:
issueTransformer- A function to transform the issues into a value that will be returned if there was no success.- Returns:
ValueTransformer- allowing to unwrap the content and transform it into a another value in case of success
-
unwrap
public <U> Result.IssueTransformer<U,I> unwrap(java.util.function.BiFunction<T,java.util.List<I>,U> mapper)
Give access to both the content and the issues in order to build an output. If the content is absent (hasFailed()returns true) the chained issue transformation is applied.- Type Parameters:
U- The type of the mapped content.- Parameters:
mapper- The function to map both the content and the issues.- Returns:
IssueTransformer<U, I>enabling the transformation of the issues.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
combineWith
public <U> Result.BiCombiner<U> combineWith(Result<U,I> other)
Combines this result with another one- Type Parameters:
U- the type of the mapped content.- Parameters:
other- Result to combine with.- Returns:
- BiCombiner to decide how to actually combine values.
-
combineAllSuccessful
public static <T,I> Result<java.util.List<T>,I> combineAllSuccessful(java.util.Collection<Result<T,I>> results)
Combines the provided results into one singleResult<List<T>, I>. As provided results may not be all successful, they are filtered and the resulting list may be smaller than the provided result list. On the other hand, all the issues are collected, including the one of the failed provided results.- Type Parameters:
T- The type of the content.I- The type of the issues.- Parameters:
results- Collection of results to combine.- Returns:
- Result containing the contents of the provided collection of results.
-
-