KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > lib > math > OperatorPriority


1 package com.teamkonzept.lib.math;
2
3
4 public class OperatorPriority implements Comparable JavaDoc{
5
6     static int LOGICAL_NOT_PRIORITY = 8;
7     static int MATH_FUNCTION_PRIORITY = 7;
8     static int MATH_EXP_PRIORITY = 6;
9     static int MATH_MULT_PRIORITY = 5;
10     static int MATH_ADD_PRIORITY = 4;
11     static int COMPARE_LESS_PRIORITY = 3;
12     static int COMPARE_EQUAL_PRIORITY = 2;
13     static int LOGICAL_OR_PRIORITY = 1;
14     static int LOGICAL_AND_PRIORITY = 0;
15
16     /* priority of the paren level */
17     int paren = 0;
18     /* priority of the symbol */
19     int symbol = 0;
20     /* priority of the position */
21     int position = 0;
22
23     
24     public OperatorPriority(int parenPriority, int symbolPriority,
25              int positionPriority){
26     symbol = symbolPriority;
27     paren = parenPriority;
28     position = positionPriority;
29     }
30
31     /**
32      * Compares this OperatorPriority with the specified OperatorPriority for order.
33      * Returns a negative integer, zero, or a positive integer as this object is less
34      * than, equal to, or greater than the specified object.
35      */

36     public int compareTo(Object JavaDoc o){
37     OperatorPriority op = (OperatorPriority)o;
38     // operators in deeper paren level have higher priority
39
if ( paren < op.paren )
40         return -1;
41     if ( paren > op.paren )
42         return 1;
43     // some operators have higher priority
44
if ( symbol < op.symbol )
45         return -1;
46     if ( symbol > op.symbol )
47         return 1;
48     // operators more left have higher priority
49
if ( position > op.position )
50         return -1;
51     if ( position < op.position )
52         return 1;
53     return 0;
54     }
55 }
56
Popular Tags