Java Clamp clamp01(long value)

Here you can find the source of clamp01(long value)

Description

Clamp the given value between 0 and 1 .

License

Open Source License

Parameter

Parameter Description
value The value to clamp.

Return

The clamped value.

Declaration

public static long clamp01(long value) 

Method Source Code

//package com.java2s;
// The MIT License(MIT)

public class Main {
    /**//from  ww w.  j  a v  a  2 s .c om
     * <p>
     * Clamp the given {@code value} between {@code 0} and {@code 1}.
     * </p>
     *
     * @param value The value to clamp.
     * @return The clamped value.
     */
    public static long clamp01(long value) {
        return clamp(value, 0, 1);
    }

    /**
     * <p>
     * Clamp the given {@code value} between {@code 0} and {@code 1}.
     * </p>
     *
     * @param value The value to clamp.
     * @return The clamped value.
     */
    public static double clamp01(double value) {
        return clamp(value, 0, 1);
    }

    /**
     * <p>
     * Clamp the given {@code value} between {@code 0} and {@code 1}.
     * </p>
     *
     * @param value The value to clamp.
     * @return The clamped value.
     */
    public static float clamp01(float value) {
        return clamp(value, 0, 1);
    }

    /**
     * <p>
     * Clamp the given {@code value} between {@code 0} and {@code 1}.
     * </p>
     *
     * @param value The value to clamp.
     * @return The clamped value.
     */
    public static int clamp01(int value) {
        return clamp(value, 0, 1);
    }

    /**
     * <p>
     * Clamp the given {@code value} between a {@code min} and a {@code max} value.
     * </p>
     *
     * @param value The value to clamp.
     * @param min The minimum value.
     * @param max The maximum value.
     * @return The clamped value.
     */
    public static long clamp(long value, long min, long max) {
        return Math.max(min, Math.min(max, value));
    }

    /**
     * <p>
     * Clamp the given {@code value} between a {@code min} and a {@code max} value.
     * </p>
     *
     * @param value The value to clamp.
     * @param min The minimum value.
     * @param max The maximum value.
     * @return The clamped value.
     */
    public static double clamp(double value, double min, double max) {
        return Math.max(min, Math.min(max, value));
    }

    /**
     * <p>
     * Clamp the given {@code value} between a {@code min} and a {@code max} value.
     * </p>
     *
     * @param value The value to clamp.
     * @param min The minimum value.
     * @param max The maximum value.
     * @return The clamped value.
     */
    public static float clamp(float value, float min, float max) {
        return Math.max(min, Math.min(max, value));
    }

    /**
     * <p>
     * Clamp the given {@code value} between a {@code min} and a {@code max} value.
     * </p>
     *
     * @param value The value to clamp.
     * @param min The minimum value.
     * @param max The maximum value.
     * @return The clamped value.
     */
    public static int clamp(int value, int min, int max) {
        return Math.max(min, Math.min(max, value));
    }
}

Related

  1. clamp(final T MIN, final T MAX, final T VALUE)
  2. clamp(final T MIN, final T MAX, final T VALUE)
  3. clamp(T min, T val, T max)
  4. clamp(T n, T l, T h)
  5. clamp(T value, T minimum, T maximum)
  6. clampAdd(long lhs, long rhs)