001 /*
002 * Java Genetic Algorithm Library (jenetics-3.0.0).
003 * Copyright (c) 2007-2014 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@gmx.at)
019 */
020 package org.jenetics;
021
022 import static java.util.Objects.requireNonNull;
023 import static org.jenetics.internal.util.Equality.eq;
024
025 import org.jenetics.internal.util.Equality;
026 import org.jenetics.internal.util.Hash;
027
028 /**
029 * Base class for genes where the alleles are bound by a minimum and a maximum
030 * value.
031 *
032 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
033 * @version 1.6 — <em>$Date: 2014-12-28 $</em>
034 * @since 1.6
035 */
036 abstract class AbstractBoundedGene<
037 A extends Comparable<? super A>,
038 G extends AbstractBoundedGene<A, G>
039 >
040 implements BoundedGene<A, G>
041 {
042
043 private static final long serialVersionUID = 1L;
044
045 /**
046 * The minimum value of this {@code BoundedGene}.
047 */
048 final A _min;
049
050 /**
051 * The maximum value of this {@code BoundedGene}.
052 */
053 final A _max;
054
055 /**
056 * The value of this {@code BoundedGene}.
057 */
058 final A _value;
059
060 // Holds the valid state of the gene.
061 private final boolean _valid;
062
063 /**
064 * Create new {@code BoundedGene}.
065 *
066 * @param value The value of the gene.
067 * @param min The allowed min value of the gene.
068 * @param max The allows max value of the gene.
069 * @throws NullPointerException if one of the given arguments is {@code null}.
070 */
071 protected AbstractBoundedGene(
072 final A value,
073 final A min,
074 final A max
075 ) {
076 _min = requireNonNull(min, "Min value not be null.");
077 _max = requireNonNull(max, "Max value must not be null.");
078 _value = requireNonNull(value, "Gene value must not be null.");
079 _valid = _value.compareTo(min) >= 0 && _value.compareTo(max) <= 0;
080 }
081
082 @Override
083 public A getAllele() {
084 return _value;
085 }
086
087 @Override
088 public A getMin() {
089 return _min;
090 }
091
092 @Override
093 public A getMax() {
094 return _max;
095 }
096
097 @Override
098 public boolean isValid() {
099 return _valid;
100 }
101
102 @Override
103 public int hashCode() {
104 return Hash.of(getClass())
105 .and(_value)
106 .and(_min)
107 .and(_max).value();
108 }
109
110 @Override
111 public boolean equals(final Object obj) {
112 return Equality.of(this, obj).test(gene ->
113 eq(_value, gene._value) &&
114 eq(_min, gene._min) &&
115 eq(_max, gene._max)
116 );
117 }
118
119 @Override
120 public String toString() {
121 return String.format("[%s]", _value);
122 }
123 }
|