Skip to content

Instantly share code, notes, and snippets.

@ekarulf
Created July 26, 2010 05:33
Show Gist options
  • Save ekarulf/490219 to your computer and use it in GitHub Desktop.
Save ekarulf/490219 to your computer and use it in GitHub Desktop.
/*
* NumericRangeCalculator.java
*
* Created on Sep 13, 2007, 12:14:01 PM
*
*/
package edu.wustl.asl.gs.common;
import edu.wustl.asl.utils.NumericRange;
import java.util.Map;
/**
*
* @author erik
*/
public class NumericRangeCalculator<T extends Number & Comparable<? super T>> implements SensorStatusCalculator<T> {
// For every class T that extends Number and is comparable to itself or one of its decendents
protected Map<NumericRange<T>, SensorStatus> rules;
protected SensorStatus defaultState;
public NumericRangeCalculator(Map<NumericRange<T>, SensorStatus> rules, SensorStatus defaultState) {
this.rules = rules;
this.defaultState = defaultState;
}
public SensorStatus calculateStatus(SensorValue<? extends T> val) {
for (Map.Entry<NumericRange<T>, SensorStatus> rule : rules.entrySet()) {
NumericRange<T> range = rule.getKey();
SensorStatus status = rule.getValue();
if (range.getLowerBound().compareTo(val.getData()) >= 0 && range.getUpperBound().compareTo(val.getData()) <= 0) {
return status;
}
}
return defaultState;
}
}
@ekarulf
Copy link
Author

ekarulf commented Jul 26, 2010

I wrote this back in college. The program was designed to read values from sensors and determine a status (nominal, warning, error, critical error). That's really not the important part.

What is important is the class declaration. Justification: Number is not Comparable...

I originally committed it without the comment, I was severely chastised :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment