BitGene.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.4.0).
003  * Copyright (c) 2007-2019 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 4.0
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 getBit() {
059         return _value;
060     }
061 
062     /**
063      * Return the {@code boolean} value of this gene.
064      *
065      @see #getAllele()
066      *
067      @return the {@code boolean} value of this gene.
068      */
069     public boolean booleanValue() {
070         return _value;
071     }
072 
073     @Override
074     public Boolean getAllele() {
075         return _value;
076     }
077 
078     /**
079      * Return always {@code true}.
080      *
081      @return always {@code true}.
082      */
083     @Override
084     public boolean isValid() {
085         return true;
086     }
087 
088     /**
089      * Create a new, <em>random</em> gene.
090      */
091     @Override
092     public BitGene newInstance() {
093         return RandomRegistry.getRandom().nextBoolean() ? TRUE : FALSE;
094     }
095 
096     /**
097      * Create a new gene from the given {@code value}..
098      *
099      @since 1.6
100      @param value the value of the new gene.
101      @return a new gene with the given value.
102      */
103     public BitGene newInstance(final Boolean value) {
104         return of(value);
105     }
106 
107     @Override
108     public String toString() {
109         return Boolean.toString(_value);
110     }
111 
112     /**
113      * Return the corresponding {@code BitGene} for the given {@code boolean}
114      * value.
115      *
116      @param value the value of the returned {@code BitGene}.
117      @return the {@code BitGene} for the given {@code boolean} value.
118      */
119     public static BitGene of(final boolean value) {
120         return value ? TRUE : FALSE;
121     }
122 
123 }