NumberGene.java
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 javolution.text.Text;
026 import javolution.text.TextBuilder;
027 import javolution.xml.XMLSerializable;
028 
029 import org.jscience.mathematics.number.Number;
030 
031 import org.jenetics.internal.util.HashBuilder;
032 
033 import org.jenetics.util.Mean;
034 
035 /**
036  * Abstract base class for implementing concrete NumberGenes.
037  *
038  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
039  @since 1.0
040  @version 1.2 &mdash; <em>$Date: 2014-03-05 $</em>
041  *
042  @deprecated Use {@link AbstractNumericGene} instead. This classes
043  *             uses the <i>JScience</i> library, which will be removed in the
044  *             next major version.
045  */
046 @Deprecated
047 public abstract class NumberGene<
048     extends Number<N>,
049     extends NumberGene<N, G>
050 >
051     extends Number<G>
052     implements
053         NumericGene<N, G>,
054         Mean<G>,
055         XMLSerializable
056 {
057     private static final long serialVersionUID = 1L;
058 
059     /**
060      * The minimum value of this {@code NumberGene}. This field is marked
061      * as transient and must serialized manually by sub classes.
062      */
063     protected transient N _min;
064 
065     /**
066      * The maximum value of this {@code NumberGene}. This field is marked
067      * as transient and must serialized manually by sub classes.
068      */
069     protected transient N _max;
070 
071     /**
072      * The value of this {@code NumberGene}. This field is marked
073      * as transient and must serialized manually by sub classes.
074      */
075     protected transient N _value;
076 
077     private transient boolean _valid = true;
078 
079     protected NumberGene() {
080     }
081 
082     /**
083      * Boxes a given Java number into the required number object.
084      *
085      @param value the Java number to box.
086      @return the boxed number.
087      */
088     protected abstract N box(final java.lang.Number value);
089 
090     /**
091      * Create a new gene from the given {@code value}.
092      *
093      @param value the value of the new gene.
094      @return a new gene with the given value.
095      */
096     public abstract G newInstance(final N value);
097 
098     @Override
099     public G copy() {
100         return newInstance(_value);
101     }
102 
103     /**
104      * Create a new NumberGene with the same limits and the given value.
105      *
106      @param value The value of the new NumberGene.
107      @return The new NumberGene.
108      @throws NullPointerException if the given {@code value} is {@code null}.
109      */
110     public G newInstance(final java.lang.Number value) {
111         return newInstance(box(value));
112     }
113 
114     /**
115      * Set the {@code NumberGene}.
116      *
117      @param value The value of the number gene.
118      @param min The allowed min value of the gene.
119      @param max The allows max value of the gene.
120      @throws NullPointerException if one of the given number is null.
121      */
122     protected void set(final N value, final N min, final N max) {
123         _min = requireNonNull(min, "Min value");
124         _max = requireNonNull(max, "Max value");
125         _value = requireNonNull(value, "Gene value");
126         _valid = _value.compareTo(_min>= && _value.compareTo(_max<= 0;
127     }
128 
129     /**
130      * Test whether this is a valid NumberGene and its value is within the
131      * interval closed interval [min, max].
132      *
133      @return if this gene is valid, which means the gene value is within the
134      *          closed interval [min, max].
135      */
136     @Override
137     public boolean isValid() {
138         return _valid;
139     }
140 
141     /**
142      * Return the number value of this gene.
143      *
144      @return the number value of this gene.
145      */
146     public N getNumber() {
147         return _value;
148     }
149 
150     @Override
151     public N getAllele() {
152         return _value;
153     }
154 
155     /**
156      * Return the allowed min value.
157      *
158      @return The allowed min value.
159      */
160     public N getMin() {
161         return _min;
162     }
163 
164     /**
165      * Return the allowed max value.
166      *
167      @return The allowed max value.
168      */
169     public N getMax() {
170         return _max;
171     }
172 
173      @Override
174     public double doubleValue() {
175         return _value.doubleValue();
176      }
177 
178      @Override
179     public long longValue() {
180         return _value.longValue();
181      }
182 
183     @Override
184     public boolean isLargerThan(final G that) {
185         return _value.isLargerThan(that._value);
186     }
187 
188     @Override
189     public G plus(final G that) {
190         return newInstance(_value.plus(that._value));
191     }
192 
193     @Override
194     public G opposite() {
195         return newInstance(_value.opposite());
196     }
197 
198     @Override
199     public G times(final G that) {
200         return newInstance(_value.times(that._value));
201     }
202 
203     /**
204      * Remind that this method is not consistent with the {@link #equals(Object)}
205      * method. Since this method only compares the {@code value} and the
206      * {@code equals} method also takes the {@code min} and {@code max} value
207      * into account.
208      * [code]
209      * final NumberGene〈?, ?〉 ng1 = ...
210      * final NumberGene〈?, ?〉 ng2 = ...
211      *
212      * if (ng1.equals(ng2) {
213      *     // Holds for every ng1 and ng2.
214      *     assert(ng1.compareTo(ng2) == 0);
215      * }
216      * if (ng1.compareTo(ng2) == 0) {
217      *     // Doesn't hold for every ng1 and ng2.
218      *     assert(ng1.equals(ng2));
219      * }
220      * [/code]
221      */
222     @Override
223     public int compareTo(final G that) {
224         return _value.compareTo(that._value);
225     }
226 
227     @Override
228     public int hashCode() {
229         return HashBuilder.of(getClass()).and(_value).and(_min).and(_max).value();
230     }
231 
232     @Override
233     public boolean equals(final Object obj) {
234         if (obj == this) {
235             return true;
236         }
237         if (obj == null || obj.getClass() != getClass()) {
238             return false;
239         }
240 
241         final NumberGene<?, ?> gene = (NumberGene<?, ?>)obj;
242         return eq(_value, gene._value&&
243                 eq(_min, gene._min&&
244                 eq(_max, gene._max);
245     }
246 
247     @Override
248     public Text toText() {
249         TextBuilder out = new TextBuilder();
250         out.append("[").append(_value).append("]");
251         return out.toText();
252     }
253 
254 }