Package io.jenetics.prog.op
Class EphemeralConst<T>
- java.lang.Object
-
- io.jenetics.prog.op.Val<T>
-
- io.jenetics.prog.op.EphemeralConst<T>
-
- All Implemented Interfaces:
Op<T>
,Serializable
,Function<T[],T>
,Supplier<Op<T>>
public final class EphemeralConst<T> extends Val<T> implements Op<T>, Serializable
Implementation of an ephemeral constant. It causes the insertion of a mutable constant into the operation tree. Every time this terminal is chosen a, different value is generated which is then used for that particular terminal, and which will remain fixed for the given tree. The main usage would be to introduce random terminal values.Serialization Although thefinal Random random = ...; final Op<Double> val = EphemeralConst.of(random::nextDouble);
EphemeralConst
class implements theSerializable
interface, the serialization will fail if the const supplier is not serializable as well. This can be achieved by casting the supplier to aSerializable
.The serialization of the constant will fail, if the lambda has to capture variables form a non-serializable context (class). In such a case it is advisable to create a dedicated supplier class.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; } }
- Since:
- 3.9
- Version:
- 5.0
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Op<T>
get()
Return a newly created, uninitialized constant of typeT
.static <T> EphemeralConst<T>
of(String name, Supplier<T> supplier)
Create a new ephemeral constant with the givenname
and valuesupplier
.static <T> EphemeralConst<T>
of(Supplier<T> supplier)
Create a new ephemeral constant with the given valuesupplier
.String
toString()
T
value()
Fixes and returns the constant value.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.jenetics.prog.op.Op
arity, isTerminal, name
-
-
-
-
Method Detail
-
of
public static <T> EphemeralConst<T> of(String name, Supplier<T> supplier)
Create a new ephemeral constant with the givenname
and valuesupplier
. 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.- Type Parameters:
T
- the constant type- Parameters:
name
- the name of the ephemeral constantsupplier
- the value supplier- Returns:
- a new ephemeral constant
- Throws:
NullPointerException
- if one of the arguments isnull
-
of
public static <T> EphemeralConst<T> of(Supplier<T> supplier)
Create a new ephemeral constant with the given valuesupplier
. 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.- Type Parameters:
T
- the constant type- Parameters:
supplier
- the value supplier- Returns:
- a new ephemeral constant
- Throws:
NullPointerException
- if thesupplier
isnull
-
-