📄 differentialset.java
字号:
package net.sf.dz.controller;import java.util.Iterator;import java.util.SortedMap;import java.util.TreeMap;/** * Data set supporting the differential calculation. * * @author Copyright © <a href="mailto:vt@freehold.crocodile.org"> Vadim Tkachenko</a> 2001-2002 * @version $Id: DifferentialSet.java,v 1.3 2004/06/28 20:35:46 vtt Exp $ */public class DifferentialSet extends DataSet { /** * Create the instance. * * @param differentialTime Differential time, milliseconds. Data * elements older than this are expired. */ public DifferentialSet(final long differentialTime) { super(differentialTime); } /** * Get the differential starting with the first data element available * and ending with the last data element available. * * <p> * * Differentiation time must have been taken care of by {@link * DataSet#expire expiration}. * * @return A differential value. */ public final double getDifferential() { if (size() < 2) { // Nothing to compare yet return 0; } long startTime = System.currentTimeMillis(); try { // The following line will throw the NoSuchElementException if // the dataSet is empty. It is possible to check the size, but // handling the exception is more efficient because happens just // once - at the startup, or when there was an interruption in // the data sequence longer than the expiration interval and all // the data elements were expired. Iterator i = iterator(); Long trailerKey = (Long) i.next(); SortedMap resultSet = new TreeMap(); while (i.hasNext()) { double trailerValue = get(trailerKey.longValue()); Long currentKey = (Long) i.next(); double currentValue = get(currentKey.longValue()); long start = trailerKey.longValue(); long stop = currentKey.longValue(); double diff = (currentValue - trailerValue) / (currentKey.longValue() - trailerKey.longValue()); resultSet.put(new Long(start + ((start + stop) / 2)), new Double(diff)); trailerKey = currentKey; } double result = 0; i = resultSet.keySet().iterator(); trailerKey = (Long) i.next(); while (i.hasNext()) { double trailerValue = ((Double) resultSet.get(trailerKey)).doubleValue(); Long currentKey = (Long) i.next(); double currentValue = ((Double) resultSet.get(currentKey)).doubleValue(); double diff = ((currentValue + trailerValue) / 2) * (currentKey.longValue() - trailerKey.longValue()); result += diff; trailerKey = currentKey; } return result; } finally { //System.err.println("getDifferential@" + hashCode() + "," + dataSet.size() + ": " + (System.currentTimeMillis() - startTime)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -