Java tutorial
//package com.java2s; import java.util.Comparator; public class Main { /** * Checks if the given object is within the given bounds. * * @param <T> the type of the element * @param o the object * @param from the lower bound of the range * @param fromInclusive true if {@code from} is included in the range; false * otherwise * @param to the upper bound of the range * @param toInclusive true if {@code to} is included in the range; false * otherwise * @param comp the comparator used to compare {@code o} to {@code to} * (cannot be null) * @return true if the specified object is within the specified bounds; false * otherwise */ public static <T> boolean isInRange(T o, T from, boolean fromInclusive, T to, boolean toInclusive, Comparator<? super T> comp) { return isInRangeLow(o, true, from, fromInclusive, comp) && isInRangeHigh(o, true, to, toInclusive, comp); } /** * Checks if the given object (or range lower bound) lies above another * lower bound. * * <p>Generally, this is used to test if an object is above the specified * lower bound (in which case, the parameter {@code oIncluded} will be true). * But we can also tell if the lower bound of one range (specified by the * parameters {@code o} and {@code oIncluded}) lies above the lower bound * of another range (specified by {@code from} and {@code fromInclusive}). * * @param <T> the type of the element * @param o the object * @param oIncluded true if {@code o} needs to be included in the range * @param from the lower bound of the range * @param fromInclusive true if {@code from} is included in the range; false * otherwise * @param comp the comparator used to compare {@code o} to {@code from} * (cannot be null) * @return true if the specified object is greater than the bound and the * bound is exclusive; true if the object is greater than or equal * to the bound and the bound is inclusive; false otherwise */ public static <T> boolean isInRangeLow(T o, boolean oIncluded, T from, boolean fromInclusive, Comparator<? super T> comp) { if (from != null) { int c = comp.compare(o, from); if (c < 0 || (c == 0 && oIncluded && !fromInclusive)) { return false; } } return true; } /** * Checks if the given object (or range upper bound) falls under another * upper bound. * * <p>Generally, this is used to test if an object is under the specified * upper bound (in which case, the parameter {@code oIncluded} will be true). * But we can also tell if the upper bound of one range (specified by the * parameters {@code o} and {@code oIncluded}) falls under the upper bound * of another range (specified by {@code to} and {@code toInclusive}). * * @param <T> the type of the element * @param o the object * @param oIncluded true if {@code o} needs to be included in the range * @param to the upper bound of the range * @param toInclusive true if {@code to} is included in the range, false * otherwise * @param comp the comparator used to compare {@code o} to {@code to} * (cannot be null) * @return true if the specified object is less than the bound and the * bound is exclusive, true if the object is less than or equal * to the bound and the bound is inclusive, and false otherwise */ public static <T> boolean isInRangeHigh(T o, boolean oIncluded, T to, boolean toInclusive, Comparator<? super T> comp) { if (to != null) { int c = comp.compare(o, to); if (c > 0 || (c == 0 && oIncluded && !toInclusive)) { return false; } } return true; } }