@Test public void testBasicConstruction() throws Exception { DataRange rawRange = new DataRange(0, 255); DataRange unitsRange = new DataRange(50.0, 100.0); int fractionalDigits = 1; LinearScale ls = new LinearScale(rawRange, unitsRange, fractionalDigits); BigDecimal scaledValue = ls.scaleRawValue(255); Assert.assertTrue(BigDecimal.valueOf(100).setScale(fractionalDigits).compareTo(scaledValue) == 0); }
public BigDecimal scaleRawValue(int rawValue) throws EepOutOfRangeException { if(rawValue < minRawValue.intValue() || rawValue > maxRawValue.intValue()) { throw new EepOutOfRangeException( "Raw value ''{0}'' is out of valid range [{1}..{2}].", rawValue, minRawValue.intValue(), maxRawValue.intValue() ); } BigDecimal decRawValue = BigDecimal.valueOf(rawValue); BigDecimal scaledValue; int divideScale = Math.max(minUnitsValue.scale(), this.fractionalDigits); scaledValue = maxUnitsValue.subtract(minUnitsValue) .multiply( decRawValue.subtract(minRawValue)) .divide( maxRawValue.subtract(minRawValue), divideScale, RoundingMode.HALF_UP) .add( minUnitsValue) .setScale(this.fractionalDigits, RoundingMode.HALF_UP); return scaledValue; }
LinearScale { public BigDecimal scaleRawValue(int rawValue) throws EepOutOfRangeException { if(rawValue < minRawValue.intValue() || rawValue > maxRawValue.intValue()) { throw new EepOutOfRangeException( "Raw value ''{0}'' is out of valid range [{1}..{2}].", rawValue, minRawValue.intValue(), maxRawValue.intValue() ); } BigDecimal decRawValue = BigDecimal.valueOf(rawValue); BigDecimal scaledValue; int divideScale = Math.max(minUnitsValue.scale(), this.fractionalDigits); scaledValue = maxUnitsValue.subtract(minUnitsValue) .multiply( decRawValue.subtract(minRawValue)) .divide( maxRawValue.subtract(minRawValue), divideScale, RoundingMode.HALF_UP) .add( minUnitsValue) .setScale(this.fractionalDigits, RoundingMode.HALF_UP); return scaledValue; } }
LinearScale { public BigDecimal scaleRawValue(int rawValue) throws EepOutOfRangeException { if(rawValue < minRawValue.intValue() || rawValue > maxRawValue.intValue()) { throw new EepOutOfRangeException( "Raw value ''{0}'' is out of valid range [{1}..{2}].", rawValue, minRawValue.intValue(), maxRawValue.intValue() ); } BigDecimal decRawValue = BigDecimal.valueOf(rawValue); BigDecimal scaledValue; int divideScale = Math.max(minUnitsValue.scale(), this.fractionalDigits); scaledValue = maxUnitsValue.subtract(minUnitsValue) .multiply( decRawValue.subtract(minRawValue)) .divide( maxRawValue.subtract(minRawValue), divideScale, RoundingMode.HALF_UP) .add( minUnitsValue) .setScale(this.fractionalDigits, RoundingMode.HALF_UP); return scaledValue; } LinearScale(DataRange rawDataRange, DataRange unitsDataRange, int fractionalDigits); }
LinearScale { public BigDecimal scaleRawValue(int rawValue) throws EepOutOfRangeException { if(rawValue < minRawValue.intValue() || rawValue > maxRawValue.intValue()) { throw new EepOutOfRangeException( "Raw value ''{0}'' is out of valid range [{1}..{2}].", rawValue, minRawValue.intValue(), maxRawValue.intValue() ); } BigDecimal decRawValue = BigDecimal.valueOf(rawValue); BigDecimal scaledValue; int divideScale = Math.max(minUnitsValue.scale(), this.fractionalDigits); scaledValue = maxUnitsValue.subtract(minUnitsValue) .multiply( decRawValue.subtract(minRawValue)) .divide( maxRawValue.subtract(minRawValue), divideScale, RoundingMode.HALF_UP) .add( minUnitsValue) .setScale(this.fractionalDigits, RoundingMode.HALF_UP); return scaledValue; } LinearScale(DataRange rawDataRange, DataRange unitsDataRange, int fractionalDigits); BigDecimal scaleRawValue(int rawValue); }
LinearScale { public BigDecimal scaleRawValue(int rawValue) throws EepOutOfRangeException { if(rawValue < minRawValue.intValue() || rawValue > maxRawValue.intValue()) { throw new EepOutOfRangeException( "Raw value ''{0}'' is out of valid range [{1}..{2}].", rawValue, minRawValue.intValue(), maxRawValue.intValue() ); } BigDecimal decRawValue = BigDecimal.valueOf(rawValue); BigDecimal scaledValue; int divideScale = Math.max(minUnitsValue.scale(), this.fractionalDigits); scaledValue = maxUnitsValue.subtract(minUnitsValue) .multiply( decRawValue.subtract(minRawValue)) .divide( maxRawValue.subtract(minRawValue), divideScale, RoundingMode.HALF_UP) .add( minUnitsValue) .setScale(this.fractionalDigits, RoundingMode.HALF_UP); return scaledValue; } LinearScale(DataRange rawDataRange, DataRange unitsDataRange, int fractionalDigits); BigDecimal scaleRawValue(int rawValue);  }