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