Class TryCompanion


  • public final class TryCompanion
    extends java.lang.Object
    Acts as a Scala type companion object for Try/Success/Failure.
    The primary purpose is to get the Scala feel of instantiating classes.
    In Scala you can define a companion object for a class, acting as a static reference/singleton for that class allowing you do define factory methods.
    One use case is to define methods with the same name as the class and let these methods invoke the constructor thus creating a nice way to create instances without using the word "new".
    This can be achieved in java by statically importing a method and then using it.
    The limitation is that classes may not have method with the same name as the class itself hence new companion classes have to be created.
    To be able to use it in a neat concise way one needs to statically import the method.
     import static javascalautils.TryCompanion.Failure;
     import static javascalautils.TryCompanion.Success;
     import static javascalautils.TryCompanion.Try;
    
     Try<Integer> t = Try(() -> 9 / 3);
     Try<String> ts = Success("Peter was here");
     Try<String> tf = Failure(new Exception("Bad mojo!"));
    
     //code that performs a side effect but has not return type can be written like this
     Try<Unit> t = Try(() -> {
        database.delete(someId);
     });
    
     
    Since:
    1.3
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> Failure<T> Failure​(java.lang.Throwable throwable)
      Creates an instance of Failure wrapping the provided throwable.
      static <T> Success<T> Success​(T value)
      Creates an instance of Success wrapping the provided value.
      static <T> Try<T> Try​(ThrowableFunction0<T> function)
      Creates an instance of Try wrapping the result of the provided function.
      static Try<Unit> Try​(VoidFunction0 function)
      Creates an instance of Try with a Unit return type.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • Try

        public static Try<Unit> Try​(VoidFunction0 function)
        Creates an instance of Try with a Unit return type.
        The purpose is to allow the user to invoke a side-effecting function that may succeed/fail but has now return type.
        E.g. deleting something from a database may not have an interesting return type. One is only interested of the outcome, Success/Failure. Best used in conjunction with statically importing this method.
         import static javascalautils.TryCompanion.Try;
        
         Try<Unit> t = Try(() -> {
            database.delete(someId);
         });
         
        Parameters:
        function - The function to render either the value T or raise an exception.
        Returns:
        The resulting Try instance wrapping either Unit or an exception
        Since:
        1.9
      • Try

        public static <T> Try<T> Try​(ThrowableFunction0<T> function)
        Creates an instance of Try wrapping the result of the provided function.
        Best used in conjunction with statically importing this method.
         import static javascalautils.TryCompanion.Try;
        
         Try<Integer> t = Try(() -> 9 / 3);
         
        Type Parameters:
        T - The type for the Try
        Parameters:
        function - The function to render either the value T or raise an exception.
        Returns:
        The resulting Try instance wrapping what the function resulted in
        Since:
        1.3
        See Also:
        Try.apply(ThrowableFunction0)
      • Failure

        public static <T> Failure<T> Failure​(java.lang.Throwable throwable)
        Creates an instance of Failure wrapping the provided throwable.
        Best used in conjunction with statically importing this method.
         import static javascalautils.TryCompanion.Failure;
        
         Try<String> tf = Failure(new Exception("Bad mojo!"));
         
        Type Parameters:
        T - The type for the Try
        Parameters:
        throwable - The throwable to wrap
        Returns:
        The failure instance
        Since:
        1.3
        See Also:
        Failure(Throwable)
      • Success

        public static <T> Success<T> Success​(T value)
        Creates an instance of Success wrapping the provided value.
        Best used in conjunction with statically importing this method.
         import static javascalautils.TryCompanion.Success;
        
         Try<String> ts = Success("Peter was here");
         
        Type Parameters:
        T - The type for the Try
        Parameters:
        value - The value to wrap
        Returns:
        The failure instance
        Since:
        1.3
        See Also:
        Success(Object)