AbstractChromosome.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.6.0).
003  * Copyright (c) 2007-2014 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.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024 import static org.jenetics.internal.util.object.Verify;
025 import static org.jenetics.internal.util.object.eq;
026 import static org.jenetics.util.functions.Null;
027 
028 import java.util.Iterator;
029 import java.util.RandomAccess;
030 
031 import org.jenetics.internal.util.HashBuilder;
032 import org.jenetics.internal.util.cast;
033 
034 import org.jenetics.util.Function;
035 import org.jenetics.util.ISeq;
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 1.5 &mdash; <em>$Date: 2014-03-01 $</em>
047  */
048 public abstract class AbstractChromosome<G extends Gene<?, G>>
049     implements
050         Chromosome<G>,
051         RandomAccess
052 {
053     private static final long serialVersionUID = 1;
054 
055     /**
056      * Array of genes which forms the chromosome. This array must
057      * be initialized by the derived classes.
058      */
059     protected transient ISeq<G> _genes = null;
060 
061     /**
062      * Indicates whether this chromosome is valid or not. If the variable is
063      * {@code null} the validation state hasn't been calculated yet.
064      */
065     protected transient Boolean _valid = null;
066 
067     /**
068      * Create a new {@code AbstractChromosome} from the given {@code genes}
069      * array. The genes array is not copied, but sealed, so changes to the given
070      * genes array doesn't effect the genes of this chromosome.
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 array is
075      *          smaller than one.
076      */
077     protected AbstractChromosome(final ISeq<? extends G> genes) {
078         requireNonNull(genes, "Gene array");
079         assert (genes.indexWhere(Null== -1"Found at least on null gene.";
080 
081         if (genes.length() 1) {
082             throw new IllegalArgumentException(format(
083                 "Chromosome length < 1: %d", genes.length()
084             ));
085         }
086 
087         _genes = cast.apply(genes);
088     }
089 
090     @Override
091     public G getGene(final int index) {
092         return _genes.get(index);
093     }
094 
095     @Override
096     public G getGene() {
097         return _genes.get(0);
098     }
099 
100     @Override
101     public ISeq<G> toSeq() {
102         return _genes;
103     }
104 
105     @Override
106     public boolean isValid() {
107         if (_valid == null) {
108             _valid = _genes.forAll(Verify);
109         }
110 
111         return _valid;
112     }
113 
114     @Override
115     public Iterator<G> iterator() {
116         return _genes.iterator();
117     }
118 
119     @Override
120     public int length() {
121         return _genes.length();
122     }
123 
124     /**
125      * Return the index of the first occurrence of the given {@code gene}.
126      *
127      @param gene the {@link Gene} to search for.
128      @return the index of the searched gene, or -1 if the given gene was not
129      *         found.
130      */
131     protected int indexOf(final Object gene) {
132         return _genes.indexOf(gene);
133     }
134 
135     @Override
136     public int hashCode() {
137         return HashBuilder.of(getClass()).and(_genes).value();
138     }
139 
140     @Override
141     public boolean equals(final Object obj) {
142         if (obj == this) {
143             return true;
144         }
145         if (obj == null || getClass() != obj.getClass()) {
146             return false;
147         }
148 
149         final AbstractChromosome<?> chromosome = (AbstractChromosome<?>)obj;
150         return eq(_genes, chromosome._genes);
151     }
152 
153     @Override
154     public String toString() {
155         return _genes.toString();
156     }
157 
158 
159     /* *************************************************************************
160      *  Property access methods
161      * ************************************************************************/
162 
163     /**
164      * Return a {@link Function} which returns the first {@link Gene} from this
165      {@link Chromosome}.
166      */
167     static <G extends Gene<?, G>, C extends Chromosome<G>>
168     Function<C, G> gene() {
169         return new Function<C, G>() {
170             @Override public G apply(final C value) {
171                 return value.getGene();
172             }
173         };
174     }
175 
176     /**
177      * Return a {@link Function} which returns the {@link Gene} with the given
178      * {@code index} from this {@link Chromosome}.
179      */
180     static <G extends Gene<?, G>, C extends Chromosome<G>>
181     Function<C, G> gene(final int index) {
182         return new Function<C, G>() {
183             @Override public G apply(final C value) {
184                 return value.getGene(index);
185             }
186         };
187     }
188 
189     /**
190      * Return a {@link Function} which returns the gene array from this
191      {@link Chromosome}.
192      */
193     static <G extends Gene<?, G>, C extends Chromosome<G>>
194     Function<C, ISeq<G>> genes() {
195         return new Function<C, ISeq<G>>() {
196             @Override public ISeq<G> apply(final C value) {
197                 return value.toSeq();
198             }
199         };
200     }
201 
202 }