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