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