Java Integer Clamp clamp(int i, int min, int max)

Here you can find the source of clamp(int i, int min, int max)

Description

clamp

License

Apache License

Declaration

public static int clamp(int i, int min, int max) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static int clamp(int i, int min, int max) {
        if (min >= max)
            throw new IllegalArgumentException("Min must be less than max. " + min + " >= " + max);

        if (i < min)
            return min;
        if (i > max)
            return max;

        return i;
    }//from w w  w. j a  v a2  s.c o m

    public static float clamp(float f, float min, float max) {
        if (min >= max)
            throw new IllegalArgumentException("Min must be less than max. " + min + " >= " + max);

        if (f < min)
            return min;
        if (f > max)
            return max;

        return f;
    }

    public static double clamp(double d, double min, double max) {
        if (min >= max)
            throw new IllegalArgumentException("Min must be less than max. " + min + " >= " + max);

        if (d < min)
            return min;
        if (d > max)
            return max;

        return d;
    }
}

Related

  1. clamp(int amt, int low, int high)
  2. clamp(int c)
  3. clamp(int c)
  4. clamp(int floor, int ceiling, int value)
  5. clamp(int i, int low, int high)
  6. clamp(int min, int val, int max)
  7. clamp(int min, int value, int max)
  8. clamp(int n, int l, int u)
  9. clamp(int n, int lower, int upper)