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

com.softicar.platform.common.math.Clamping Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.math;

import com.softicar.platform.common.core.exceptions.SofticarDeveloperException;

/**
 * Provides the methods to clamp values in a value range.
 * 
 * @author Oliver Richers
 */
public class Clamping {

	public static int clamp(int min, int max, int v) {

		if (max < min) {
			throw new SofticarDeveloperException("Invalid range [%d, %d]", min, max);
		}
		return Math.min(Math.max(min, v), max);
	}

	public static long clamp(long min, long max, long v) {

		if (max < min) {
			throw new SofticarDeveloperException("Invalid range [%d, %d]", min, max);
		}
		return Math.min(Math.max(min, v), max);
	}

	public static float clamp(float min, float max, float v) {

		if (max < min) {
			throw new SofticarDeveloperException("Invalid range [%g, %g]", min, max);
		}
		return Math.min(Math.max(min, v), max);
	}

	public static double clamp(double min, double max, double v) {

		if (max < min) {
			throw new SofticarDeveloperException("Invalid range [%g, %g]", min, max);
		}
		return Math.min(Math.max(min, v), max);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy