@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) { } }
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);  }