public final class EphemeralConst<T> extends Val<T> implements Op<T>, Serializable
final Random random = ...;
final Op<Double> val = EphemeralConst.of(random::nextDouble);
EphemeralConst
class implements the Serializable
interface, the serialization will fail if the const supplier is not
serializable as well. This can be achieved by casting the
supplier to a Serializable
.
final Random random = new Random();
final EphemeralConst<Integer> object = EphemeralConst.of(
"R",
(Supplier<Integer> & Serializable)random::nextInt
);
final class RandomInt implements Supplier<Integer>, Serializable {
private final Random rnd = new Random();
private final int min;
private final int max;
private RandomInt(final int min, final int max) {
this.min = min;
this.max = max;
}
@Override
public Integer get() {
return rnd.nextInt(max - min) + min;
}
}
Modifier and Type | Method and Description |
---|---|
Op<T> |
get()
Return a newly created, uninitialized constant of type
T . |
static <T> EphemeralConst<T> |
of(String name,
Supplier<T> supplier)
Create a new ephemeral constant with the given
name and value
supplier . |
static <T> EphemeralConst<T> |
of(Supplier<T> supplier)
Create a new ephemeral constant with the given value
supplier . |
String |
toString() |
T |
value()
Fixes and returns the constant value.
|
public static <T> EphemeralConst<T> of(String name, Supplier<T> supplier)
name
and value
supplier
. For every newly created operation tree, a new constant
value is chosen for this terminal operation. The value is than kept
constant for this tree.T
- the constant typename
- the name of the ephemeral constantsupplier
- the value supplierNullPointerException
- if one of the arguments is null
public static <T> EphemeralConst<T> of(Supplier<T> supplier)
supplier
.
For every newly created operation tree, a new constant value is chosen
for this terminal operation. The value is than kept constant for this tree.T
- the constant typesupplier
- the value supplierNullPointerException
- if the supplier
is null
© 2007-2019 Franz Wilhelmstötter (2019-11-18 20:30)