LongGene.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.8.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 org.jenetics.internal.math.random.nextLong;
023 import static org.jenetics.util.RandomRegistry.getRandom;
024 
025 import java.io.Serializable;
026 import java.util.Random;
027 
028 import javax.xml.bind.annotation.XmlAccessType;
029 import javax.xml.bind.annotation.XmlAccessorType;
030 import javax.xml.bind.annotation.XmlAttribute;
031 import javax.xml.bind.annotation.XmlRootElement;
032 import javax.xml.bind.annotation.XmlType;
033 import javax.xml.bind.annotation.XmlValue;
034 import javax.xml.bind.annotation.adapters.XmlAdapter;
035 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
036 
037 import org.jenetics.internal.util.require;
038 
039 import org.jenetics.util.ISeq;
040 import org.jenetics.util.LongRange;
041 import org.jenetics.util.MSeq;
042 import org.jenetics.util.Mean;
043 
044 /**
045  * NumericGene implementation which holds a 64 bit integer number.
046  *
047  <p>This is a <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html">
048  * value-based</a> class; use of identity-sensitive operations (including
049  * reference equality ({@code ==}), identity hash code, or synchronization) on
050  * instances of {@code LongGene} may have unpredictable results and should
051  * be avoided.
052  *
053  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
054  @since 1.6
055  @version 3.2
056  */
057 @XmlJavaTypeAdapter(LongGene.Model.Adapter.class)
058 public final class LongGene
059     extends AbstractNumericGene<Long, LongGene>
060     implements
061         NumericGene<Long, LongGene>,
062         Mean<LongGene>,
063         Comparable<LongGene>,
064         Serializable
065 {
066 
067     private static final long serialVersionUID = 1L;
068 
069     /**
070      * Create a new random {@code LongGene} with the given value and the
071      * given range. If the {@code value} isn't within the interval [min, max],
072      * no exception is thrown. In this case the method
073      {@link LongGene#isValid()} returns {@code false}.
074      *
075      @param value the value of the gene.
076      @param min the minimal valid value of this gene (inclusively).
077      @param max the maximal valid value of this gene (inclusively).
078      @throws NullPointerException if one of the arguments is {@code null}.
079      */
080     LongGene(final Long value, final Long min, final Long max) {
081         super(value, min, max);
082     }
083 
084     @Override
085     public int compareTo(final LongGene other) {
086         return _value.compareTo(other._value);
087     }
088 
089     /**
090      * Create a new random {@code LongGene} with the given value and the
091      * given range. If the {@code value} isn't within the interval [min, max],
092      * no exception is thrown. In this case the method
093      {@link LongGene#isValid()} returns {@code false}.
094      *
095      @param value the value of the gene.
096      @param min the minimal valid value of this gene (inclusively).
097      @param max the maximal valid value of this gene (inclusively).
098      @return a new {@code LongGene} with the given parameters.
099      */
100     public static LongGene of(final long value, final long min, final long max) {
101         return new LongGene(value, min, max);
102     }
103 
104     /**
105      * Create a new random {@code LongGene} with the given value and the
106      * given range. If the {@code value} isn't within the interval [min, max],
107      * no exception is thrown. In this case the method
108      {@link LongGene#isValid()} returns {@code false}.
109      *
110      @since 3.2
111      *
112      @param value the value of the gene.
113      @param range the long range to use
114      @return a new random {@code LongGene}
115      @throws NullPointerException if the given {@code range} is {@code null}.
116      */
117     public static LongGene of(final long value, final LongRange range) {
118         return new LongGene(value, range.getMin(), range.getMax());
119     }
120 
121     /**
122      * Create a new random {@code LongGene}. It is guaranteed that the value of
123      * the {@code LongGene} lies in the interval [min, max].
124      *
125      @param min the minimal valid value of this gene (inclusively).
126      @param max the maximal valid value of this gene (inclusively).
127      @return a new {@code LongGene} with the given parameters.
128      */
129     public static LongGene of(final long min, final long max) {
130         return of(nextLong(getRandom(), min, max), min, max);
131     }
132 
133     /**
134      * Create a new random {@code LongGene}. It is guaranteed that the value of
135      * the {@code LongGene} lies in the interval [min, max].
136      *
137      @since 3.2
138      *
139      @param range the long range to use
140      @return a new random {@code LongGene}
141      @throws NullPointerException if the given {@code range} is {@code null}.
142      */
143     public static LongGene of(final LongRange range) {
144         return of(nextLong(getRandom(), range.getMin(), range.getMax()), range);
145     }
146 
147     static ISeq<LongGene> seq(
148         final Long minimum,
149         final Long maximum,
150         final int length
151     ) {
152         require.positive(length);
153 
154         final long min = minimum;
155         final long max = maximum;
156         final Random r = getRandom();
157 
158         return MSeq.<LongGene>ofLength(length)
159             .fill(() -> new LongGene(nextLong(r, min, max), minimum, maximum))
160             .toISeq();
161     }
162 
163     @Override
164     public LongGene newInstance(final Number number) {
165         return new LongGene(number.longValue(), _min, _max);
166     }
167 
168     @Override
169     public LongGene newInstance() {
170         return new LongGene(
171             nextLong(getRandom(), _min, _max), _min, _max
172         );
173     }
174 
175     @Override
176     public LongGene mean(final LongGene that) {
177         return new LongGene(_value + (that._value - _value)/2, _min, _max);
178     }
179 
180     /* *************************************************************************
181      *  JAXB object serialization
182      * ************************************************************************/
183 
184     @XmlRootElement(name = "long-gene")
185     @XmlType(name = "org.jenetics.LongGene")
186     @XmlAccessorType(XmlAccessType.FIELD)
187     final static class Model {
188 
189         @XmlAttribute(name = "min", required = true)
190         public long min;
191 
192         @XmlAttribute(name = "max", required = true)
193         public long max;
194 
195         @XmlValue
196         public long value;
197 
198         public final static class Adapter
199             extends XmlAdapter<Model, LongGene>
200         {
201             @Override
202             public Model marshal(final LongGene value) {
203                 final Model m = new Model();
204                 m.min = value.getMin();
205                 m.max = value.getMax();
206                 m.value = value.getAllele();
207                 return m;
208             }
209 
210             @Override
211             public LongGene unmarshal(final Model m) {
212                 return LongGene.of(m.value, m.min, m.max);
213             }
214         }
215     }
216 
217 }