summaryrefslogtreecommitdiff
path: root/xmerge/source/pexcel/java/org/openoffice/xmerge/converter/xml/sxc/pexcel/records/formula/FunctionLookup.java
blob: a0cba9e566c8b92d9d1045b7da4f2386757982f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package org.openoffice.xmerge.converter.xml.sxc.pexcel.records.formula;

import java.util.HashMap;

public class FunctionLookup extends SymbolLookup {

    private HashMap<String, Integer> stringToArgs = null;

    /**
    * The default constructor - invokes {@link #initialize() initialize()}
     */
    public FunctionLookup() {
        initialize();
    }

    /**
     * Initialize the lookup table for functions
     */
    public void initialize() {
        if ((stringToID != null) || (idToString != null) || (stringToArgs !=null)) {
            return;
        }
        stringToID = new HashMap<String, Integer>();
        idToString = new HashMap<Integer, String>();
        stringToArgs = new HashMap<String, Integer>();

                // Functions with Variable number of Arguments
                // Math and Trig
                addEntry("SUM", TokenConstants.TSUM, -1);
                addEntry("MIN", TokenConstants.TMIN, -1);
                addEntry("PRODUCT", TokenConstants.TPRODUCT, -1);
                addEntry("LOG", TokenConstants.TLOG, -1);
                addEntry("SUMIF", TokenConstants.TSUMIF, -1);
                addEntry("TRUNC", TokenConstants.TRUNC, -1);
                // Financial
                addEntry("DDB", TokenConstants.TDDB, -1);
                addEntry("FV", TokenConstants.TFV, -1);
                addEntry("IRR", TokenConstants.TIRR, -1);
                addEntry("NPER", TokenConstants.TNPER, -1);
                addEntry("NPV", TokenConstants.TNPV, -1);
                addEntry("PMT", TokenConstants.TPMT, -1);
                addEntry("PV", TokenConstants.TPV, -1);
                addEntry("RATE", TokenConstants.TRATE, -1);
                // Statistical
                addEntry("AVERAGE", TokenConstants.TAVERAGE, -1);
                addEntry("COUNT", TokenConstants.TCOUNT, -1);
                addEntry("COUNTA", TokenConstants.TCOUNTA, -1);
                addEntry("MAX", TokenConstants.TMAX, -1 );
                addEntry("MIN", TokenConstants.TMIN, -1);
                addEntry("STDEV", TokenConstants.TSTDEV, -1 );
                addEntry("STDEVP", TokenConstants.TSTDEVP, -1 );
                addEntry("VAR", TokenConstants.TVAR, -1);
                addEntry("VARP", TokenConstants.TVARP, -1);
                // Lookup
                addEntry("CHOOSE", TokenConstants.TCHOOSE, -1);
                addEntry("HLOOKUP", TokenConstants.THLOOKUP, -1);
                addEntry("INDEX", TokenConstants.TINDEX, -1);
                addEntry("MATCH", TokenConstants.TMATCH, -1) ;
                addEntry("VLOOKUP", TokenConstants.TVLOOKUP, -1);
                // Text
                addEntry("RIGHT", TokenConstants.TRIGHT, -1);
                addEntry("SUBSTITUTE", TokenConstants.TSUBSTITUTE, -1);
                addEntry("FIND", TokenConstants.TFIND, -1);
                addEntry("LEFT", TokenConstants.TLEFT, -1);
                // Logical
                addEntry("AND", TokenConstants.TAND, -1 );
                addEntry("IF", TokenConstants.TIF, -1) ;
                addEntry("OR", TokenConstants.TOR, -1);

                // Functions with Fixed number of Arguments
                // Math and Trig
                addEntry("ABS", TokenConstants.TABS, 1);
                addEntry("ACOS", TokenConstants.TACOS, 1);
                addEntry("ASIN", TokenConstants.TASIN, 1);
                addEntry("ATAN", TokenConstants.TATAN, 1);
                addEntry("ATAN2", TokenConstants.TATAN2, 1);
                addEntry("COS", TokenConstants.TCOS, 1);
                addEntry("COUNTIF", TokenConstants.TCOUNTIF, 1);
                addEntry("DEGREES", TokenConstants.TDEGREES, 1);
                addEntry("EXP", TokenConstants.TEXP, 1);
                addEntry("FACT", TokenConstants.TFACT, 1);
                addEntry("INT", TokenConstants.TINTE, 1);
                addEntry("LN", TokenConstants.TLN, 1);
                addEntry("LOG10", TokenConstants.TLOG10, 1);
                addEntry("MOD", TokenConstants.TMOD, 1);
                addEntry("PI", TokenConstants.TPI, 0);
                addEntry("POWER", TokenConstants.TPOWERF, 2);
                addEntry("RADIANS", TokenConstants.TRADIANS, 1);
                addEntry("RAND", TokenConstants.TRAND, 1);
                addEntry("ROUND", TokenConstants.TROUND, 1);
                addEntry("SQRT", TokenConstants.TSQRT, 1);
                addEntry("TAN", TokenConstants.TTAN, 1);
                addEntry("SIN", TokenConstants.TSIN, 1);
                // Financial
                addEntry("SLN", TokenConstants.TSLN, 3);
                addEntry("SYD", TokenConstants.TSYD, 4);
                // Date and Time
                addEntry("DATE", TokenConstants.TDATE, 3);
                addEntry("DATEVALUE", TokenConstants.TDATEVALUE, 1);
                addEntry("DAY", TokenConstants.TDAY, 1);
                addEntry("HOUR", TokenConstants.THOUR, 1);
                addEntry("MINUTE", TokenConstants.TMINUTE, 1 );
                addEntry("MONTH", TokenConstants.TMONTH, 1);
                addEntry("NOW", TokenConstants.TNOW, 0);
                addEntry("SECOND", TokenConstants.TSECOND, 1);
                addEntry("TIME", TokenConstants.TTIME, 3);
                addEntry("TIMEVALUE", TokenConstants.TTIMEVALUE, 1);
                addEntry("YEAR", TokenConstants.TYEAR, 1);
                // Statistical
                addEntry("COUNTBLANK", TokenConstants.TCOUNTBLANK, 1);
                // lookup
                addEntry("COLUMNS", TokenConstants.TCOLUMNS, 1);
                addEntry("ROWS", TokenConstants.TROWS, 1);
                // Database
                addEntry("DAVERAGE", TokenConstants.TDAVAERAGE, 3);
                addEntry("DCOUNT", TokenConstants.TDCOUNT, 3);
                addEntry("DCOUNTA", TokenConstants.TDCOUNTA, 2);
                addEntry("DGET", TokenConstants.TDGET, 3);
                addEntry("DMAX", TokenConstants.TDMAX, 3);
                addEntry("DMIN", TokenConstants.TDMIN, 3);
                addEntry("DPRODUCT", TokenConstants.TDPRODUCT, 3);
                addEntry("DSTDEV", TokenConstants.TDSTDEV, 3);
                addEntry("DSTDEVP", TokenConstants.TDSTDEVP, 3) ;
                addEntry("DSUM", TokenConstants.TDSUM, 3);
                addEntry("DVAR", TokenConstants.TDVAR, 3);
                addEntry("DVARP", TokenConstants.TDVARP, 3);
                // Text
                addEntry("EXACT", TokenConstants.TEXACT, 2);
                addEntry("LEN", TokenConstants.TLEN, 1);
                addEntry("LOWER", TokenConstants.TLOWER, 1);
                addEntry("MID", TokenConstants.TMID, 3); // ??????
                addEntry("PROPER", TokenConstants.TPROPER, 1);
                addEntry("REPLACE", TokenConstants.TREPLACE, 4);
                addEntry("REPT", TokenConstants.TREPT, 2);
                addEntry("T", TokenConstants.TT, 1);
                addEntry("TRIM", TokenConstants.TRIM, 1);
                addEntry("UPPER", TokenConstants.TUPPER, 1);
                addEntry("VALUE", TokenConstants.TVALUE, 1);
                // Logical
                addEntry("FALSE", TokenConstants.TFALSE, 0);
                addEntry("NOT", TokenConstants.TNOT, 1);
                addEntry("TRUE", TokenConstants.TTRUE, 0);
                // Informational
                addEntry("ERRORTYPE", TokenConstants.TERRORTYPE, 1);
                addEntry("ISBLANK", TokenConstants.TISBLANK, 1);
                addEntry("ISERR", TokenConstants.TISERR, 1);
                addEntry("ISERROR", TokenConstants.TISERROR, 1);
                addEntry("ISLOGICAL", TokenConstants.TISLOGICAL, 1);
                addEntry("ISNA", TokenConstants.TISNA, 1);
                addEntry("ISNONTEXT", TokenConstants.TISNONTEXT, 1);
                addEntry("ISNUMBER", TokenConstants.TISNUMBER, 1);
                addEntry("ISTEXT", TokenConstants.TISTEXT, 1);
                addEntry("N", TokenConstants.TN, 1);
                addEntry("NA", TokenConstants.TNA, 0);

    }

    /**
     * Associate a function with an identifier and specifiy the number of arguments for that function
     * @param symbol    The function string that will act as the key in the lookup table
     * @param id        The identifier for the function
     * @param args      The number of arguments this function requires
     */
    public void addEntry(String symbol, int id, int args) {
        addEntry(symbol, id);
        stringToArgs.put(symbol, new Integer(args));
    }

    /**
     * Retrieve the number of arguments for this function
     * @param   symbol  The function name
     * @return  The number of arguments required by this function
     */
    public int getArgCountFromString(String symbol) {
        return stringToArgs.get(symbol).intValue();
    }
}