01: package com.teamkonzept.lib.math;
02:
03: public class OperatorPriority implements Comparable {
04:
05: static int LOGICAL_NOT_PRIORITY = 8;
06: static int MATH_FUNCTION_PRIORITY = 7;
07: static int MATH_EXP_PRIORITY = 6;
08: static int MATH_MULT_PRIORITY = 5;
09: static int MATH_ADD_PRIORITY = 4;
10: static int COMPARE_LESS_PRIORITY = 3;
11: static int COMPARE_EQUAL_PRIORITY = 2;
12: static int LOGICAL_OR_PRIORITY = 1;
13: static int LOGICAL_AND_PRIORITY = 0;
14:
15: /* priority of the paren level */
16: int paren = 0;
17: /* priority of the symbol */
18: int symbol = 0;
19: /* priority of the position */
20: int position = 0;
21:
22: public OperatorPriority(int parenPriority, int symbolPriority,
23: int positionPriority) {
24: symbol = symbolPriority;
25: paren = parenPriority;
26: position = positionPriority;
27: }
28:
29: /**
30: * Compares this OperatorPriority with the specified OperatorPriority for order.
31: * Returns a negative integer, zero, or a positive integer as this object is less
32: * than, equal to, or greater than the specified object.
33: */
34: public int compareTo(Object o) {
35: OperatorPriority op = (OperatorPriority) o;
36: // operators in deeper paren level have higher priority
37: if (paren < op.paren)
38: return -1;
39: if (paren > op.paren)
40: return 1;
41: // some operators have higher priority
42: if (symbol < op.symbol)
43: return -1;
44: if (symbol > op.symbol)
45: return 1;
46: // operators more left have higher priority
47: if (position > op.position)
48: return -1;
49: if (position < op.position)
50: return 1;
51: return 0;
52: }
53: }
|