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.
final Random random = ...;
final Op<Double> val = EphemeralConst.of(random::nextDouble);
Serialization Although the 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
);
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 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:
7.0
See Also:
  • Method Details

    • get

      public Op<T> get()
      Return a newly created, uninitialized constant of type T.
      Specified by:
      get in interface Op<T>
      Specified by:
      get in interface Supplier<T>
      Returns:
      a newly created, uninitialized constant of type T
      See Also:
    • value

      public T value()
      Fixes and returns the constant value.
      Specified by:
      value in class Val<T>
      Returns:
      the constant value
      Since:
      5.0
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • of

      public static <T> EphemeralConst<T> of(String name, Supplier<T> supplier)
      Create a new ephemeral constant with the given name and value supplier. For every newly created operation tree, a new constant value is chosen for this terminal operation. The value is then kept constant for this tree.
      Type Parameters:
      T - the constant type
      Parameters:
      name - the name of the ephemeral constant
      supplier - the value supplier
      Returns:
      a new ephemeral constant
      Throws:
      NullPointerException - if one of the arguments is null
    • of

      public static <T> EphemeralConst<T> of(Supplier<T> supplier)
      Create a new ephemeral constant with the given value supplier. For every newly created operation tree, a new constant value is chosen for this terminal operation. The value is then kept constant for this tree.
      Type Parameters:
      T - the constant type
      Parameters:
      supplier - the value supplier
      Returns:
      a new ephemeral constant
      Throws:
      NullPointerException - if the supplier is null