You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
Java
38 lines
1.1 KiB
Java
Find Numeric filter
|
|
|
|
class FindNumFilter {
|
|
/** The value of this filter */
|
|
int num;
|
|
/** Constants for the comparison operators. */
|
|
final int LE = -2, LT = -1, EQ = 0, GT = +1, GE = +2;
|
|
/** The current comparison operator */
|
|
int mode = EQ;
|
|
|
|
/** Constructor */
|
|
FindNumFilter(String input) {
|
|
switch(input.charAt(0)) {
|
|
case '+': mode = GT; break;
|
|
case '-': mode = LT; break;
|
|
case '=': mode = EQ; break;
|
|
// No syntax for LE or GE yet.
|
|
}
|
|
num = Math.abs(Integer.parseInt(input));
|
|
}
|
|
|
|
/** Construct a NumFilter when you know its mode and value */
|
|
FindNumFilter(int mode, int value) {
|
|
this.mode = mode;
|
|
num = value;
|
|
}
|
|
boolean accept(int n) {
|
|
switch(mode) {
|
|
case GT: return n > num;
|
|
case EQ: return n == num;
|
|
case LT: return n < num;
|
|
default:
|
|
System.err.println("UNEX CASE " + mode );
|
|
return false;
|
|
}
|
|
}
|
|
}
|