Here you can find the source of clamp(int ptr, int size)
public static int clamp(int ptr, int size)
//package com.java2s; //License from project: Apache License public class Main { public static int clamp(int ptr, int size) { return clamp(ptr, 0, size - 1);//for array indices }//from ww w . j a v a 2 s . c o m public static int clamp(int ptr, int min, int max) { if (max < min) { return (ptr < min) ? min : ptr; } if (max == min) { return min;///*or,*/ return max; } return (ptr < min) ? min : ((ptr > max) ? max : ptr); } public static double clamp(double val, double min, double max) { return (val < min) ? min : ((val > max) ? max : val); } }