BitGene.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-5.2.0).
003  * Copyright (c) 2007-2020 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@gmail.com)
019  */
020 package io.jenetics;
021 
022 import io.jenetics.util.RandomRegistry;
023 
024 /**
025  * Implementation of a BitGene.
026  *
027  @see BitChromosome
028  *
029  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
030  @since 1.0
031  @version 5.2
032  */
033 public enum BitGene
034     implements
035         Gene<Boolean, BitGene>,
036         Comparable<BitGene>
037 {
038 
039     FALSE(false),
040     TRUE(true);
041 
042     private static final long serialVersionUID = 3L;
043 
044     public static final BitGene ZERO = FALSE;
045     public static final BitGene ONE = TRUE;
046 
047     private final boolean _value;
048 
049     private BitGene(final boolean value) {
050         _value = value;
051     }
052 
053     /**
054      * Return the value of the BitGene.
055      *
056      @return The value of the BitGene.
057      */
058     public final boolean bit() {
059         return _value;
060     }
061 
062     /**
063      * Return the value of the BitGene.
064      *
065      @return The value of the BitGene.
066      @deprecated Use {@link #bit()} instead
067      */
068     @Deprecated
069     public final boolean getBit() {
070         return _value;
071     }
072 
073     /**
074      * Return the {@code boolean} value of this gene.
075      *
076      @see #allele()
077      *
078      @return the {@code boolean} value of this gene.
079      */
080     public boolean booleanValue() {
081         return _value;
082     }
083 
084     @Deprecated
085     @Override
086     public Boolean getAllele() {
087         return _value;
088     }
089 
090     /**
091      * Return always {@code true}.
092      *
093      @return always {@code true}.
094      */
095     @Override
096     public boolean isValid() {
097         return true;
098     }
099 
100     /**
101      * Create a new, <em>random</em> gene.
102      */
103     @Override
104     public BitGene newInstance() {
105         return RandomRegistry.random().nextBoolean() ? TRUE : FALSE;
106     }
107 
108     /**
109      * Create a new gene from the given {@code value}..
110      *
111      @since 1.6
112      @param value the value of the new gene.
113      @return a new gene with the given value.
114      */
115     public BitGene newInstance(final Boolean value) {
116         return of(value);
117     }
118 
119     @Override
120     public String toString() {
121         return Boolean.toString(_value);
122     }
123 
124     /**
125      * Return the corresponding {@code BitGene} for the given {@code boolean}
126      * value.
127      *
128      @param value the value of the returned {@code BitGene}.
129      @return the {@code BitGene} for the given {@code boolean} value.
130      */
131     public static BitGene of(final boolean value) {
132         return value ? TRUE : FALSE;
133     }
134 
135 }