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