1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.converter.xml.sxc.pexcel.records.formula;
25 
26 import java.util.HashMap;
27 
28 /**
29  * This class defines the precedence applied to each operator when performing a conversion
30  * {@link org.openoffice.xmerge.converter.xml.sxc.pexcel.records.formula.FormulaCompiler#infix2RPN	from infix to RPN.}.
31  */
32 public class PrecedenceTable {
33 	public static final int DEFAULT_PRECEDENCE  = 0;
34 	public static final int EQNEQ_PRECEDENCE    = 1; // =, <>
35 	public static final int GTLTEQ_PRECEDENCE	= 1; // >=, <=
36 	public static final int GTLT_PRECEDENCE		= 2; // >, <
37 	public static final int ADDOP_PRECEDENCE    = 4; // +, -
38 	public static final int MULTOP_PRECEDENCE   = 5; // *, /
39 	public static final int FACTOR_PRECEDENCE	= 6; // ^
40 	public static final int CONCAT_PRECEDENCE	= 6; // &
41 	public static final int UNARY_PRECEDENCE    = 7; // !, Unary +, Unary -
42 	public static final int PAREN_PRECEDENCE    = 8; // (,  )
43 	public static final int FUNCTION_PRECEDENCE = 8;
44 	public static final int COMMA_PRECEDENCE    = 8;
45 
46 	private static HashMap map;
47 	static {
48 		map = new HashMap();
49 
50 		map.put("%", new Integer(UNARY_PRECEDENCE));
51 		map.put("+", new Integer(ADDOP_PRECEDENCE));
52 		map.put("-", new Integer(ADDOP_PRECEDENCE));
53 		map.put("*", new Integer(MULTOP_PRECEDENCE));
54 		map.put("/", new Integer(MULTOP_PRECEDENCE));
55 		map.put("(", new Integer(PAREN_PRECEDENCE));
56 		map.put(")", new Integer(PAREN_PRECEDENCE));
57 		map.put(",", new Integer(COMMA_PRECEDENCE));
58 		map.put(">", new Integer(GTLT_PRECEDENCE));
59 		map.put("<", new Integer(GTLT_PRECEDENCE));
60 		map.put("=", new Integer(EQNEQ_PRECEDENCE));
61 		map.put("&", new Integer(CONCAT_PRECEDENCE));
62 		map.put("^", new Integer(FACTOR_PRECEDENCE));
63 		map.put(">=", new Integer(GTLTEQ_PRECEDENCE));
64 		map.put("<=", new Integer(GTLTEQ_PRECEDENCE));
65 		map.put("<>", new Integer(EQNEQ_PRECEDENCE));
66 		map.put("FUNCTION", new Integer(FUNCTION_PRECEDENCE));
67 	}
68 
69 	/**
70 	 *	Retrieve the precedence value for a given operator.
71 	 *	@param	op	Look up the precedence for this operator
72 	 *	@return an integer representing the integer value of the operator
73 	 */
getPrecedence(String op)74 	public static int getPrecedence(String op) {
75 		Object obj = map.get(op);
76 		if (obj == null) {
77 			return DEFAULT_PRECEDENCE;
78 		}
79 		return ((Integer)obj).intValue();
80 	}
81 }
82