Class OptionCompanion


  • public final class OptionCompanion
    extends java.lang.Object
    Acts as a Scala type companion object for Option/Some/None.
    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.OptionCompanion.Option;
     import static javascalautils.OptionCompanion.Some;
     import static javascalautils.OptionCompanion.None;
    
     Option<String> option = Option("Life is full of options");
     Option<String> some = Some("Some is never None");
     Option<String> none = None();
     
    Since:
    1.3
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> None<T> None()
      Returns an instance of None.
      static <T> Option<T> Option​(T value)
      Creates an instance of Option.
      static <T> Some<T> Some​(T value)
      Creates an instance of Some.
      • Methods inherited from class java.lang.Object

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

      • Option

        public static <T> Option<T> Option​(T value)
        Creates an instance of Option.
        If a null value is provided then None is returned, else Some containing the provided value.
        Best used in conjunction with statically importing this method.
         import static javascalautils.OptionCompanion.Option;
        
         Option<String> option = Option("Life is full of options");
         
        Type Parameters:
        T - The type for the value this Option represents
        Parameters:
        value - The value this Option shall represent
        Returns:
        The Option representing the provided value
        Since:
        1.3
        See Also:
        Option.apply(Object)
      • Some

        public static <T> Some<T> Some​(T value)
        Creates an instance of Some.
        Best used in conjunction with statically importing this method.
         import static javascalautils.OptionCompanion.Some;
        
         Option<String> some = Some("Some is never None");
         
        Type Parameters:
        T - The type for the value this Some represents
        Parameters:
        value - The value represented by this Some
        Returns:
        The Some representing the value
        Since:
        1.3
        See Also:
        Some(Object)
      • None

        public static <T> None<T> None()
        Returns an instance of None.
        In practice returns the singleton instance for None, never creates new instances.
        Best used in conjunction with statically importing this method.
         import static javascalautils.OptionCompanion.None;
        
         Option<String> none = None();
         
        Type Parameters:
        T - The type for the value this None represents
        Returns:
        The singleton instance for None
        Since:
        1.3
        See Also:
        Option.None(), Option.empty(), Option.DEFAULT_NONE