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