Here you can find the source of clamp(final int min, final int x, final int max)
static int clamp(final int min, final int x, final int max)
//package com.java2s; public class Main { /**/* w w w .ja v a2 s . co m*/ Clamps the value 'x' to be in the range [min, max]. */ static int clamp(final int min, final int x, final int max) { if (max < min) { throw new IllegalArgumentException("Max is less than min"); } return Math.max(min, Math.min(max, x)); } }