<commit_before>/*
 * Java Genetic Algorithm Library (@!identifier!@).
 * Copyright (c) @!year!@ <NAME>
 *  
 * 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 2.1 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 *    <NAME> (<EMAIL>)
 * 
 */
package org.jenetics;

import static org.jenetics.util.object.Verify;
import static org.jenetics.util.object.eq;
import static org.jenetics.util.object.hashCodeOf;
import static org.jenetics.util.object.nonNull;

import java.util.Iterator;

import javolution.lang.Immutable;
import javolution.lang.Realtime;
import javolution.text.Text;
import javolution.xml.XMLFormat;
import javolution.xml.XMLSerializable;
import javolution.xml.stream.XMLStreamException;

import org.jenetics.util.Array;
import org.jenetics.util.Function;
import org.jenetics.util.Factory;
import org.jenetics.util.ISeq;
import org.jenetics.util.Seq;
import org.jenetics.util.Verifiable;
import org.jenetics.util.object;

/**
 * This class is the encoded problem solution with one to many Chromosomes.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public final class Genotype<T extends Gene<?, T>> 
	implements 
		Factory<Genotype<T>>,
		Iterable<Chromosome<T>>, 
		Verifiable, 
		XMLSerializable, 
		Realtime, 
		Immutable
{
	private static final long serialVersionUID = 2L;
	
	private final ISeq<Chromosome<T>> _chromosomes; 
	private final int _ngenes;
	
	//Caching isValid value.
	private volatile Boolean _valid = null;
	
	private Genotype(final ISeq<Chromosome<T>> chromosomes, final int ngenes) {
		assert(chromosomes != null);
		assert(ngenes(chromosomes) == ngenes);
		
		_chromosomes = chromosomes;
		_ngenes = ngenes;
	}
	
	private static int ngenes(final Seq<? extends Chromosome<?>> chromosomes) {
		int ngenes = 0;
		for (int i = chromosomes.length(); --i >= 0;) {
			ngenes += chromosomes.get(i).length();
		}
		return ngenes;
	}
	
	/**
	 * Return the chromosome at the given index. It is guaranteed, that the 
	 * returned chromosome is not null.
	 * 
	 * @param index Chromosome index.
	 * @return The Chromosome.
	 * @throws IndexOutOfBoundsException if (index < 0 || index >= _length).
	 */
	public Chromosome<T> getChromosome(final int index) {
		assert(_chromosomes != null);
		assert(_chromosomes.get(index) != null);
		
		return _chromosomes.get(index);
	}
	
	/**
	 * Return the first chromosome.
	 * 
	 * @return The first chromosome.
	 */
	public Chromosome<T> getChromosome() {
		assert(_chromosomes != null);
		assert(_chromosomes.get(0) != null);
		
		return _chromosomes.get(0);
	}
	
	/**
	 * Return the first {@link Gene} of the first {@link Chromosome} of this
	 * {@code Genotype}.
	 * 
	 * @return the first {@link Gene} of the first {@link Chromosome} of this
	 *         {@code Genotype}.
	 */
	public T getGene() {
		assert(_chromosomes != null);
		assert(_chromosomes.get(0) != null);
		
		return _chromosomes.get(0).getGene();
	}
	
	public ISeq<Chromosome<T>> toSeq() {
		return _chromosomes;
	}
	
	@Override
	public Iterator<Chromosome<T>> iterator() {
		return _chromosomes.iterator();
	}
	
	/**
	 * Getting the number of _chromosomes of this genotype.
	 * 
	 * @return number of _chromosomes.
	 */ 
	public int length() {
		return _chromosomes.length();
	}
	
	/**
	 * Return the number of genes this genotype consists of. This is the sum of
	 * the number of genes of the genotype chromosomes.
	 * 
	 * @return Return the number of genes this genotype consists of.
	 */
	int getNumberOfGenes() {
		return _ngenes;
	}
	
	/**
	 * Test if this genotype is valid. A genotype is valid if all its
	 * {@link Chromosome}s are valid.
	 * 
	 * @return true if this genotype is valid, false otherwise.
	 */
	@Override
	public boolean isValid() {
		if (_valid == null) {
			_valid = _chromosomes.foreach(Verify) == -1;
		}
		return _valid;
	}
	
	/**
	 * Return a new, random genotype by creating new, random chromosomes (calling
	 * the {@link Chromosome#newInstance()} method) from the chromosomes of this
	 * genotype.
	 */
	@Override<commit_msg>Make the method Genotype.getNumberOfGenes() public.<commit_after>/*
 * Java Genetic Algorithm Library (@!identifier!@).
 * Copyright (c) @!year!@ <NAME>
 *  
 * 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 2.1 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 *    <NAME> (<EMAIL>)
 * 
 */
package org.jenetics;

import static org.jenetics.util.object.Verify;
import static org.jenetics.util.object.eq;
import static org.jenetics.util.object.hashCodeOf;
import static org.jenetics.util.object.nonNull;

import java.util.Iterator;

import javolution.lang.Immutable;
import javolution.lang.Realtime;
import javolution.text.Text;
import javolution.xml.XMLFormat;
import javolution.xml.XMLSerializable;
import javolution.xml.stream.XMLStreamException;

import org.jenetics.util.Array;
import org.jenetics.util.Function;
import org.jenetics.util.Factory;
import org.jenetics.util.ISeq;
import org.jenetics.util.Seq;
import org.jenetics.util.Verifiable;
import org.jenetics.util.object;

/**
 * This class is the encoded problem solution with one to many Chromosomes.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public final class Genotype<T extends Gene<?, T>> 
	implements 
		Factory<Genotype<T>>,
		Iterable<Chromosome<T>>, 
		Verifiable, 
		XMLSerializable, 
		Realtime, 
		Immutable
{
	private static final long serialVersionUID = 2L;
	
	private final ISeq<Chromosome<T>> _chromosomes; 
	private final int _ngenes;
	
	//Caching isValid value.
	private volatile Boolean _valid = null;
	
	private Genotype(final ISeq<Chromosome<T>> chromosomes, final int ngenes) {
		assert(chromosomes != null);
		assert(ngenes(chromosomes) == ngenes);
		
		_chromosomes = chromosomes;
		_ngenes = ngenes;
	}
	
	private static int ngenes(final Seq<? extends Chromosome<?>> chromosomes) {
		int ngenes = 0;
		for (int i = chromosomes.length(); --i >= 0;) {
			ngenes += chromosomes.get(i).length();
		}
		return ngenes;
	}
	
	/**
	 * Return the chromosome at the given index. It is guaranteed, that the 
	 * returned chromosome is not null.
	 * 
	 * @param index Chromosome index.
	 * @return The Chromosome.
	 * @throws IndexOutOfBoundsException if (index < 0 || index >= _length).
	 */
	public Chromosome<T> getChromosome(final int index) {
		assert(_chromosomes != null);
		assert(_chromosomes.get(index) != null);
		
		return _chromosomes.get(index);
	}
	
	/**
	 * Return the first chromosome.
	 * 
	 * @return The first chromosome.
	 */
	public Chromosome<T> getChromosome() {
		assert(_chromosomes != null);
		assert(_chromosomes.get(0) != null);
		
		return _chromosomes.get(0);
	}
	
	/**
	 * Return the first {@link Gene} of the first {@link Chromosome} of this
	 * {@code Genotype}.
	 * 
	 * @return the first {@link Gene} of the first {@link Chromosome} of this
	 *         {@code Genotype}.
	 */
	public T getGene() {
		assert(_chromosomes != null);
		assert(_chromosomes.get(0) != null);
		
		return _chromosomes.get(0).getGene();
	}
	
	public ISeq<Chromosome<T>> toSeq() {
		return _chromosomes;
	}
	
	@Override
	public Iterator<Chromosome<T>> iterator() {
		return _chromosomes.iterator();
	}
	
	/**
	 * Getting the number of _chromosomes of this genotype.
	 * 
	 * @return number of _chromosomes.
	 */ 
	public int length() {
		return _chromosomes.length();
	}
	
	/**
	 * Return the number of genes this genotype consists of. This is the sum of
	 * the number of genes of the genotype chromosomes.
	 * 
	 * @return Return the number of genes this genotype consists of.
	 */
	public int getNumberOfGenes() {
		return _ngenes;
	}
	
	/**
	 * Test if this genotype is valid. A genotype is valid if all its
	 * {@link Chromosome}s are valid.
	 * 
	 * @return true if this genotype is valid, false otherwise.
	 */
	@Override
	public boolean isValid() {
		if (_valid == null) {
			_valid = _chromosomes.foreach(Verify) == -1;
		}
		return _valid;
	}
	
	/**
	 * Return a new, random genotype by creating new, random chromosomes (calling
	 * the {@link Chromosome#newInstance()} method) from the chromosomes of this
	 * genotype.
	 */
	@Override
{'fraction_non_alphanumeric': 0.11093333333333333, 'fraction_numerical': 0.004693333333333333, 'mean_word_length': 3.725806451612903, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1446002', 'n_tokens_mistral': 3284, 'n_tokens_neox': 2783, 'n_words': 964}