Package javascalautils
Class OptionCompanion
- java.lang.Object
-
- javascalautils.OptionCompanion
-
public final class OptionCompanion extends java.lang.Object
Acts as a Scala type companion object forOption
/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 ofNone
.static <T> Option<T>
Option(T value)
Creates an instance of Option.static <T> Some<T>
Some(T value)
Creates an instance ofSome
.
-
-
-
Method Detail
-
Option
public static <T> Option<T> Option(T value)
Creates an instance of Option.
If anull
value is provided thenNone
is returned, elseSome
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 thisOption
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 ofSome
.
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 thisSome
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 ofNone
.
In practice returns the singleton instance forNone
, 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 thisNone
represents- Returns:
- The singleton instance for
None
- Since:
- 1.3
- See Also:
Option.None()
,Option.empty()
,Option.DEFAULT_NONE
-
-