AbstractBoundedChromosome.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-1.6.0).
03  * Copyright (c) 2007-2014 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
19  */
20 package org.jenetics;
21 
22 import static org.jenetics.internal.util.object.eq;
23 
24 import org.jenetics.internal.util.HashBuilder;
25 
26 import org.jenetics.util.ISeq;
27 
28 /**
29  * Abstract chromosome for {@code BoundedGene}s.
30  *
31  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
32  @version 1.6 &mdash; <em>$Date: 2014-03-05 $</em>
33  @since 1.6
34  */
35 abstract class AbstractBoundedChromosome<
36     extends Comparable<? super A>,
37     extends AbstractBoundedGene<A, G>
38 >
39     extends AbstractChromosome<G> implements BoundedChromosome<A, G> {
40 
41     private static final long serialVersionUID = 1L;
42 
43     /**
44      * The minimum value of this {@code BoundedChromosome}.
45      */
46     A _min;
47 
48     /**
49      * The maximum value of this {@code BoundedChromosome}.
50      */
51     A _max;
52 
53     /**
54      * Create a new chromosome from the given genes array.
55      *
56      @param genes the genes of the new chromosome.
57      @throws IllegalArgumentException if the {@code genes.length()} is smaller
58      *         than one.
59      @throws NullPointerException if the {@code genes} are {@code null}.
60      */
61     protected AbstractBoundedChromosome(final ISeq<? extends G> genes) {
62         super(genes);
63         _min = genes.get(0)._min;
64         _max = genes.get(0)._max;
65     }
66 
67     @Override
68     public A getMin() {
69         return _min;
70     }
71 
72     @Override
73     public A getMax() {
74         return _max;
75     }
76 
77     @Override
78     public int hashCode() {
79         return HashBuilder.of(getClass()).
80             and(super.hashCode()).
81             and(_min).
82             and(_max).value();
83     }
84 
85     @Override
86     public boolean equals(final Object object) {
87         if (object == this) {
88             return true;
89         }
90         if (!(object instanceof AbstractBoundedChromosome<?, ?>)) {
91             return false;
92         }
93 
94         final AbstractBoundedChromosome<?, ?> nc = (AbstractBoundedChromosome<?, ?>)object;
95         return eq(_min, nc._min&& eq(_max, nc._max&& super.equals(object);
96     }
97 
98 }