Collector.java 1.17 KB
package se.lil.om;

import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;

class Collector {
	ArrayList<LongDelta> list = new ArrayList<LongDelta>();
	
	public void update(ResultSet rset) throws Throwable {
		while(rset.next()) {
			updateValue(rset.getTimestamp(1), rset.getString(2), rset.getLong(3));
		}
		rset.close();
	}
	
	public void updateValue(Timestamp ts, String name, Long value) {
		LongDelta myDelta = null;
		for(int x = 0; x<list.size() && myDelta == null; x++) {
			if(name.equals(list.get(x).name)) {
				myDelta = list.get(x);
			}
		}
		if(myDelta != null) {
			myDelta.lastFetch = myDelta.curFetch;
			myDelta.curFetch = ts;
			myDelta.lastValue = myDelta.curValue;
			myDelta.curValue = value;
		} else {
			myDelta = new LongDelta();
			myDelta.curFetch = ts;
			myDelta.curValue = value;
			myDelta.name = name;
			list.add(myDelta);
		}
	}
	
	public long getPerSecValue(String name) throws Throwable {
		LongDelta myDelta = null;
		for(int x = 0; x<list.size() && myDelta == null; x++) {
			if(name.equals(list.get(x).name)) {
				myDelta = list.get(x);
			}
		}
		if(myDelta != null)
			return myDelta.getPerSecondValue();
		else
			return 0;
	}
}