| 
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.io.Serializable;
 023
 024 import javax.xml.bind.annotation.XmlAccessType;
 025 import javax.xml.bind.annotation.XmlAccessorType;
 026 import javax.xml.bind.annotation.XmlRootElement;
 027 import javax.xml.bind.annotation.XmlType;
 028 import javax.xml.bind.annotation.XmlValue;
 029 import javax.xml.bind.annotation.adapters.XmlAdapter;
 030 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 031
 032 import org.jenetics.util.RandomRegistry;
 033
 034 /**
 035  * Implementation of a BitGene.
 036  *
 037  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 038  * @since 1.0
 039  * @version 2.0 — <em>$Date: 2014-12-28 $</em>
 040  */
 041 @XmlJavaTypeAdapter(BitGene.Model.Adapter.class)
 042 public enum BitGene
 043     implements
 044         Gene<Boolean, BitGene>,
 045         Comparable<BitGene>,
 046         Serializable
 047 {
 048
 049     FALSE(false),
 050     TRUE(true);
 051
 052     private static final long serialVersionUID = 3L;
 053
 054     public static final BitGene ZERO = FALSE;
 055     public static final BitGene ONE = TRUE;
 056
 057     private final boolean _value;
 058
 059     private BitGene(final boolean value) {
 060         _value = value;
 061     }
 062
 063     /**
 064      * Return the value of the BitGene.
 065      *
 066      * @return The value of the BitGene.
 067      */
 068     public final boolean getBit() {
 069         return _value;
 070     }
 071
 072     /**
 073      * Return the {@code boolean} value of this gene.
 074      *
 075      * @see #getAllele()
 076      *
 077      * @return the {@code boolean} value of this gene.
 078      */
 079     public boolean booleanValue() {
 080         return _value;
 081     }
 082
 083     @Override
 084     public Boolean getAllele() {
 085         return _value;
 086     }
 087
 088     /**
 089      * Return always {@code true}.
 090      *
 091      * @return always {@code true}.
 092      */
 093     @Override
 094     public boolean isValid() {
 095         return true;
 096     }
 097
 098     /**
 099      * Create a new, <em>random</em> gene.
 100      */
 101     @Override
 102     public BitGene newInstance() {
 103         return RandomRegistry.getRandom().nextBoolean() ? TRUE : FALSE;
 104     }
 105
 106     /**
 107      * Create a new gene from the given {@code value}..
 108      *
 109      * @since 1.6
 110      * @param value the value of the new gene.
 111      * @return a new gene with the given value.
 112      */
 113     public BitGene newInstance(final Boolean value) {
 114         return of(value);
 115     }
 116
 117     @Override
 118     public String toString() {
 119         return Boolean.toString(_value);
 120     }
 121
 122     /**
 123      * Return the corresponding {@code BitGene} for the given {@code boolean}
 124      * value.
 125      *
 126      * @param value the value of the returned {@code BitGene}.
 127      * @return the {@code BitGene} for the given {@code boolean} value.
 128      */
 129     public static BitGene of(final boolean value) {
 130         return value ? TRUE : FALSE;
 131     }
 132
 133
 134     /* *************************************************************************
 135      *  JAXB object serialization
 136      * ************************************************************************/
 137
 138     @XmlRootElement(name = "bit-gene")
 139     @XmlType(name = "org.jenetics.BitGene")
 140     @XmlAccessorType(XmlAccessType.FIELD)
 141     final static class Model {
 142
 143         @XmlValue
 144         public boolean value;
 145
 146         public final static class Adapter
 147             extends XmlAdapter<Model, BitGene>
 148         {
 149             @Override
 150             public Model marshal(final BitGene value) {
 151                 final Model m = new Model();
 152                 m.value = value.booleanValue();
 153                 return m;
 154             }
 155
 156             @Override
 157             public BitGene unmarshal(final Model m) {
 158                 return BitGene.of(m.value);
 159             }
 160         }
 161     }
 162
 163 }
 |