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