All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bnd.math.business.evo.EvoEnumeratedTestSampleGeneratorBO Maven / Gradle / Ivy

The newest version!
package com.bnd.math.business.evo;

import java.util.*;

import com.bnd.math.BndMathException;


public class EvoEnumeratedTestSampleGeneratorBO implements EvoTestSampleGeneratorBO {

	private final List availableTestSamples;
	private final Integer numberOfSelectedSampleCopies;
	private final Integer numberOfSamplesToSelect;

	public EvoEnumeratedTestSampleGeneratorBO(Collection availableTestSamples) {
		this(availableTestSamples, null);
	}

	public EvoEnumeratedTestSampleGeneratorBO(
		Collection availableTestSamples,
		Integer numberOfSelectedSampleCopies
	) {
		this(availableTestSamples, numberOfSelectedSampleCopies, null);
	}

	public EvoEnumeratedTestSampleGeneratorBO(
		Collection availableTestSamples,
		Integer numberOfSelectedSampleCopies,
		Integer numberOfSamplesToSelect
	) {
		this.availableTestSamples = new ArrayList(availableTestSamples);
		this.numberOfSelectedSampleCopies = numberOfSelectedSampleCopies;
		this.numberOfSamplesToSelect = numberOfSamplesToSelect;
		if (numberOfSamplesToSelect != null && numberOfSamplesToSelect > availableTestSamples.size()) {
			throw new BndMathException("The number of test samples to select is greater than the number of available test samples.");
		}
	}

	@Override
	public Collection createTestSamples() {
		Collection testSamples = null;
		if (numberOfSamplesToSelect == null) {
			testSamples = availableTestSamples;
		} else {
			testSamples = selectTestSamplesRandomly();
		}
		if (numberOfSelectedSampleCopies != null && numberOfSelectedSampleCopies > 1) {
			Collection duplicatedTestSamples = new ArrayList();
			for (int copyIndex = 0; copyIndex < numberOfSelectedSampleCopies; copyIndex++) {
				duplicatedTestSamples.addAll(testSamples);
			}
			return duplicatedTestSamples;
		}
		return testSamples;
	}

	protected Collection selectTestSamplesRandomly() {
		Collection randomlySelectedSamples = new ArrayList();
		Collections.shuffle(availableTestSamples);
		Iterator availableTestSamplesIterator = availableTestSamples.iterator();
		for (int i = 0; i < numberOfSamplesToSelect; i++) {
			randomlySelectedSamples.add(availableTestSamplesIterator.next());
		}
		return randomlySelectedSamples;
	}

	protected List getAvailableTestSamples() {
		return availableTestSamples;
	}

	protected Integer getNumberOfSamplesToSelect() {
		return numberOfSamplesToSelect;
	}

	protected int getTestSamplesNumber() {
		int testSamplesNum = numberOfSamplesToSelect != null ? numberOfSamplesToSelect : availableTestSamples.size();
		return numberOfSelectedSampleCopies != null ? numberOfSelectedSampleCopies * testSamplesNum : testSamplesNum;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy