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