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