AbstractBoundedGene.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.1.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 java.util.Objects.requireNonNull;
023 
024 import java.io.Serializable;
025 import java.util.Objects;
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@gmail.com">Franz Wilhelmstötter</a>
032  @version 1.6
033  @since 1.6
034  */
035 abstract class AbstractBoundedGene<
036     extends Comparable<? super A>,
037     extends AbstractBoundedGene<A, G>
038 >
039     implements BoundedGene<A, G>, Serializable
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     /**
060      * Create new {@code BoundedGene}.
061      *
062      @param value The value of the gene.
063      @param min The allowed min value of the gene.
064      @param max The allows max value of the gene.
065      @throws NullPointerException if one of the given arguments is {@code null}.
066      */
067     protected AbstractBoundedGene(
068         final A value,
069         final A min,
070         final A max
071     ) {
072         _min = requireNonNull(min, "Min value not be null.");
073         _max = requireNonNull(max, "Max value must not be null.");
074         _value = requireNonNull(value, "Gene value must not be null.");
075     }
076 
077     @Override
078     public A getAllele() {
079         return _value;
080     }
081 
082     @Override
083     public A getMin() {
084         return _min;
085     }
086 
087     @Override
088     public A getMax() {
089         return _max;
090     }
091 
092     @Override
093     public boolean isValid() {
094         return _value.compareTo(_min>= && _value.compareTo(_max<= 0;
095     }
096 
097     @Override
098     public int hashCode() {
099         int hash = 17;
100         hash += 31*Objects.hashCode(_value37;
101         hash += 31*Objects.hashCode(_min37;
102         hash += 31*Objects.hashCode(_max37;
103         return hash;
104     }
105 
106     @Override
107     public boolean equals(final Object obj) {
108         return obj == this ||
109             obj instanceof AbstractBoundedGene<?, ?> &&
110             Objects.equals(((AbstractBoundedGene)obj)._value, _value&&
111             Objects.equals(((AbstractBoundedGene)obj)._min, _min&&
112             Objects.equals(((AbstractBoundedGene)obj)._max, _max);
113     }
114 
115     @Override
116     public String toString() {
117         return String.format("[%s]", _value);
118     }
119 }