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