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 27 /** 28 * A Token is the basic building block of any formula. 29 * A Token can be of four types (Operator, Operand, Function with fixed 30 * arguments and function with a variable number of arguments. Each type can 31 * have numerous id's. Thetypes are define in <code>ParseToken</code> and the 32 * id's are defined in <code>TokenConstants</code>. The other member variables 33 * are priority which is returned from the <code>PrecedenceTable</code>, the 34 * value which is the String equivalent of the token (eg. "+", "$A$12", "SUM") 35 * and the number of arguments which is only valid for operators and functions. 36 * Tokens should never be created directly and instead are created by the 37 * <code>TokenFactory</code> 38 */ 39 public class Token implements ParseToken { 40 41 private String value; 42 private int type; // operator, operand, function fixed, function variable 43 private int id; // cell reference, SUM, integer 44 private int priority; 45 private int numArgs=-1; 46 Token(String op, int type, int id, int args)47 public Token(String op, int type, int id, int args) { 48 this.value = op; 49 this.type = type; 50 this.id = id; 51 this.numArgs = args; 52 if(type==ParseToken.TOKEN_FUNCTION_VARIABLE) { 53 priority = PrecedenceTable.getPrecedence("FUNCTION"); 54 } else if(type==ParseToken.TOKEN_OPERATOR) { 55 priority = PrecedenceTable.getPrecedence(op); 56 } else { 57 priority = PrecedenceTable.getPrecedence("DEFAULT"); 58 } 59 } 60 61 /** 62 * Checks if the current token is an operator 63 * 64 * @return A <code>boolean</code> result of the comaparison 65 */ isOperator()66 public boolean isOperator() { 67 return type == ParseToken.TOKEN_OPERATOR; 68 } 69 70 /** 71 * Checks if the current token is an operand 72 * 73 * @return A <code>boolean</code> result of the comaparison 74 */ isOperand()75 public boolean isOperand() { 76 return type == ParseToken.TOKEN_OPERAND; 77 } 78 79 /** 80 * Checks if the current token is a function 81 * 82 * @return A <code>boolean</code> result of the comaparison 83 */ isFunction()84 public boolean isFunction() { 85 return (type==ParseToken.TOKEN_FUNCTION_FIXED) || (type==ParseToken.TOKEN_FUNCTION_VARIABLE); 86 } 87 88 /** 89 * Returns the token type. This can be one of four values (TOKEN_OPERATOR, 90 * TOKEN_OPERAND, TOKEN_FUNCTION_FIXED, TOKEN_FUNCTION_VARIABLE) defined in 91 * <code>ParseToken</code> 92 * 93 * @return A <code>boolean</code> result of the comparison 94 */ getTokenType()95 public int getTokenType() { 96 97 return type; 98 } 99 100 /** 101 * Returns the ID of this token. This ID is equivalent to the pexcel hex 102 * value and is defined in <code>ParseToken</code> 103 * 104 * @return Returns the id of this token 105 */ getTokenID()106 public int getTokenID() { 107 108 return id; 109 } 110 111 /** 112 * Returns the <code>String</code> equivalent of this token 113 * 114 * @return The <code>String</code> representing this Token 115 */ getValue()116 public String getValue() { 117 return value; 118 } 119 120 /** 121 * Returns the number of arguments if this token represents an operator or 122 * function. Otherwise returns -1. 123 * 124 * @return The number of arguments 125 */ getNumArgs()126 public int getNumArgs() { 127 return numArgs; 128 } 129 130 /** 131 * Checks if the current token is an operator 132 * 133 * @return A <code>boolean</code> result of the comparison 134 */ getTokenPriority()135 public int getTokenPriority() { 136 return priority; 137 } 138 139 /** 140 * Returns the <code>String</code> equivalent of this token 141 * 142 * @return The <code>String</code> representing this Token 143 */ toString()144 public String toString() { 145 return getValue(); 146 } 147 } 148