001 /*
002 * Java Genetic Algorithm Library (jenetics-5.2.0).
003 * Copyright (c) 2007-2020 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 */
020 package io.jenetics;
021
022 import io.jenetics.util.BaseSeq;
023 import io.jenetics.util.Factory;
024 import io.jenetics.util.ISeq;
025 import io.jenetics.util.Verifiable;
026
027 /**
028 * A chromosome consists of one or more genes. It also provides a factory
029 * method for creating new, random chromosome instances of the same type and the
030 * same constraint.
031 *
032 * @implSpec
033 * Implementations of the {@code Chromosome} interface must be <em>immutable</em>
034 * and guarantee an efficient random access ({@code O(1)}) to the genes. A
035 * {@code Chromosome} must contains at least one {@code Gene}.
036 *
037 * @see <a href="http://en.wikipedia.org/wiki/Chromosome">Wikipedia: Chromosome</a>
038 * @see Genotype
039 * @see Gene
040 *
041 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
042 * @since 1.0
043 * @version 5.2
044 */
045 public interface Chromosome<G extends Gene<?, G>>
046 extends
047 BaseSeq<G>,
048 Factory<Chromosome<G>>,
049 Verifiable
050 {
051
052 @Override
053 default G get(final int index) {
054 return getGene(index);
055 }
056
057 /**
058 * Return the first gene of this chromosome. Each chromosome must contain
059 * at least one gene.
060 *
061 * @since 5.2
062 *
063 * @return the first gene of this chromosome.
064 */
065 default G gene() {
066 return get(0);
067 }
068
069 /**
070 * Return the gene on the specified index.
071 *
072 * @param index The gene index.
073 * @return the wanted gene.
074 * @throws IndexOutOfBoundsException if the index is out of range
075 * (index < 1 || index >= length()).
076 * @deprecated Use {@link #get(int)} instead. Will be removed.
077 */
078 @Deprecated
079 G getGene(final int index);
080
081 /**
082 * Return the first gene of this chromosome. Each chromosome must contain
083 * at least one gene.
084 *
085 * @return the first gene of this chromosome.
086 * @deprecated Use {@link #gene()} instead
087 */
088 @Deprecated
089 default G getGene() {
090 return get(0);
091 }
092
093 /**
094 * A factory method which creates a new {@link Chromosome} of specific type
095 * and the given {@code genes}.
096 *
097 * @param genes the genes of the new chromosome. The given genes array is
098 * not copied.
099 * @return A new {@link Chromosome} of the same type with the given genes.
100 * @throws NullPointerException if the given {@code gene}s are {@code null}.
101 * @throws IllegalArgumentException if the length of the given gene sequence
102 * is smaller than one.
103 */
104 Chromosome<G> newInstance(final ISeq<G> genes);
105
106 /**
107 * Return an unmodifiable sequence of the genes of this chromosome.
108 *
109 * @return an immutable gene sequence.
110 * @deprecated Since the chromosome itself extends the {@link BaseSeq}, it
111 * is no longer necessary to get a sequence with genes. If it is
112 * necessary to create an {@link ISeq} from a chromosome, use
113 * {@code ISeq.of(chromosome)} instead. This method will be
114 * removed in the next major release.
115 */
116 @Deprecated
117 ISeq<G> toSeq();
118
119 /**
120 * Casts this {@code Chromosome} to an instance of type {@code C}.
121 * This is a convenient method for an ordinary cast and allows seamless
122 * method-chaining. Instead of
123 * <pre>{@code
124 * final Genotype<BitGene> gt = ...
125 * final int count = ((BitChromosome)gt.chromosome()).bitCount()
126 * }</pre>
127 * you can write
128 * <pre>{@code
129 * final Genotype<BitGene> gt = ...
130 * final int count = gt.chromosome()
131 * .as(BitChromosome.class)
132 * .bitCount()
133 * }</pre>
134 * This may lead to a more elegant programming style in some cases.
135 *
136 * @since 3.7
137 *
138 * @param type the target type class
139 * @param <C> the target chromosome type
140 * @return this chromosome casted as {@code C}
141 * @throws NullPointerException if the target type class is {@code null}
142 * @throws ClassCastException if this chromosome can't be casted to a
143 * chromosome of type {@code C}
144 */
145 default <C extends Chromosome<G>> C as(final Class<C> type) {
146 return type.cast(this);
147 }
148
149 }
|