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