001/* 002 * Java Genetic Algorithm Library (jenetics-9.1.0). 003 * Copyright (c) 2007-2026 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 static java.util.Objects.requireNonNull; 023 024import java.util.Objects; 025 026import io.jenetics.util.ISeq; 027import io.jenetics.util.Verifiable; 028 029/** 030 * The abstract base implementation of the Chromosome interface. The implementors 031 * of this class must assure that the protected member {@code _genes} is not 032 * {@code null} and the length of the {@code genes} > 0. 033 * 034 * @param <G> the gene type. 035 * 036 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> 037 * @since 1.0 038 * @version 5.2 039 */ 040public abstract class AbstractChromosome<G extends Gene<?, G>> 041 implements Chromosome<G> 042{ 043 044 /** 045 * Array of genes which forms the chromosome. This array must 046 * be initialized by the derived classes. 047 */ 048 protected final transient ISeq<G> _genes; 049 050 /** 051 * Indicates whether this chromosome is valid or not. If the variable is 052 * {@code null} the validation state hasn't been calculated yet. 053 */ 054 protected transient Boolean _valid = null; 055 056 /** 057 * Create a new {@code AbstractChromosome} from the given {@code genes} 058 * array. 059 * 060 * @param genes the genes that form the chromosome. 061 * @throws NullPointerException if the given gene array is {@code null}. 062 * @throws IllegalArgumentException if the length of the gene sequence is 063 * empty. 064 */ 065 protected AbstractChromosome(final ISeq<? extends G> genes) { 066 requireNonNull(genes, "Gene array"); 067 assert genes.forAll(Objects::nonNull) : "Found at least on null gene."; 068 069 if (genes.isEmpty()) { 070 throw new IllegalArgumentException( 071 "The genes sequence must contain at least one gene." 072 ); 073 } 074 075 _genes = ISeq.upcast(genes); 076 } 077 078 @Override 079 public G get(final int index) { 080 return _genes.get(index); 081 } 082 083 @Override 084 public int length() { 085 return _genes.length(); 086 } 087 088 @Override 089 public boolean isValid() { 090 if (_valid == null) { 091 _valid = _genes.forAll(Verifiable::isValid); 092 } 093 return _valid; 094 } 095 096 @Override 097 public int hashCode() { 098 return _genes.hashCode(); 099 } 100 101 @Override 102 public boolean equals(final Object obj) { 103 return obj != null && 104 getClass() == obj.getClass() && 105 Objects.equals(_genes, ((AbstractChromosome<?>)obj)._genes); 106 } 107 108 @Override 109 public String toString() { 110 return Objects.toString(_genes); 111 } 112 113}