/*
 * 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 eu.aleon.aleoncean.util;

public class CalculationUtil {

    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;
        }
    }

    public static double rangeToScale(long rawValue, long rangeMin, long rangeMax, double scaleMin, double scaleMax) {
        final double multiplier = (scaleMax - scaleMin) / (rangeMax - rangeMin);
        final double devValue = multiplier * (rawValue - rangeMin) + scaleMin;
        return fitInRange(devValue, scaleMin, scaleMax);
    }

    public static long scaleToRange(double devValue, double scaleMin, double scaleMax, long rangeMin, long rangeMax) {
        final double multiplier = (rangeMax - rangeMin) / (scaleMax - scaleMin);
        final double rawValue = multiplier * (devValue - scaleMin) + rangeMin;
        return fitInRange((long) rawValue, rangeMin, rangeMax);
    }

    private CalculationUtil() {
    }

}

@@ -1077,346 +1077,1046 @@
     
-public static long scaleToRange(double devValue, double scaleMin, double scaleMax, long rangeMin, long rangeMax) %7B%0A        final double multiplier = (rangeMax - rangeMin) / (scaleMax - scaleMin);%0A        final double rawValue = multiplier * (devValue - scaleMin) + rangeMin;%0A        return fitInRange((long) rawValue, rangeMin, rangeMax);
+/**%0A     * Calculate the given scale value to the corresponding range value.%0A     *%0A     * @param devValue The value (scale) for the calculation.%0A     * @param scaleMin The minimum value of scale.%0A     * @param scaleMax The maximum value of scale.%0A     * @param rangeMin The minimum value of range.%0A     * @param rangeMax The maximum value of range.%0A     * @return The calculated range value.%0A     */%0A    public static long scaleToRange(double devValue, double scaleMin, double scaleMax, long rangeMin, long rangeMax) %7B%0A        final double multiplier = (rangeMax - rangeMin) / (scaleMax - scaleMin);%0A        final double rawValue = multiplier * (devValue - scaleMin) + rangeMin;%0A%0A        // The range could also be inverse (255..0 instead of 0..255),%0A        // so we have to improve the check and set the correct min and max values.%0A        if(rangeMin %3C= rangeMax)%7B%0A            return fitInRange((long) rawValue, rangeMin, rangeMax);%0A        %7D%0A        else%7B%0A            return fitInRange((long) rawValue, rangeMax, rangeMin);%0A        %7D
 %0A   

Insert check for range min and max value (on conversion)