package it.univaq.disim.sealab.metaheuristic.evolutionary.operator;

import org.uma.jmetal.util.JMetalException;

/**
 * @author Antonio J. Nebro
 * @version 1.0
 */
@SuppressWarnings("serial")
public class RepairRSolutionAtBounds implements RepairRSolution {
	/**
	 * Checks if the value is between its bounds; if not, the lower or upper bound
	 * is returned
	 * 
	 * @param value
	 *            The value to be checked
	 * @param lowerBound
	 * @param upperBound
	 * @return The same value if it is in the limits or a repaired value otherwise
	 */
	public double repairSolutionVariableValue(double value, double lowerBound, double upperBound) {
		if (lowerBound > upperBound) {
			throw new JMetalException(
					"The lower bound (" + lowerBound + ") is greater than the " + "upper bound (" + upperBound + ")");
		}

		double result = value;
		if (value < lowerBound) {
			result = lowerBound;
		}
		if (value > upperBound) {
			result = upperBound;
		}

		return result;
	}
}