BitGene.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-6.0.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 6.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     BitGene(final boolean value) {
050         _value = value;
051     }
052 
053     /**
054      * Return the value of the BitGene.
055      *
056      @see #allele()
057      @see #booleanValue()
058      *
059      @return The value of the BitGene.
060      */
061     public final boolean bit() {
062         return _value;
063     }
064 
065     /**
066      * Return the {@code boolean} value of this gene.
067      *
068      @see #allele()
069      @see #bit()
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 allele() {
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     /**
093      * Create a new, <em>random</em> gene.
094      */
095     @Override
096     public BitGene newInstance() {
097         return RandomRegistry.random().nextBoolean() ? TRUE : FALSE;
098     }
099 
100     /**
101      * Create a new gene from the given {@code value}..
102      *
103      @since 1.6
104      @param value the value of the new gene.
105      @return a new gene with the given value.
106      */
107     public BitGene newInstance(final Boolean value) {
108         return of(value);
109     }
110 
111     @Override
112     public String toString() {
113         return Boolean.toString(_value);
114     }
115 
116     /**
117      * Return the corresponding {@code BitGene} for the given {@code boolean}
118      * value.
119      *
120      @param value the value of the returned {@code BitGene}.
121      @return the {@code BitGene} for the given {@code boolean} value.
122      */
123     public static BitGene of(final boolean value) {
124         return value ? TRUE : FALSE;
125     }
126 
127 }