Class Result<T,​I>


  • public class Result<T,​I>
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      Result​(T content, java.util.List<I> issues)
      Construct a Result in a simple way.
    • 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 single Result<List<T>, I>.
      <U> Result.BiCombiner<U> combineWith​(Result<U,​I> other)
      Combines this result with another one
      boolean equals​(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 another Result.
      boolean hasFailed()
      Indicates if the result has failed, in other words, is there a content in it.
      int hashCode()  
      Result.ElseValueProcessor ifFailedDo​(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.ThenValueProcessor ifIssuesExistDo​(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 new Result with 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 new Result, copy of this one, but with added issues at the end.
      Result<T,​I> withAddedIssues​(java.lang.Iterable<I>... issueLists)
      Creates a new Result, copy of this one, but with added issues at the end.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Result

        public Result​(T content,
                      java.util.List<I> issues)
        Construct a Result in a simple way. If the content is null, then the result is considered as failed.
        Parameters:
        content - An object being the content of the result.
        issues - List of issues, non null.
    • 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 returned Result.
        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 returned Result.
        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 Result with mapped content.
      • flatMap

        public <U> Result<U,​I> flatMap​(java.util.function.Function<T,​Result<U,​I>> mapper)
        Map the content (if any) to another Result.
        Type Parameters:
        U - The type of the mapped content.
        Parameters:
        mapper - Function to transform the content (if any).
        Returns:
        A new Result with 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 - Function to transform the issues (if any).
        Returns:
        A new Result with 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 new Result with them.
        Type Parameters:
        U - The type of the mapped content.
        J - The type of the mapped issues.
        Parameters:
        contentMapper - Function to transform the content. Will only be called if the content is present.
        issueMapper - Function to 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 new Result, copy of this one, but with added issues at the end.
        Parameters:
        issueLists - the issues to add.
        Returns:
        The new Result including new issues.
      • withAddedIssues

        @SafeVarargs
        public final Result<T,​I> withAddedIssues​(I... issues)
        Creates a new Result, copy of this one, but with added issues at the end.
        Parameters:
        issues - the issues to add.
        Returns:
        The new Result including 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 - Consumer to 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 returned ValueTransformer.
        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:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.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 single Result<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.