AbstractBoundedChromosome.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-3.0.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.Equality.eq;
23 
24 import org.jenetics.internal.util.Equality;
25 import org.jenetics.internal.util.Hash;
26 
27 import org.jenetics.util.ISeq;
28 
29 /**
30  * Abstract chromosome for {@code BoundedGene}s.
31  *
32  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
33  @version 1.6 &mdash; <em>$Date: 2014-12-28 $</em>
34  @since 1.6
35  */
36 abstract class AbstractBoundedChromosome<
37     extends Comparable<? super A>,
38     extends AbstractBoundedGene<A, G>
39 >
40     extends AbstractChromosome<G>
41     implements BoundedChromosome<A, G>
42 {
43 
44     private static final long serialVersionUID = 1L;
45 
46     /**
47      * The minimum value of this {@code BoundedChromosome}.
48      */
49     A _min;
50 
51     /**
52      * The maximum value of this {@code BoundedChromosome}.
53      */
54     A _max;
55 
56     /**
57      * Create a new chromosome from the given genes array.
58      *
59      @param genes the genes of the new chromosome.
60      @throws IllegalArgumentException if the {@code genes.length()} is smaller
61      *         than one.
62      @throws NullPointerException if the {@code genes} are {@code null}.
63      */
64     protected AbstractBoundedChromosome(final ISeq<? extends G> genes) {
65         super(genes);
66         _min = genes.get(0)._min;
67         _max = genes.get(0)._max;
68     }
69 
70     @Override
71     public A getMin() {
72         return _min;
73     }
74 
75     @Override
76     public A getMax() {
77         return _max;
78     }
79 
80     @Override
81     public int hashCode() {
82         return Hash.of(getClass())
83             .and(super.hashCode())
84             .and(_min)
85             .and(_max).value();
86     }
87 
88     @Override
89     public boolean equals(final Object object) {
90         return Equality.of(this, object).test(nc ->
91             eq(_min, nc._min&&
92             eq(_max, nc._max&&
93             super.equals(object)
94         );
95     }
96 
97 }