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