java.lang.Object
io.jenetics.AnyGene<A>
Gene implementation, which allows creating 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);
}
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 TypeMethodDescriptionallele()Return the allele of this gene.booleaninthashCode()booleanisValid()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 givenvalueand the gene context.static <A> AnyGene<A> Create a newAnyGeneinstance with the given parameters.static <A> AnyGene<A> Create a newAnyGeneinstance with the given parameters.static <A> AnyGene<A> Create a newAnyGeneinstance with the given allelesupplier.static <A> AnyGene<A> Create a newAnyGeneinstance with the given parameters.toString()
-
Method Details
-
allele
Description copied from interface:GeneReturn the allele of this gene. -
newInstance
Description copied from interface:GeneReturn a new, random gene with the same type and with the same constraints as this gene. For all genes returned by this method holdsgene.getClass() == gene.newInstance().getClass(). Implementations of this method have to use theRandomGeneratorobject which can be fetched from theRandomRegistry.- Specified by:
newInstancein interfaceFactory<A>- Specified by:
newInstancein interfaceGene<A,AnyGene<A>> - Returns:
- a new instance of type T
-
newInstance
Description copied from interface:GeneCreate a new gene from the givenvalueand the gene context.- Specified by:
newInstancein interfaceGene<A,AnyGene<A>> - Parameters:
value- the value of the new gene.- Returns:
- a new gene with the given value.
-
isValid
Description copied from interface:VerifiableCheck if this object is valid.- Specified by:
isValidin interfaceVerifiable- Returns:
- true if this object is valid, false otherwise.
-
hashCode
-
equals
-
toString
-
of
public static <A> AnyGene<A> of(A allele, Supplier<? extends A> supplier, Predicate<? super A> validator) Create a newAnyGeneinstance with the given parameters. New (random) genes are created with the given allelesupplier.- Type Parameters:
A- the allele type- Parameters:
allele- the actual allele instance the created gene represents.nullvalues are allowed.supplier- the allele-supplier which is used for creating new, random allelesvalidator- the validator used for validating the created gene. This predicate is used in theisValid()method.- Returns:
- a new
AnyGenewith the given parameters - Throws:
NullPointerException- if thesupplierorvalidatorisnull
-
of
Create a newAnyGeneinstance with the given parameters. New (random) genes are created with the given allelesupplier. Thevalidatorpredicate of the generated gene will always returntrue.- Type Parameters:
A- the allele type- Parameters:
allele- the actual allele instance the created gene represents.nullvalues are allowed.supplier- the allele-supplier which is used for creating new, random alleles- Returns:
- a new
AnyGenewith the given parameters - Throws:
NullPointerException- if thesupplerisnull
-
of
Create a newAnyGeneinstance with the given allelesupplier. Thevalidatorpredicate of the generated gene will always returntrue.- Type Parameters:
A- the allele type- Parameters:
supplier- the allele-supplier which is used for creating new, random alleles- Returns:
- a new
AnyGenewith the given parameters - Throws:
NullPointerException- if one of the parameters isnull
-
of
Create a newAnyGeneinstance with the given parameters. New (random) genes are created with the given allelesupplier.- Type Parameters:
A- the allele type- Parameters:
supplier- the allele-supplier which is used for creating new, random allelesvalidator- the validator used for validating the created gene. This predicate is used in theisValid()method.- Returns:
- a new
AnyGenewith the given parameters - Throws:
NullPointerException- if one of the parameters isnull
-