Package io.jenetics

Class AnyGene<A>

java.lang.Object
io.jenetics.AnyGene<A>
All Implemented Interfaces:
Gene<A,AnyGene<A>>, Factory<AnyGene<A>>, Self<AnyGene<A>>, Verifiable

public final class AnyGene<A> extends Object implements Gene<A,AnyGene<A>>
Gene implementation, which allows to create genes without explicit implementing the Gene interface.
class Main { // First monday of 2015. private static final LocalDate MIN_MONDAY = LocalDate.of(2015, 1, 5); // Supplier of random 'LocalDate' objects. The implementation is responsible // for guaranteeing the desired allele restriction. In this case we will // generate only mondays. static LocalDate nextRandomMonday() { return MIN_MONDAY.plusWeeks(RandomRegistry.getRandom().nextInt(1000)); } // Create a new 'LocalDate' gene. All other genes, created with // gene.newInstance(), are calling the 'newRandomMonday' method. final AnyGene<LocalDate> gene = AnyGene.of(Main::nextRandomMonday); }
The example above shows how to create LocalDate genes from a random LocalDate supplier. It also shows how to implement a restriction on the created dates. The usage of the AnyGene class is useful for supporting custom allele types without explicit implementation of the Gene interface. But the AnyGene can only be used for a subset of the existing alterers.
Since:
3.3
Version:
6.0
See Also:
Implementation Note:
This class is immutable and thread-safe.
  • Method Summary

    Modifier and Type
    Method
    Description
    Return the allele of this gene.
    boolean
     
    int
     
    boolean
    Check if this object is valid.
    Return a new, random gene with the same type and with the same constraints as this gene.
    newInstance(A value)
    Create a new gene from the given value and the gene context.
    static <A> AnyGene<A>
    of(A allele, Supplier<? extends A> supplier)
    Create a new AnyGene instance with the given parameters.
    static <A> AnyGene<A>
    of(A allele, Supplier<? extends A> supplier, Predicate<? super A> validator)
    Create a new AnyGene instance with the given parameters.
    static <A> AnyGene<A>
    of(Supplier<? extends A> supplier)
    Create a new AnyGene instance with the given allele supplier.
    static <A> AnyGene<A>
    of(Supplier<? extends A> supplier, Predicate<? super A> validator)
    Create a new AnyGene instance with the given parameters.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface io.jenetics.util.Factory

    instances

    Methods inherited from interface io.jenetics.util.Self

    self
  • Method Details

    • allele

      public A allele()
      Description copied from interface: Gene
      Return the allele of this gene.
      Specified by:
      allele in interface Gene<A,AnyGene<A>>
      Returns:
      the allele of this gene.
    • newInstance

      public AnyGene<A> newInstance()
      Description copied from interface: Gene
      Return a new, random gene with the same type and with the same constraints as this gene. For all genes returned by this method holds gene.getClass() == gene.newInstance().getClass(). Implementations of this method has to use the Random object which can be fetched from the RandomRegistry.
      Specified by:
      newInstance in interface Factory<A>
      Specified by:
      newInstance in interface Gene<A,AnyGene<A>>
      Returns:
      a new instance of type T
    • newInstance

      public AnyGene<A> newInstance(A value)
      Description copied from interface: Gene
      Create a new gene from the given value and the gene context.
      Specified by:
      newInstance in interface Gene<A,AnyGene<A>>
      Parameters:
      value - the value of the new gene.
      Returns:
      a new gene with the given value.
    • isValid

      public boolean isValid()
      Description copied from interface: Verifiable
      Check if this object is valid.
      Specified by:
      isValid in interface Verifiable
      Returns:
      true if this object is valid, false otherwise.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

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

      public static <A> AnyGene<A> of(A allele, Supplier<? extends A> supplier, Predicate<? super A> validator)
      Create a new AnyGene instance with the given parameters. New (random) genes are created with the given allele supplier.
      Type Parameters:
      A - the allele type
      Parameters:
      allele - the actual allele instance the created gene represents. null values are allowed.
      supplier - the allele-supplier which is used for creating new, random alleles
      validator - the validator used for validating the created gene. This predicate is used in the isValid() method.
      Returns:
      a new AnyGene with the given parameters
      Throws:
      NullPointerException - if the supplier or validator is null
    • of

      public static <A> AnyGene<A> of(A allele, Supplier<? extends A> supplier)
      Create a new AnyGene instance with the given parameters. New (random) genes are created with the given allele supplier. The validator predicate of the generated gene will always return true.
      Type Parameters:
      A - the allele type
      Parameters:
      allele - the actual allele instance the created gene represents. null values are allowed.
      supplier - the allele-supplier which is used for creating new, random alleles
      Returns:
      a new AnyGene with the given parameters
      Throws:
      NullPointerException - if the suppler is null
    • of

      public static <A> AnyGene<A> of(Supplier<? extends A> supplier)
      Create a new AnyGene instance with the given allele supplier. The validator predicate of the generated gene will always return true.
      Type Parameters:
      A - the allele type
      Parameters:
      supplier - the allele-supplier which is used for creating new, random alleles
      Returns:
      a new AnyGene with the given parameters
      Throws:
      NullPointerException - if one of the parameters is null
    • of

      public static <A> AnyGene<A> of(Supplier<? extends A> supplier, Predicate<? super A> validator)
      Create a new AnyGene instance with the given parameters. New (random) genes are created with the given allele supplier.
      Type Parameters:
      A - the allele type
      Parameters:
      supplier - the allele-supplier which is used for creating new, random alleles
      validator - the validator used for validating the created gene. This predicate is used in the isValid() method.
      Returns:
      a new AnyGene with the given parameters
      Throws:
      NullPointerException - if one of the parameters is null