Java Range rangeToScale(long rawValue, long rangeMin, long rangeMax, double scaleMin, double scaleMax)

Here you can find the source of rangeToScale(long rawValue, long rangeMin, long rangeMax, double scaleMin, double scaleMax)

Description

range To Scale

License

Open Source License

Declaration

public static double rangeToScale(long rawValue, long rangeMin, long rangeMax, double scaleMin,
            double scaleMax) 

Method Source Code

//package com.java2s;
/*/*from w  w  w .  j ava 2  s .  co m*/
 * 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
 */

public class Main {
    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 <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

  1. rangeIncludes(long x, long min, long max)
  2. rangeInteger(Integer num, Integer min, Integer max)
  3. rangeLimit(float value, float min, float max)
  4. rangeLimit(int receiver, int minBound, int maxBound)
  5. rangeOfByteArray(byte[] data, int offset, int length)