Created
July 26, 2010 05:33
-
-
Save ekarulf/490219 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :)