Comparable fit In Range - Java java.lang

Java examples for java.lang:double

Description

Comparable fit In Range

Demo Code

/*//from   w  ww . ja  v a  2s  . c  om
 * Copyright (c) 2014 aleon GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Markus Rathgeb - initial API and implementation and/or initial documentation
 */
//package com.java2s;

public class Main {
    public static <T extends Comparable<T>> T fitInRange(T value, T min,
            T max) {
        if (value.compareTo(min) < 0) {
            return min;
        } else if (value.compareTo(max) > 0) {
            return max;
        } else {
            return value;
        }
    }
}

Related Tutorials