/**
 * Computational Intelligence Library (CIlib) Copyright (C) 2003 - 2010
 * Computational Intelligence Research Group (CIRG@UP) Department of Computer
 * Science University of Pretoria South Africa
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
package net.sourceforge.cilib.functions.discrete;

import java.math.BigInteger;
import net.sourceforge.cilib.functions.DiscreteFunction;
import net.sourceforge.cilib.math.random.generator.MersenneTwister;
import net.sourceforge.cilib.math.random.generator.RandomAdaptor;
import net.sourceforge.cilib.type.types.container.Vector;

/**
 * Discrete function to match the given bit string or a randomly generated bit
 * string.
 *
 */
public class BitStringMatcher implements DiscreteFunction {

    private static final long serialVersionUID = 7535776840908399415L;
    private String targetRandomString;
    private int numberOfBits;

    /**
     * Constructor.
     */
    public BitStringMatcher() {
    }

    /**
     * Get the target random bit string to match.
     *
     * @return The target random bit string
     */
    public String getTargetRandomString() {
        return this.targetRandomString;
    }

    /**
     * Set the target random bit string to match.
     *
     * @param target The target random bit string to set
     */
    public void setTargetRandomString(String target) {
        this.targetRandomString = target;
    }

    /**
     * Get the number of bits in the bit string that must be matched.
     *
     * @return The number of bits within the bit string
     */
    public int getNumberOfBits() {
        return this.numberOfBits;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer apply(Vector input) {
        if (this.targetRandomString == null) {
            this.numberOfBits = input.size();

            BigInteger bi = new BigInteger(this.numberOfBits, new RandomAdaptor(new MersenneTwister()));
            this.targetRandomString = bi.toString(2);

            // We need to prepend leading '0's cause the BigInteger removes leading 0's
            // cause it does not change the value of the number that is represented
            if (this.targetRandomString.length() != this.numberOfBits) {
                StringBuilder buf = new StringBuilder(this.targetRandomString);
                int difference = this.numberOfBits - this.targetRandomString.length();

                for (int i = 0; i < difference; i++) {
                    buf.insert(0, '0');
                }

                this.targetRandomString = buf.toString();
            }
        }
        int result = 0;

        for (int i = 0; i < input.size(); i++) {
            boolean bitValue = (this.targetRandomString.charAt(i) == '1') ? true : false;

            if (input.booleanValueOf(i) == bitValue) {
                result++;
            }
        }

        return result;
    }
}

@@ -917,16 +917,55 @@
 crete;%0A%0A
+import com.google.common.base.Strings;%0A
 import j
@@ -2613,525 +2613,53 @@
 g = 
-bi.to
 String
-(2);%0A%0A            // We need to prepend leading '0's cause the BigInteger removes leading 0's%0A            // cause it does not change the value of the number that is represented%0A            if (this.targetRandomString.length() != this.numberOfBits) %7B%0A                StringBuilder buf = new StringBuilder(this.targetRandomString);%0A                int difference = this.numberOfBits - this.targetRandomString.length();%0A%0A                for (int i = 0; i %3C difference; i++) %7B%0A                    buf.insert(0
+s.padStart(bi.toString(2), numberOfBits
 , '0
@@ -2674,100 +2674,10 @@
     
- 
-       %7D%0A%0A                this.targetRandomString = buf.toString();%0A            %7D%0A        
 %7D
+%0A
 %0A   

Use library functions and don't roll my own.