Package com.vladris.maki
Class Variant1<T1>
- java.lang.Object
-
- com.vladris.maki.VariantBase
-
- com.vladris.maki.Variant1<T1>
-
- Type Parameters:
T1
- Represents the variants first type.
public class Variant1<T1> extends VariantBase
Represents a 1-type discriminate union.class Sample { static void Example() { // A variant can hold a value of any of its generic types Variant3<Integer, String, Double> variant = new Variant3<Integer, String, Double>(42); // Can reassign variant with another of its generic types variant.set("Hello world!"); // Check the type of the value currently inhabiting the variant if (variant.is(String.class)) { System.out.println("The string is: " + variant.<String>get()); } variant.set(0.5); // The right function will get picked depending on the value // currently inhabiting the variant variant.apply( (i) -> System.out.println(i + 1), (s) -> System.out.println(s + "!"), (d) -> System.out.println(d / 2)); } }
-
-
Constructor Summary
Constructors Constructor Description Variant1(T1 item, TypeGuard1... guard)
Creates a new Variant instance from an item of typeT1
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
apply(java.util.function.Consumer<T1> consumer1)
Applies one of the given consumers to the variant depending on the type currently inhabiting the variant.<R> R
apply(java.util.function.Function<T1,R> func1)
Applies one of the given functions to the variant depending on the type currently inhabiting the variant.static <T1> Variant1<T1>
make(T1 item, TypeGuard1... guard)
Creates a new Variant given an item of typeT1
.static <T1> Variant1<T1>
make1(T1 item)
Creates a new Variant given an item of typeT1
explicitly placed at index 0.<U1> Variant1<U1>
map(java.util.function.Function<T1,U1> func1)
Applies one of the given functions to the variant depending on the type currently inhabiting the variant.void
set(T1 item, TypeGuard1... guard)
Assigns an item of typeT1
to the variant.void
set1(T1 item)
Explicitly assigns an item of typeT1
at index 0.
-
-
-
Constructor Detail
-
Variant1
public Variant1(T1 item, TypeGuard1... guard)
Creates a new Variant instance from an item of typeT1
.- Parameters:
item
- Item of typeT1
.guard
- TypeGuards are used to disambiguate methods after type erasure, no argument should be supplied.
-
-
Method Detail
-
set
public void set(T1 item, TypeGuard1... guard)
Assigns an item of typeT1
to the variant.- Parameters:
item
- Item of typeT1
.guard
- TypeGuards are used to disambiguate methods after type erasure, no argument should be supplied.
-
set1
public void set1(T1 item)
Explicitly assigns an item of typeT1
at index 0.- Parameters:
item
- Item of typeT1
.
-
apply
public <R> R apply(java.util.function.Function<T1,R> func1)
Applies one of the given functions to the variant depending on the type currently inhabiting the variant.- Type Parameters:
R
- Represents the return type of all functions.- Parameters:
func1
- Function to apply on typeT1
.- Returns:
- Result of applying function.
-
apply
public void apply(java.util.function.Consumer<T1> consumer1)
Applies one of the given consumers to the variant depending on the type currently inhabiting the variant.- Parameters:
consumer1
- Consumer to apply on typeT1
.
-
map
public <U1> Variant1<U1> map(java.util.function.Function<T1,U1> func1)
Applies one of the given functions to the variant depending on the type currently inhabiting the variant.- Type Parameters:
U1
- Represents the return type offunc1
.- Parameters:
func1
- Function to apply on typeT1
.- Returns:
- Variant containing the result of applying the selected function.
-
make
public static <T1> Variant1<T1> make(T1 item, TypeGuard1... guard)
Creates a new Variant given an item of typeT1
.- Type Parameters:
T1
- Represents the variants first type.- Parameters:
item
- Item of typeT1
.guard
- TypeGuards are used to disambiguate methods after type erasure, no argument should be supplied.- Returns:
- A Variant containing the given item.
-
make1
public static <T1> Variant1<T1> make1(T1 item)
Creates a new Variant given an item of typeT1
explicitly placed at index 0.- Type Parameters:
T1
- Represents the variants first type.- Parameters:
item
- Item of typeT1
.- Returns:
- A Variant containing the given item at index 0.
-
-