AbstractBoundedChromosome.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.3.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 io.jenetics.internal.util.Hashes.hash;
023 
024 import java.io.Serializable;
025 import java.util.Objects;
026 
027 import io.jenetics.util.ISeq;
028 import io.jenetics.util.IntRange;
029 
030 /**
031  * Abstract chromosome for {@code BoundedGene}s.
032  *
033  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
034  @version 1.6
035  @since 1.6
036  */
037 abstract class AbstractBoundedChromosome<
038     extends Comparable<? super A>,
039     extends BoundedGene<A, G>
040 >
041     extends VariableChromosome<G>
042     implements BoundedChromosome<A, G>, Serializable
043 {
044 
045     private static final long serialVersionUID = 1L;
046 
047     /**
048      * The minimum value of this {@code BoundedChromosome}.
049      */
050     final A _min;
051 
052     /**
053      * The maximum value of this {@code BoundedChromosome}.
054      */
055     final A _max;
056 
057     /**
058      * Create a new chromosome from the given genes array.
059      *
060      @param genes the genes of the new chromosome.
061      @throws IllegalArgumentException if the length of the gene sequence is
062      *         empty or doesn't match with the allowed length range.
063      @throws IllegalArgumentException if the minimum or maximum of the range
064      *         is smaller or equal zero
065      @throws IllegalArgumentException if the given range size is zero
066      @throws NullPointerException if the {@code genes} are {@code null}.
067      */
068     protected AbstractBoundedChromosome(
069         final ISeq<? extends G> genes,
070         final IntRange lengthRange
071     ) {
072         super(genes, lengthRange);
073         _min = genes.get(0).getMin();
074         _max = genes.get(0).getMax();
075     }
076 
077     @Override
078     public A getMin() {
079         return _min;
080     }
081 
082     @Override
083     public A getMax() {
084         return _max;
085     }
086 
087     @Override
088     public int hashCode() {
089         return
090             hash(super.hashCode(),
091             hash(_min,
092             hash(_max)));
093     }
094 
095     @Override
096     public boolean equals(final Object obj) {
097         return obj == this ||
098             obj != null &&
099             getClass() == obj.getClass() &&
100             Objects.equals(_min, ((AbstractBoundedChromosome)obj)._min&&
101             Objects.equals(_max, ((AbstractBoundedChromosome)obj)._max&&
102             super.equals(obj);
103     }
104 
105 }