code: public static int restrictRange(int value, int min, int max)
      {
          return Math.min((Math.max(value, min)), max);
      }
docs: If the specified value is not greater than or equal to the specified minimum and
  less than or equal to the specified maximum, adjust it so that it is.
  @param value The value to check.
  @param min The minimum permitted value.
  @param max The maximum permitted value.
  @return {@code value} if it is between the specified limits, {@code min} if the value
  is too low, or {@code max} if the value is too high.
  @since 1.2