001/* 002 * Java Genetic Algorithm Library (jenetics-7.2.0). 003 * Copyright (c) 2007-2023 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.BaseSeq; 023import io.jenetics.util.Factory; 024import io.jenetics.util.ISeq; 025import 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 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 6.0 044 */ 045public interface Chromosome<G extends Gene<?, G>> 046 extends 047 BaseSeq<G>, 048 Factory<Chromosome<G>>, 049 Verifiable 050{ 051 052 /** 053 * Return the first gene of this chromosome. Each chromosome must contain 054 * at least one gene. 055 * 056 * @since 5.2 057 * 058 * @return the first gene of this chromosome. 059 */ 060 default G gene() { 061 return get(0); 062 } 063 064 @Override 065 default boolean isValid() { 066 return stream().allMatch(Gene::isValid); 067 } 068 069 /** 070 * A factory method which creates a new {@link Chromosome} of specific type 071 * and the given {@code genes}. 072 * 073 * @param genes the genes of the new chromosome. The given genes array is 074 * not copied. 075 * @return A new {@link Chromosome} of the same type with the given genes. 076 * @throws NullPointerException if the given {@code gene}s are {@code null}. 077 * @throws IllegalArgumentException if the length of the given gene sequence 078 * is smaller than one. 079 */ 080 Chromosome<G> newInstance(final ISeq<G> genes); 081 082 /** 083 * Casts this {@code Chromosome} to an instance of type {@code C}. 084 * This is a convenient method for an ordinary cast and allows seamless 085 * method-chaining. Instead of 086 * <pre>{@code 087 * final Genotype<BitGene> gt = ... 088 * final int count = ((BitChromosome)gt.chromosome()).bitCount() 089 * }</pre> 090 * you can write 091 * <pre>{@code 092 * final Genotype<BitGene> gt = ... 093 * final int count = gt.chromosome() 094 * .as(BitChromosome.class) 095 * .bitCount() 096 * }</pre> 097 * This may lead to a more elegant programming style in some cases. 098 * 099 * @since 3.7 100 * 101 * @param type the target type class 102 * @param <C> the target chromosome type 103 * @return this chromosome cast as {@code C} 104 * @throws NullPointerException if the target type class is {@code null} 105 * @throws ClassCastException if this chromosome can't be cast to a 106 * chromosome of type {@code C} 107 */ 108 default <C extends Chromosome<G>> C as(final Class<C> type) { 109 return type.cast(this); 110 } 111 112}