Collector.java 1.95 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()) {
			// Add try catch for issue #5
			Timestamp ts = rset.getTimestamp(1);
			String name = rset.getString(2);
			Long value;
			try {
				value = rset.getLong(3);
			}
			catch (java.sql.SQLException e) {
				value = Long.MAX_VALUE;
			}
			updateValue(ts, name, value);
		}
		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 getCurrentValue(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.getCurrentValue();
		else
			return 0;
	}
	
	public long getDelta(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.getDelta();
		else
			return 0;
	}
	
	public double 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;
	}
}