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