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