BitGene.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.5.0).
003  * Copyright (c) 2007-2013 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 javolution.xml.XMLFormat;
023 import javolution.xml.XMLSerializable;
024 import javolution.xml.stream.XMLStreamException;
025 
026 import org.jenetics.util.Function;
027 import org.jenetics.util.RandomRegistry;
028 
029 /**
030  * Implementation of a BitGene.
031  *
032  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
033  @since 1.0
034  @version 1.4 &mdash; <em>$Date: 2013-09-01 $</em>
035  */
036 public enum BitGene
037     implements
038         Gene<Boolean, BitGene>,
039         Comparable<BitGene>,
040         XMLSerializable
041 {
042 
043     FALSE(false),
044     TRUE(true);
045 
046     private static final long serialVersionUID = 2L;
047 
048     public static final BitGene ZERO = FALSE;
049     public static final BitGene ONE = TRUE;
050 
051     private final boolean _value;
052 
053     private BitGene(final boolean value) {
054         _value = value;
055     }
056 
057     /**
058      * Return the value of the BitGene.
059      *
060      @return The value of the BitGene.
061      */
062     public final boolean getBit() {
063         return _value;
064     }
065 
066     /**
067      * Return the {@code boolean} value of this gene.
068      *
069      @see #getAllele()
070      *
071      @return the {@code boolean} value of this gene.
072      */
073     public boolean booleanValue() {
074         return _value;
075     }
076 
077     @Override
078     public Boolean getAllele() {
079         return _value;
080     }
081 
082     /**
083      * Return always {@code true}.
084      *
085      @return always {@code true}.
086      */
087     @Override
088     public boolean isValid() {
089         return true;
090     }
091 
092     @Override
093     public BitGene copy() {
094         return this;
095     }
096 
097     /**
098      * Create a new, <em>random</em> gene.
099      */
100     @Override
101     public BitGene newInstance() {
102         return RandomRegistry.getRandom().nextBoolean() ? TRUE : FALSE;
103     }
104 
105     @Override
106     public String toString() {
107         return Boolean.toString(_value);
108     }
109 
110     /**
111      * Return the corresponding {@code BitGene} for the given {@code boolean}
112      * value.
113      *
114      @param value the value of the returned {@code BitGene}.
115      @return the {@code BitGene} for the given {@code boolean} value.
116      */
117     public static BitGene valueOf(final boolean value) {
118         return value ? TRUE : FALSE;
119     }
120 
121 
122     /* *************************************************************************
123      *  Property access methods methods
124      * ************************************************************************/
125 
126     /**
127      * Converter for accessing the allele from a given gene.
128      */
129     public static final Function<BitGene, Boolean> Allele =
130         new Function<BitGene, Boolean>() {
131             @Override public Boolean apply(final BitGene value) {
132                 return value._value;
133             }
134         };
135 
136 
137     /* *************************************************************************
138      *  XML object serialization
139      * ************************************************************************/
140 
141     static final XMLFormat<BitGene>
142     XML = new XMLFormat<BitGene>(BitGene.class)
143     {
144         private static final String VALUE = "value";
145 
146         @Override
147         public BitGene newInstance(final Class<BitGene> cls, final InputElement element)
148             throws XMLStreamException
149         {
150             final boolean value = element.getAttribute(VALUE, true);
151             return value ? BitGene.TRUE : BitGene.FALSE;
152         }
153         @Override
154         public void write(final BitGene gene, final OutputElement element)
155             throws XMLStreamException
156         {
157             element.setAttribute(VALUE, gene._value);
158         }
159         @Override
160         public void read(final InputElement element, final BitGene gene) {
161         }
162     };
163 
164 }
165 
166