Gene.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-6.1.0).
03  * Copyright (c) 2007-2020 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
19  */
20 package io.jenetics;
21 
22 import io.jenetics.util.Factory;
23 import io.jenetics.util.Verifiable;
24 
25 /**
26  * Genes are the atoms of the <em>Jenetics</em> library. They contain the actual
27  * information (alleles) of the encoded solution. All implementations of the
28  * this interface are final, immutable and can be only created via static
29  * factory methods which have the name {@code of}. When extending the library
30  * with own {@code Gene} implementations, it is recommended to also implement it
31  * as <a href="https://en.wikipedia.org/wiki/Value_object">value objects</a>.
32  *
33  * @implSpec
34  <em>Jenetics</em> requires that the individuals ({@link Genotype} and
35  {@link Phenotype}) are not changed after they have been created. Therefore,
36  * all implementations of the {@code Gene} interface must also be
37  <em>immutable</em>.
38  *
39  @see <a href="https://en.wikipedia.org/wiki/Value_object">Value object</a>
40  @see Chromosome
41  *
42  @param <A> the <a href="http://en.wikipedia.org/wiki/Allele">Allele</a> type
43  *         of this gene.
44  *
45  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
46  @since 1.0
47  @version 6.0
48  */
49 public interface Gene<A, G extends Gene<A, G>>
50     extends
51         Factory<G>,
52         Verifiable
53 {
54 
55     /**
56      * Return the allele of this gene.
57      *
58      @return the allele of this gene.
59      */
60     A allele();
61 
62     /**
63      * Return a new, random gene with the same type and with the same constraints
64      * than this gene. For all genes returned by this method holds
65      * {@code gene.getClass() == gene.newInstance().getClass()}. Implementations
66      * of this method has to use the {@link java.util.Random} object which can
67      * be fetched from the {@link io.jenetics.util.RandomRegistry}.
68      */
69     @Override
70     G newInstance();
71 
72     /**
73      * Create a new gene from the given {@code value} and the gene context.
74      *
75      @since 2.0
76      @param value the value of the new gene.
77      @return a new gene with the given value.
78      */
79     G newInstance(final A value);
80 
81 }