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);  }
@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); }
@Test public void testScaling() throws Exception { DataRange rawRange = new DataRange(100, 200); DataRange unitsRange = new DataRange(50.0, 100.0); int fractionalDigits = 1; LinearScale ls = new LinearScale(rawRange, unitsRange, fractionalDigits); BigDecimal scaledValue = ls.scaleRawValue(100); Assert.assertTrue(BigDecimal.valueOf(50).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(200); Assert.assertTrue(BigDecimal.valueOf(100).setScale(fractionalDigits).compareTo(scaledValue) == 0); rawRange = new DataRange(200, 100); unitsRange = new DataRange(50.0, 100.0); ls = new LinearScale(rawRange, unitsRange, fractionalDigits); scaledValue = ls.scaleRawValue(100); Assert.assertTrue(BigDecimal.valueOf(100).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(200); Assert.assertTrue(BigDecimal.valueOf(50).setScale(fractionalDigits).compareTo(scaledValue) == 0); rawRange = new DataRange(0, 100); unitsRange = new DataRange(-50.0, 50.0); ls = new LinearScale(rawRange, unitsRange, fractionalDigits); scaledValue = ls.scaleRawValue(0); Assert.assertTrue(BigDecimal.valueOf(-50).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(50); Assert.assertTrue(BigDecimal.valueOf(0).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(100); Assert.assertTrue(BigDecimal.valueOf(50).setScale(fractionalDigits).compareTo(scaledValue) == 0); rawRange = new DataRange(0, 100); unitsRange = new DataRange(50.0, -50.0); ls = new LinearScale(rawRange, unitsRange, fractionalDigits); scaledValue = ls.scaleRawValue(0); Assert.assertTrue(BigDecimal.valueOf(50).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(50); Assert.assertTrue(BigDecimal.valueOf(0).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(100); Assert.assertTrue(BigDecimal.valueOf(-50).setScale(fractionalDigits).compareTo(scaledValue) == 0); }
@Test public void testFractionalDigits() throws Exception { DataRange rawRange = new DataRange(0, 100); DataRange unitsRange = new DataRange(0.25, 1.25); int fractionalDigits = 2; LinearScale ls = new LinearScale(rawRange, unitsRange, fractionalDigits); BigDecimal scaledValue = ls.scaleRawValue(0); Assert.assertTrue(BigDecimal.valueOf(0.25).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(50); Assert.assertTrue(BigDecimal.valueOf(0.75).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(100); Assert.assertTrue(BigDecimal.valueOf(1.25).setScale(fractionalDigits).compareTo(scaledValue) == 0); fractionalDigits = 1; ls = new LinearScale(rawRange, unitsRange, fractionalDigits); scaledValue = ls.scaleRawValue(0); Assert.assertTrue(BigDecimal.valueOf(0.3).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(50); Assert.assertTrue(BigDecimal.valueOf(0.8).setScale(fractionalDigits).compareTo(scaledValue) == 0); scaledValue = ls.scaleRawValue(100); Assert.assertTrue(BigDecimal.valueOf(1.3).setScale(fractionalDigits).compareTo(scaledValue) == 0); }
@Test public void testScaleOutOfRangeValue() throws Exception { DataRange rawRange = new DataRange(50, 250); DataRange unitsRange = new DataRange(50.0, 100.0); int fractionalDigits = 1; LinearScale ls = new LinearScale(rawRange, unitsRange, fractionalDigits); BigDecimal scaledValue; try { scaledValue = ls.scaleRawValue(49); Assert.fail(); } catch (EepOutOfRangeException e) { } try { scaledValue = ls.scaleRawValue(251); Assert.fail(); } catch (EepOutOfRangeException e) { } }