001/*
002 * Java Genetic Algorithm Library (jenetics-8.1.0).
003 * Copyright (c) 2007-2024 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020package io.jenetics;
021
022import io.jenetics.util.Factory;
023import io.jenetics.util.Self;
024import io.jenetics.util.Verifiable;
025
026/**
027 * Genes are the atoms of the <em>Jenetics</em> library. They contain the actual
028 * information (alleles) of the encoded solution. All implementations of the
029 * interface are final, immutable and can be only created via static
030 * factory methods which have the name {@code of}. When extending the library
031 * with own {@code Gene} implementations, it is recommended to also implement it
032 * as <a href="https://en.wikipedia.org/wiki/Value_object">value objects</a>.
033 *
034 * @implSpec
035 * <em>Jenetics</em> requires that the individuals ({@link Genotype} and
036 * {@link Phenotype}) are not changed after they have been created. Therefore,
037 * all implementations of the {@code Gene} interface must also be
038 * <em>immutable</em>.
039 *
040 * @see <a href="https://en.wikipedia.org/wiki/Value_object">Value object</a>
041 * @see Chromosome
042 *
043 * @param <A> the <a href="http://en.wikipedia.org/wiki/Allele">Allele</a> type
044 *         of this gene.
045 *
046 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
047 * @since 1.0
048 * @version 6.0
049 */
050public interface Gene<A, G extends Gene<A, G>>
051        extends
052                Self<G>,
053                Factory<G>,
054                Verifiable
055{
056
057        /**
058         * Return the allele of this gene.
059         *
060         * @return the allele of this gene.
061         */
062        A allele();
063
064        /**
065         * Return a new, random gene with the same type and with the same constraints
066         * as this gene. For all genes returned by this method holds
067         * {@code gene.getClass() == gene.newInstance().getClass()}. Implementations
068         * of this method have to use the {@link java.util.random.RandomGenerator}
069         * object which can be fetched from the {@link io.jenetics.util.RandomRegistry}.
070         */
071        @Override
072        G newInstance();
073
074        /**
075         * Create a new gene from the given {@code value} and the gene context.
076         *
077         * @since 2.0
078         * @param value the value of the new gene.
079         * @return a new gene with the given value.
080         */
081        G newInstance(final A value);
082
083}