001 /*
002 * Java Genetic Algorithm Library (jenetics-4.2.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.Equality.eq;
023
024 import java.io.Serializable;
025
026 import io.jenetics.internal.util.Equality;
027 import io.jenetics.internal.util.Hash;
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 A extends Comparable<? super A>,
040 G extends AbstractBoundedGene<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)._min;
075 _max = genes.get(0)._max;
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 Hash.of(getClass())
091 .and(super.hashCode())
092 .and(_min)
093 .and(_max).value();
094 }
095
096 @Override
097 public boolean equals(final Object object) {
098 return Equality.of(this, object).test(nc ->
099 eq(_min, nc._min) &&
100 eq(_max, nc._max) &&
101 super.equals(object)
102 );
103 }
104
105 }
|