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 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 org.apache.poi.ss.formula.OperationEvaluationContext;
  12. import org.apache.poi.ss.formula.eval.NotImplementedException;
  13. import org.apache.poi.ss.formula.eval.ValueEval;
  14. import org.apache.poi.ss.formula.function.FunctionMetadata;
  15. import org.apache.poi.ss.formula.function.FunctionMetadataRegistry;
  16. import org.apache.poi.ss.formula.functions.*;
  17. import org.apache.poi.ss.formula.udf.UDFFinder;
  18. import java.util.*;
  19. /**
  20. * Analysis Toolpack Function Definitions
  21. */
  22. public final class AnalysisToolPak implements UDFFinder {
  23. public static final UDFFinder instance = new AnalysisToolPak();
  24. private static final class NotImplemented implements FreeRefFunction {
  25. private final String _functionName;
  26. public NotImplemented(String functionName) {
  27. _functionName = functionName;
  28. }
  29. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  30. throw new NotImplementedException(_functionName);
  31. }
  32. }
  33. private final Map<String, FreeRefFunction> _functionsByName = createFunctionsMap();
  34. private AnalysisToolPak() {
  35. // enforce singleton
  36. }
  37. public FreeRefFunction findFunction(String name) {
  38. // functions that are available in Excel 2007+ have a prefix _xlfn.
  39. // if you save such a .xlsx workbook as .xls
  40. if(name.startsWith("_xlfn.")) name = name.substring(6);
  41. return _functionsByName.get(name.toUpperCase());
  42. }
  43. private Map<String, FreeRefFunction> createFunctionsMap() {
  44. Map<String, FreeRefFunction> m = new HashMap<String, FreeRefFunction>(108);
  45. r(m, "ACCRINT", null);
  46. r(m, "ACCRINTM", null);
  47. r(m, "AMORDEGRC", null);
  48. r(m, "AMORLINC", null);
  49. r(m, "AVERAGEIF", null);
  50. r(m, "AVERAGEIFS", null);
  51. r(m, "BAHTTEXT", null);
  52. r(m, "BESSELI", null);
  53. r(m, "BESSELJ", null);
  54. r(m, "BESSELK", null);
  55. r(m, "BESSELY", null);
  56. r(m, "BIN2DEC", null);
  57. r(m, "BIN2HEX", null);
  58. r(m, "BIN2OCT", null);
  59. r(m, "COMPLEX", Complex.instance);
  60. r(m, "CONVERT", null);
  61. r(m, "COUNTIFS", null);
  62. r(m, "COUPDAYBS", null);
  63. r(m, "COUPDAYS", null);
  64. r(m, "COUPDAYSNC", null);
  65. r(m, "COUPNCD", null);
  66. r(m, "COUPNUM", null);
  67. r(m, "COUPPCD", null);
  68. r(m, "CUBEKPIMEMBER", null);
  69. r(m, "CUBEMEMBER", null);
  70. r(m, "CUBEMEMBERPROPERTY", null);
  71. r(m, "CUBERANKEDMEMBER", null);
  72. r(m, "CUBESET", null);
  73. r(m, "CUBESETCOUNT", null);
  74. r(m, "CUBEVALUE", null);
  75. r(m, "CUMIPMT", null);
  76. r(m, "CUMPRINC", null);
  77. r(m, "DEC2BIN", null);
  78. r(m, "DEC2HEX", null);
  79. r(m, "DEC2OCT", null);
  80. r(m, "DELTA", Delta.instance);
  81. r(m, "DISC", null);
  82. r(m, "DOLLARDE", null);
  83. r(m, "DOLLARFR", null);
  84. r(m, "DURATION", null);
  85. r(m, "EDATE", EDate.instance);
  86. r(m, "EFFECT", null);
  87. r(m, "EOMONTH", null);
  88. r(m, "ERF", null);
  89. r(m, "ERFC", null);
  90. r(m, "FACTDOUBLE", null);
  91. r(m, "FVSCHEDULE", null);
  92. r(m, "GCD", null);
  93. r(m, "GESTEP", null);
  94. r(m, "HEX2BIN", null);
  95. r(m, "HEX2DEC", null);
  96. r(m, "HEX2OCT", null);
  97. r(m, "IFERROR", IfError.instance);
  98. r(m, "IMABS", null);
  99. r(m, "IMAGINARY", Imaginary.instance);
  100. r(m, "IMARGUMENT", null);
  101. r(m, "IMCONJUGATE", null);
  102. r(m, "IMCOS", null);
  103. r(m, "IMDIV", null);
  104. r(m, "IMEXP", null);
  105. r(m, "IMLN", null);
  106. r(m, "IMLOG10", null);
  107. r(m, "IMLOG2", null);
  108. r(m, "IMPOWER", null);
  109. r(m, "IMPRODUCT", null);
  110. r(m, "IMREAL", ImReal.instance);
  111. r(m, "IMSIN", null);
  112. r(m, "IMSQRT", null);
  113. r(m, "IMSUB", null);
  114. r(m, "IMSUM", null);
  115. r(m, "INTRATE", null);
  116. r(m, "ISEVEN", ParityFunction.IS_EVEN);
  117. r(m, "ISODD", ParityFunction.IS_ODD);
  118. r(m, "JIS", null);
  119. r(m, "LCM", null);
  120. r(m, "MDURATION", null);
  121. r(m, "MROUND", MRound.instance);
  122. r(m, "MULTINOMIAL", null);
  123. r(m, "NETWORKDAYS", NetworkdaysFunction.instance);
  124. r(m, "NOMINAL", null);
  125. r(m, "OCT2BIN", null);
  126. r(m, "OCT2DEC", null);
  127. r(m, "OCT2HEX", null);
  128. r(m, "ODDFPRICE", null);
  129. r(m, "ODDFYIELD", null);
  130. r(m, "ODDLPRICE", null);
  131. r(m, "ODDLYIELD", null);
  132. r(m, "PRICE", null);
  133. r(m, "PRICEDISC", null);
  134. r(m, "PRICEMAT", null);
  135. r(m, "QUOTIENT", Quotient.instance);
  136. r(m, "RANDBETWEEN", RandBetween.instance);
  137. r(m, "RECEIVED", null);
  138. r(m, "RTD", null);
  139. r(m, "SERIESSUM", null);
  140. r(m, "SQRTPI", null);
  141. r(m, "SUMIFS", Sumifs.instance);
  142. r(m, "TBILLEQ", null);
  143. r(m, "TBILLPRICE", null);
  144. r(m, "TBILLYIELD", null);
  145. r(m, "WEEKNUM", WeekNum.instance);
  146. r(m, "WORKDAY", WorkdayFunction.instance);
  147. r(m, "XIRR", null);
  148. r(m, "XNPV", null);
  149. r(m, "YEARFRAC", YearFrac.instance);
  150. r(m, "YIELD", null);
  151. r(m, "YIELDDISC", null);
  152. r(m, "YIELDMAT", null);
  153. return m;
  154. }
  155. private static void r(Map<String, FreeRefFunction> m, String functionName, FreeRefFunction pFunc) {
  156. FreeRefFunction func = pFunc == null ? new NotImplemented(functionName) : pFunc;
  157. m.put(functionName, func);
  158. }
  159. public static boolean isATPFunction(String name){
  160. AnalysisToolPak inst = (AnalysisToolPak)instance;
  161. return inst._functionsByName.containsKey(name);
  162. }
  163. /**
  164. * Returns a collection of ATP function names implemented by POI.
  165. *
  166. * @return an array of supported functions
  167. * @since 3.8 beta6
  168. */
  169. public static Collection<String> getSupportedFunctionNames(){
  170. AnalysisToolPak inst = (AnalysisToolPak)instance;
  171. Collection<String> lst = new TreeSet<String>();
  172. for(String name : inst._functionsByName.keySet()){
  173. FreeRefFunction func = inst._functionsByName.get(name);
  174. if(func != null && !(func instanceof NotImplemented)){
  175. lst.add(name);
  176. }
  177. }
  178. return Collections.unmodifiableCollection(lst);
  179. }
  180. /**
  181. * Returns a collection of ATP function names NOT implemented by POI.
  182. *
  183. * @return an array of not supported functions
  184. * @since 3.8 beta6
  185. */
  186. public static Collection<String> getNotSupportedFunctionNames(){
  187. AnalysisToolPak inst = (AnalysisToolPak)instance;
  188. Collection<String> lst = new TreeSet<String>();
  189. for(String name : inst._functionsByName.keySet()){
  190. FreeRefFunction func = inst._functionsByName.get(name);
  191. if(func != null && (func instanceof NotImplemented)){
  192. lst.add(name);
  193. }
  194. }
  195. return Collections.unmodifiableCollection(lst);
  196. }
  197. /**
  198. * Register a ATP function in runtime.
  199. *
  200. * @param name the function name
  201. * @param func the functoin to register
  202. * @throws IllegalArgumentException if the function is unknown or already registered.
  203. * @since 3.8 beta6
  204. */
  205. public static void registerFunction(String name, FreeRefFunction func){
  206. AnalysisToolPak inst = (AnalysisToolPak)instance;
  207. if(!isATPFunction(name)) {
  208. FunctionMetadata metaData = FunctionMetadataRegistry.getFunctionByName(name);
  209. if(metaData != null) {
  210. throw new IllegalArgumentException(name + " is a built-in Excel function. " +
  211. "Use FunctoinEval.registerFunction(String name, Function func) instead.");
  212. }
  213. throw new IllegalArgumentException(name + " is not a function from the Excel Analysis Toolpack.");
  214. }
  215. FreeRefFunction f = inst.findFunction(name);
  216. if(f != null && !(f instanceof NotImplemented)) {
  217. throw new IllegalArgumentException("POI already implememts " + name +
  218. ". You cannot override POI's implementations of Excel functions");
  219. }
  220. inst._functionsByName.put(name, func);
  221. }
  222. }