You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnalysisToolPak.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership.
  4. * 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
  5. * with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or
  6. * agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  7. * KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  8. * ====================================================================
  9. */
  10. package org.apache.poi.ss.formula.atp;
  11. import java.util.Collection;
  12. import java.util.Collections;
  13. import java.util.HashMap;
  14. import java.util.Locale;
  15. import java.util.Map;
  16. import java.util.TreeSet;
  17. import org.apache.poi.ss.formula.OperationEvaluationContext;
  18. import org.apache.poi.ss.formula.eval.NotImplementedFunctionException;
  19. import org.apache.poi.ss.formula.eval.ValueEval;
  20. import org.apache.poi.ss.formula.function.FunctionMetadata;
  21. import org.apache.poi.ss.formula.function.FunctionMetadataRegistry;
  22. import org.apache.poi.ss.formula.functions.Bin2Dec;
  23. import org.apache.poi.ss.formula.functions.Complex;
  24. import org.apache.poi.ss.formula.functions.Countifs;
  25. import org.apache.poi.ss.formula.functions.Dec2Bin;
  26. import org.apache.poi.ss.formula.functions.Dec2Hex;
  27. import org.apache.poi.ss.formula.functions.Delta;
  28. import org.apache.poi.ss.formula.functions.EDate;
  29. import org.apache.poi.ss.formula.functions.EOMonth;
  30. import org.apache.poi.ss.formula.functions.FactDouble;
  31. import org.apache.poi.ss.formula.functions.FreeRefFunction;
  32. import org.apache.poi.ss.formula.functions.Hex2Dec;
  33. import org.apache.poi.ss.formula.functions.ImReal;
  34. import org.apache.poi.ss.formula.functions.Imaginary;
  35. import org.apache.poi.ss.formula.functions.Oct2Dec;
  36. import org.apache.poi.ss.formula.functions.Quotient;
  37. import org.apache.poi.ss.formula.functions.Single;
  38. import org.apache.poi.ss.formula.functions.Sumifs;
  39. import org.apache.poi.ss.formula.functions.TextFunction;
  40. import org.apache.poi.ss.formula.functions.WeekNum;
  41. import org.apache.poi.ss.formula.udf.UDFFinder;
  42. /**
  43. * Analysis Toolpack Function Definitions
  44. */
  45. public final class AnalysisToolPak implements UDFFinder {
  46. public static final UDFFinder instance = new AnalysisToolPak();
  47. private static final class NotImplemented implements FreeRefFunction {
  48. private final String _functionName;
  49. public NotImplemented(String functionName) {
  50. _functionName = functionName;
  51. }
  52. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  53. throw new NotImplementedFunctionException(_functionName);
  54. }
  55. }
  56. private final Map<String, FreeRefFunction> _functionsByName = createFunctionsMap();
  57. private AnalysisToolPak() {
  58. // enforce singleton
  59. }
  60. public FreeRefFunction findFunction(String name) {
  61. // functions that are available in Excel 2007+ have a prefix _xlfn.
  62. // if you save such a .xlsx workbook as .xls
  63. final String prefix = "_xlfn.";
  64. // case-sensitive
  65. if(name.startsWith(prefix)) name = name.substring(prefix.length());
  66. // FIXME: inconsistent case-sensitivity
  67. return _functionsByName.get(name.toUpperCase(Locale.ROOT));
  68. }
  69. private Map<String, FreeRefFunction> createFunctionsMap() {
  70. Map<String, FreeRefFunction> m = new HashMap<>(127);
  71. r(m, "ACCRINT", null);
  72. r(m, "ACCRINTM", null);
  73. r(m, "AMORDEGRC", null);
  74. r(m, "AMORLINC", null);
  75. r(m, "AVERAGEIF", null);
  76. r(m, "AVERAGEIFS", null);
  77. r(m, "BAHTTEXT", null);
  78. r(m, "BESSELI", null);
  79. r(m, "BESSELJ", null);
  80. r(m, "BESSELK", null);
  81. r(m, "BESSELY", null);
  82. r(m, "BIN2DEC", Bin2Dec.instance);
  83. r(m, "BIN2HEX", null);
  84. r(m, "BIN2OCT", null);
  85. r(m, "COMPLEX", Complex.instance);
  86. r(m, "CONCAT", TextFunction.CONCAT);
  87. r(m, "CONVERT", null);
  88. r(m, "COUNTIFS", Countifs.instance);
  89. r(m, "COUPDAYBS", null);
  90. r(m, "COUPDAYS", null);
  91. r(m, "COUPDAYSNC", null);
  92. r(m, "COUPNCD", null);
  93. r(m, "COUPNUM", null);
  94. r(m, "COUPPCD", null);
  95. r(m, "CUBEKPIMEMBER", null);
  96. r(m, "CUBEMEMBER", null);
  97. r(m, "CUBEMEMBERPROPERTY", null);
  98. r(m, "CUBERANKEDMEMBER", null);
  99. r(m, "CUBESET", null);
  100. r(m, "CUBESETCOUNT", null);
  101. r(m, "CUBEVALUE", null);
  102. r(m, "CUMIPMT", null);
  103. r(m, "CUMPRINC", null);
  104. r(m, "DEC2BIN", Dec2Bin.instance);
  105. r(m, "DEC2HEX", Dec2Hex.instance);
  106. r(m, "DEC2OCT", null);
  107. r(m, "DELTA", Delta.instance);
  108. r(m, "DISC", null);
  109. r(m, "DOLLARDE", null);
  110. r(m, "DOLLARFR", null);
  111. r(m, "DURATION", null);
  112. r(m, "EDATE", EDate.instance);
  113. r(m, "EFFECT", null);
  114. r(m, "EOMONTH", EOMonth.instance);
  115. r(m, "ERF", null);
  116. r(m, "ERFC", null);
  117. r(m, "FACTDOUBLE", FactDouble.instance);
  118. r(m, "FVSCHEDULE", null);
  119. r(m, "GCD", null);
  120. r(m, "GESTEP", null);
  121. r(m, "HEX2BIN", null);
  122. r(m, "HEX2DEC", Hex2Dec.instance);
  123. r(m, "HEX2OCT", null);
  124. r(m, "IFERROR", IfError.instance);
  125. r(m, "IFNA", IfNa.instance);
  126. r(m, "IFS", Ifs.instance);
  127. r(m, "IMABS", null);
  128. r(m, "IMAGINARY", Imaginary.instance);
  129. r(m, "IMARGUMENT", null);
  130. r(m, "IMCONJUGATE", null);
  131. r(m, "IMCOS", null);
  132. r(m, "IMDIV", null);
  133. r(m, "IMEXP", null);
  134. r(m, "IMLN", null);
  135. r(m, "IMLOG10", null);
  136. r(m, "IMLOG2", null);
  137. r(m, "IMPOWER", null);
  138. r(m, "IMPRODUCT", null);
  139. r(m, "IMREAL", ImReal.instance);
  140. r(m, "IMSIN", null);
  141. r(m, "IMSQRT", null);
  142. r(m, "IMSUB", null);
  143. r(m, "IMSUM", null);
  144. r(m, "INTRATE", null);
  145. r(m, "ISEVEN", ParityFunction.IS_EVEN);
  146. r(m, "ISODD", ParityFunction.IS_ODD);
  147. r(m, "JIS", null);
  148. r(m, "LCM", null);
  149. r(m, "MDURATION", null);
  150. r(m, "MROUND", MRound.instance);
  151. r(m, "MULTINOMIAL", null);
  152. r(m, "NETWORKDAYS", NetworkdaysFunction.instance);
  153. r(m, "NOMINAL", null);
  154. r(m, "OCT2BIN", null);
  155. r(m, "OCT2DEC", Oct2Dec.instance);
  156. r(m, "OCT2HEX", null);
  157. r(m, "ODDFPRICE", null);
  158. r(m, "ODDFYIELD", null);
  159. r(m, "ODDLPRICE", null);
  160. r(m, "ODDLYIELD", null);
  161. r(m, "PRICE", null);
  162. r(m, "PRICEDISC", null);
  163. r(m, "PRICEMAT", null);
  164. r(m, "QUOTIENT", Quotient.instance);
  165. r(m, "RANDBETWEEN", RandBetween.instance);
  166. r(m, "RECEIVED", null);
  167. r(m, "RTD", null);
  168. r(m, "SERIESSUM", null);
  169. r(m, "SINGLE", Single.instance);
  170. r(m, "SQRTPI", null);
  171. r(m, "SUMIFS", Sumifs.instance);
  172. r(m, "SWITCH", Switch.instance);
  173. r(m, "TBILLEQ", null);
  174. r(m, "TBILLPRICE", null);
  175. r(m, "TBILLYIELD", null);
  176. r(m, "TEXTJOIN", TextJoinFunction.instance);
  177. r(m, "WEEKNUM", WeekNum.instance);
  178. r(m, "WORKDAY", WorkdayFunction.instance);
  179. r(m, "XIRR", null);
  180. r(m, "XNPV", null);
  181. r(m, "YEARFRAC", YearFrac.instance);
  182. r(m, "YIELD", null);
  183. r(m, "YIELDDISC", null);
  184. r(m, "YIELDMAT", null);
  185. return m;
  186. }
  187. private static void r(Map<String, FreeRefFunction> m, String functionName, FreeRefFunction pFunc) {
  188. FreeRefFunction func = pFunc == null ? new NotImplemented(functionName) : pFunc;
  189. m.put(functionName, func);
  190. }
  191. public static boolean isATPFunction(String name){
  192. AnalysisToolPak inst = (AnalysisToolPak)instance;
  193. // FIXME: inconsistent case-sensitivity
  194. return inst._functionsByName.containsKey(name);
  195. }
  196. /**
  197. * Returns a collection of ATP function names implemented by POI.
  198. *
  199. * @return an array of supported functions
  200. * @since 3.8 beta6
  201. */
  202. public static Collection<String> getSupportedFunctionNames(){
  203. AnalysisToolPak inst = (AnalysisToolPak)instance;
  204. Collection<String> lst = new TreeSet<>();
  205. for(Map.Entry<String, FreeRefFunction> me : inst._functionsByName.entrySet()){
  206. FreeRefFunction func = me.getValue();
  207. if(func != null && !(func instanceof NotImplemented)){
  208. lst.add(me.getKey());
  209. }
  210. }
  211. return Collections.unmodifiableCollection(lst);
  212. }
  213. /**
  214. * Returns a collection of ATP function names NOT implemented by POI.
  215. *
  216. * @return an array of not supported functions
  217. * @since 3.8 beta6
  218. */
  219. public static Collection<String> getNotSupportedFunctionNames(){
  220. AnalysisToolPak inst = (AnalysisToolPak)instance;
  221. Collection<String> lst = new TreeSet<>();
  222. for(Map.Entry<String, FreeRefFunction> me : inst._functionsByName.entrySet()){
  223. FreeRefFunction func = me.getValue();
  224. if (func instanceof NotImplemented) {
  225. lst.add(me.getKey());
  226. }
  227. }
  228. return Collections.unmodifiableCollection(lst);
  229. }
  230. /**
  231. * Register a ATP function in runtime.
  232. *
  233. * @param name the function name
  234. * @param func the functoin to register
  235. * @throws IllegalArgumentException if the function is unknown or already registered.
  236. * @since 3.8 beta6
  237. */
  238. public static void registerFunction(String name, FreeRefFunction func){
  239. AnalysisToolPak inst = (AnalysisToolPak)instance;
  240. if(!isATPFunction(name)) {
  241. FunctionMetadata metaData = FunctionMetadataRegistry.getFunctionByName(name);
  242. if(metaData != null) {
  243. throw new IllegalArgumentException(name + " is a built-in Excel function. " +
  244. "Use FunctionEval.registerFunction(String name, Function func) instead.");
  245. }
  246. throw new IllegalArgumentException(name + " is not a function from the Excel Analysis Toolpack.");
  247. }
  248. FreeRefFunction f = inst.findFunction(name);
  249. if(f != null && !(f instanceof NotImplemented)) {
  250. throw new IllegalArgumentException("POI already implements " + name +
  251. ". You cannot override POI's implementations of Excel functions");
  252. }
  253. // FIXME: inconsistent case-sensitivity
  254. inst._functionsByName.put(name, func);
  255. }
  256. }