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