01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fit;
04:
05: import java.util.regex.*;
06: import fit.exception.FitMatcherException;
07:
08: class FitMatcher {
09: private String expression;
10:
11: private Object parameter;
12:
13: public FitMatcher(String expression, Object parameter) {
14: this .expression = expression;
15: this .parameter = parameter;
16: }
17:
18: public boolean matches() throws Exception {
19: Pattern p = Pattern
20: .compile("\\s*_?\\s*(<|>|<=|>=)\\s*([-+]?[\\d]*\\.?[\\d]+)");
21: Matcher m = p.matcher(expression);
22: if (m.matches()) {
23: String op = m.group(1);
24: String operandString = m.group(2);
25: double operand = Double.parseDouble(operandString);
26: double n = ((Number) parameter).doubleValue();
27: if (op.equals("<"))
28: return (n < operand);
29: if (op.equals(">"))
30: return (n > operand);
31: if (op.equals("<="))
32: return (n <= operand);
33: if (op.equals(">="))
34: return (n >= operand);
35: return false;
36: }
37:
38: p = Pattern
39: .compile("\\s*([-+]?[\\d]*\\.?[\\d]+)\\s*(<|>|<=|>=)\\s*_\\s*(<|>|<=|>=)\\s*([-+]?[\\d]*\\.?[\\d]+)");
40: m = p.matcher(expression);
41: if (m.matches()) {
42: double a = Double.parseDouble(m.group(1));
43: String aop = m.group(2);
44: String bop = m.group(3);
45: double b = Double.parseDouble(m.group(4));
46: double n = ((Number) parameter).doubleValue();
47:
48: boolean an = false;
49: if (aop.equals("<"))
50: an = a < n;
51: if (aop.equals("<="))
52: an = a <= n;
53: if (aop.equals(">"))
54: an = a > n;
55: if (aop.equals(">="))
56: an = a >= n;
57:
58: boolean nb = false;
59: if (bop.equals("<"))
60: nb = n < b;
61: if (bop.equals("<="))
62: nb = n <= b;
63: if (bop.equals(">"))
64: nb = n > b;
65: if (bop.equals(">="))
66: nb = n >= b;
67:
68: return an && nb;
69: }
70: throw new FitMatcherException("Invalid FitMatcher Expression");
71: }
72:
73: public String message() {
74: String message = null;
75: String parmString = "<b>" + parameter.toString() + "</b>";
76: if (expression.indexOf("_") == -1)
77: message = parmString + expression;
78: else
79: message = expression.replaceFirst("_", parmString);
80: return message;
81: }
82: }
|