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