Gene.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-5.2.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 5.2
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     default A allele() {
61         return getAllele();
62     }
63 
64     /**
65      * Return the allele of this gene.
66      *
67      @return the allele of this gene.
68      @deprecated Use {@link #allele()} instead. Implementer must still
69      *             implement this method.
70      */
71     @Deprecated
72     A getAllele();
73 
74     /**
75      * Return a new, random gene with the same type and with the same constraints
76      * than this gene. For all genes returned by this method holds
77      * {@code gene.getClass() == gene.newInstance().getClass()}. Implementations
78      * of this method has to use the {@link java.util.Random} object which can
79      * be fetched from the {@link io.jenetics.util.RandomRegistry}.
80      */
81     @Override
82     G newInstance();
83 
84     /**
85      * Create a new gene from the given {@code value} and the gene context.
86      *
87      @since 2.0
88      @param value the value of the new gene.
89      @return a new gene with the given value.
90      */
91     G newInstance(final A value);
92 
93 }