@Test(groups = "slow") public void rateTest1() { RateTransform rt = new RateTransform(); assertEquals(null, rt.process(new Double(12))); sleep(10); assertEquals(0.0, rt.process(new Double(12))); sleep(10); assertTrue((Double) rt.process(new Double(0.0)) < 0.0); sleep(10); assertTrue((Double) rt.process(new Double(10.0)) > 0.0); }
@Override public Object process(Object value) { Long time = System.currentTimeMillis(); if (this.lastValue == null) { this.lastUpdate = time; this.lastValue = value; return null; } if (!(value instanceof Number)) { throw new IllegalArgumentException(String.format("Rate requested, but value '%s' of type '%s' is not a number", value.toString(),value.getClass().getName())); } double diff = ((Number) value).doubleValue() - ((Number) this.lastValue).doubleValue(); double timeChangeInSeconds = ((double)(time - this.lastUpdate)) / 1000.0; double rate; if(timeChangeInSeconds == 0.0) rate = Double.NaN; else { rate = diff / timeChangeInSeconds; if(Math.abs(rate) < EPS) { rate = 0.0; } } if (Double.isNaN(rate) || rate == Double.POSITIVE_INFINITY || rate == Double.NEGATIVE_INFINITY) { log.warn("Overflow in rate calculation: val=%f, last val=%f; time=%tc, last time=%tc", ((Number) value).doubleValue(), ((Number) this.lastValue).doubleValue(), time, this.lastUpdate); this.lastUpdate = time; this.lastValue = value; return null; } this.lastUpdate = time; this.lastValue = value; return rate; }
RateTransform extends Transform { @Override public Object process(Object value) { Long time = System.currentTimeMillis(); if (this.lastValue == null) { this.lastUpdate = time; this.lastValue = value; return null; } if (!(value instanceof Number)) { throw new IllegalArgumentException(String.format("Rate requested, but value '%s' of type '%s' is not a number", value.toString(),value.getClass().getName())); } double diff = ((Number) value).doubleValue() - ((Number) this.lastValue).doubleValue(); double timeChangeInSeconds = ((double)(time - this.lastUpdate)) / 1000.0; double rate; if(timeChangeInSeconds == 0.0) rate = Double.NaN; else { rate = diff / timeChangeInSeconds; if(Math.abs(rate) < EPS) { rate = 0.0; } } if (Double.isNaN(rate) || rate == Double.POSITIVE_INFINITY || rate == Double.NEGATIVE_INFINITY) { log.warn("Overflow in rate calculation: val=%f, last val=%f; time=%tc, last time=%tc", ((Number) value).doubleValue(), ((Number) this.lastValue).doubleValue(), time, this.lastUpdate); this.lastUpdate = time; this.lastValue = value; return null; } this.lastUpdate = time; this.lastValue = value; return rate; } }
RateTransform extends Transform { @Override public Object process(Object value) { Long time = System.currentTimeMillis(); if (this.lastValue == null) { this.lastUpdate = time; this.lastValue = value; return null; } if (!(value instanceof Number)) { throw new IllegalArgumentException(String.format("Rate requested, but value '%s' of type '%s' is not a number", value.toString(),value.getClass().getName())); } double diff = ((Number) value).doubleValue() - ((Number) this.lastValue).doubleValue(); double timeChangeInSeconds = ((double)(time - this.lastUpdate)) / 1000.0; double rate; if(timeChangeInSeconds == 0.0) rate = Double.NaN; else { rate = diff / timeChangeInSeconds; if(Math.abs(rate) < EPS) { rate = 0.0; } } if (Double.isNaN(rate) || rate == Double.POSITIVE_INFINITY || rate == Double.NEGATIVE_INFINITY) { log.warn("Overflow in rate calculation: val=%f, last val=%f; time=%tc, last time=%tc", ((Number) value).doubleValue(), ((Number) this.lastValue).doubleValue(), time, this.lastUpdate); this.lastUpdate = time; this.lastValue = value; return null; } this.lastUpdate = time; this.lastValue = value; return rate; }  }
RateTransform extends Transform { @Override public Object process(Object value) { Long time = System.currentTimeMillis(); if (this.lastValue == null) { this.lastUpdate = time; this.lastValue = value; return null; } if (!(value instanceof Number)) { throw new IllegalArgumentException(String.format("Rate requested, but value '%s' of type '%s' is not a number", value.toString(),value.getClass().getName())); } double diff = ((Number) value).doubleValue() - ((Number) this.lastValue).doubleValue(); double timeChangeInSeconds = ((double)(time - this.lastUpdate)) / 1000.0; double rate; if(timeChangeInSeconds == 0.0) rate = Double.NaN; else { rate = diff / timeChangeInSeconds; if(Math.abs(rate) < EPS) { rate = 0.0; } } if (Double.isNaN(rate) || rate == Double.POSITIVE_INFINITY || rate == Double.NEGATIVE_INFINITY) { log.warn("Overflow in rate calculation: val=%f, last val=%f; time=%tc, last time=%tc", ((Number) value).doubleValue(), ((Number) this.lastValue).doubleValue(), time, this.lastUpdate); this.lastUpdate = time; this.lastValue = value; return null; } this.lastUpdate = time; this.lastValue = value; return rate; }  @Override Object process(Object value); }
RateTransform extends Transform { @Override public Object process(Object value) { Long time = System.currentTimeMillis(); if (this.lastValue == null) { this.lastUpdate = time; this.lastValue = value; return null; } if (!(value instanceof Number)) { throw new IllegalArgumentException(String.format("Rate requested, but value '%s' of type '%s' is not a number", value.toString(),value.getClass().getName())); } double diff = ((Number) value).doubleValue() - ((Number) this.lastValue).doubleValue(); double timeChangeInSeconds = ((double)(time - this.lastUpdate)) / 1000.0; double rate; if(timeChangeInSeconds == 0.0) rate = Double.NaN; else { rate = diff / timeChangeInSeconds; if(Math.abs(rate) < EPS) { rate = 0.0; } } if (Double.isNaN(rate) || rate == Double.POSITIVE_INFINITY || rate == Double.NEGATIVE_INFINITY) { log.warn("Overflow in rate calculation: val=%f, last val=%f; time=%tc, last time=%tc", ((Number) value).doubleValue(), ((Number) this.lastValue).doubleValue(), time, this.lastUpdate); this.lastUpdate = time; this.lastValue = value; return null; } this.lastUpdate = time; this.lastValue = value; return rate; }  @Override Object process(Object value);  }