BitGene.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 javax.xml.bind.annotation.XmlAccessType;
023 import javax.xml.bind.annotation.XmlAccessorType;
024 import javax.xml.bind.annotation.XmlAttribute;
025 import javax.xml.bind.annotation.XmlRootElement;
026 import javax.xml.bind.annotation.XmlType;
027 import javax.xml.bind.annotation.adapters.XmlAdapter;
028 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
029 
030 import javolution.xml.XMLFormat;
031 import javolution.xml.XMLSerializable;
032 import javolution.xml.stream.XMLStreamException;
033 
034 import org.jenetics.internal.util.model.ModelType;
035 import org.jenetics.internal.util.model.ValueType;
036 
037 import org.jenetics.util.Function;
038 import org.jenetics.util.RandomRegistry;
039 
040 /**
041  * Implementation of a BitGene.
042  *
043  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
044  @since 1.0
045  @version 1.6 &mdash; <em>$Date: 2014-02-23 $</em>
046  */
047 @XmlJavaTypeAdapter(BitGene.Model.Adapter.class)
048 public enum BitGene
049     implements
050         Gene<Boolean, BitGene>,
051         Comparable<BitGene>,
052         XMLSerializable
053 {
054 
055     FALSE(false),
056     TRUE(true);
057 
058     private static final long serialVersionUID = 2L;
059 
060     public static final BitGene ZERO = FALSE;
061     public static final BitGene ONE = TRUE;
062 
063     private final boolean _value;
064 
065     private BitGene(final boolean value) {
066         _value = value;
067     }
068 
069     /**
070      * Return the value of the BitGene.
071      *
072      @return The value of the BitGene.
073      */
074     public final boolean getBit() {
075         return _value;
076     }
077 
078     /**
079      * Return the {@code boolean} value of this gene.
080      *
081      @see #getAllele()
082      *
083      @return the {@code boolean} value of this gene.
084      */
085     public boolean booleanValue() {
086         return _value;
087     }
088 
089     @Override
090     public Boolean getAllele() {
091         return _value;
092     }
093 
094     /**
095      * Return always {@code true}.
096      *
097      @return always {@code true}.
098      */
099     @Override
100     public boolean isValid() {
101         return true;
102     }
103 
104     @Deprecated
105     @Override
106     public BitGene copy() {
107         return this;
108     }
109 
110     /**
111      * Create a new, <em>random</em> gene.
112      */
113     @Override
114     public BitGene newInstance() {
115         return RandomRegistry.getRandom().nextBoolean() ? TRUE : FALSE;
116     }
117 
118     /**
119      * Create a new gene from the given {@code value}..
120      *
121      @since 1.6
122      @param value the value of the new gene.
123      @return a new gene with the given value.
124      */
125     public BitGene newInstance(final Boolean value) {
126         return of(value);
127     }
128 
129     @Override
130     public String toString() {
131         return Boolean.toString(_value);
132     }
133 
134     /**
135      * Return the corresponding {@code BitGene} for the given {@code boolean}
136      * value.
137      *
138      @param value the value of the returned {@code BitGene}.
139      @return the {@code BitGene} for the given {@code boolean} value.
140      *
141      @deprecated Use {@link #of(boolean)} instead.
142      */
143     @Deprecated
144     public static BitGene valueOf(final boolean value) {
145         return of(value);
146     }
147 
148     /**
149      * Return the corresponding {@code BitGene} for the given {@code boolean}
150      * value.
151      *
152      @param value the value of the returned {@code BitGene}.
153      @return the {@code BitGene} for the given {@code boolean} value.
154      */
155     public static BitGene of(final boolean value) {
156         return value ? TRUE : FALSE;
157     }
158 
159 
160     /* *************************************************************************
161      *  Property access methods methods
162      * ************************************************************************/
163 
164     /**
165      * Converter for accessing the allele from a given gene.
166      */
167     public static final Function<BitGene, Boolean> Allele =
168         new Function<BitGene, Boolean>() {
169             @Override public Boolean apply(final BitGene value) {
170                 return value._value;
171             }
172         };
173 
174 
175     /* *************************************************************************
176      *  XML object serialization
177      * ************************************************************************/
178 
179     static final XMLFormat<BitGene>
180     XML = new XMLFormat<BitGene>(BitGene.class)
181     {
182         private static final String VALUE = "value";
183 
184         @Override
185         public BitGene newInstance(final Class<BitGene> cls, final InputElement element)
186             throws XMLStreamException
187         {
188             final boolean value = element.getAttribute(VALUE, true);
189             return value ? BitGene.TRUE : BitGene.FALSE;
190         }
191         @Override
192         public void write(final BitGene gene, final OutputElement element)
193             throws XMLStreamException
194         {
195             element.setAttribute(VALUE, gene._value);
196         }
197         @Override
198         public void read(final InputElement element, final BitGene gene) {
199         }
200     };
201 
202     /* *************************************************************************
203      *  JAXB object serialization
204      * ************************************************************************/
205 
206     @XmlRootElement(name = "org.jenetics.BitGene")
207     @XmlType(name = "org.jenetics.BitGene")
208     @XmlAccessorType(XmlAccessType.FIELD)
209     final static class Model {
210 
211         @XmlAttribute
212         public boolean value;
213 
214         @ValueType(BitGene.class)
215         @ModelType(Model.class)
216         public final static class Adapter
217             extends XmlAdapter<Model, BitGene>
218         {
219             @Override
220             public Model marshal(final BitGene value) {
221                 final Model m = new Model();
222                 m.value = value.booleanValue();
223                 return m;
224             }
225 
226             @Override
227             public BitGene unmarshal(final Model m) {
228                 return BitGene.of(m.value);
229             }
230         }
231     }
232 
233 }