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