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);
}
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.AnyChromosome
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj) |
A |
getAllele()
Return the allele of this gene.
|
int |
hashCode() |
boolean |
isValid()
Check if this object is valid.
|
AnyGene<A> |
newInstance()
Return a new, random gene with the same type and with the same constraints
than this gene.
|
AnyGene<A> |
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. |
String |
toString() |
public A getAllele()
Gene
public AnyGene<A> newInstance()
Gene
gene.getClass() == gene.newInstance().getClass()
. Implementations
of this method has to use the Random
object which can
be fetched from the RandomRegistry
.newInstance
in interface Gene<A,AnyGene<A>>
newInstance
in interface Factory<AnyGene<A>>
public AnyGene<A> newInstance(A value)
Gene
value
and the gene context.newInstance
in interface Gene<A,AnyGene<A>>
value
- the value of the new gene.public boolean isValid()
Verifiable
isValid
in interface Verifiable
public static <A> AnyGene<A> of(A allele, Supplier<? extends A> supplier, Predicate<? super A> validator)
AnyGene
instance with the given parameters. New
(random) genes are created with the given allele supplier
.A
- the allele typeallele
- the actual allele instance the created gene represents.
null
values 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 the isValid()
method.AnyGene
with the given parametersNullPointerException
- if the supplier
or validator
is null
public static <A> AnyGene<A> of(A allele, Supplier<? extends A> supplier)
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
.A
- the allele typeallele
- the actual allele instance the created gene represents.
null
values are allowed.supplier
- the allele-supplier which is used for creating new,
random allelesAnyGene
with the given parametersNullPointerException
- if the suppler
is null
public static <A> AnyGene<A> of(Supplier<? extends A> supplier)
AnyGene
instance with the given allele
supplier
. The validator
predicate of the generated gene
will always return true
.A
- the allele typesupplier
- the allele-supplier which is used for creating new,
random allelesAnyGene
with the given parametersNullPointerException
- if one of the parameters is null
public static <A> AnyGene<A> of(Supplier<? extends A> supplier, Predicate<? super A> validator)
AnyGene
instance with the given parameters. New
(random) genes are created with the given allele supplier
.A
- the allele typesupplier
- the allele-supplier which is used for creating new,
random allelesvalidator
- the validator used for validating the created gene. This
predicate is used in the isValid()
method.AnyGene
with the given parametersNullPointerException
- if one of the parameters is null
© 2007-2019 Franz Wilhelmstötter (2019-11-18 20:30)