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.

UserDefinedFunction.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula;
  16. import org.apache.poi.ss.formula.eval.FunctionNameEval;
  17. import org.apache.poi.ss.formula.eval.NotImplementedFunctionException;
  18. import org.apache.poi.ss.formula.eval.ValueEval;
  19. import org.apache.poi.ss.formula.functions.FreeRefFunction;
  20. /**
  21. *
  22. * Common entry point for all user-defined (non-built-in) functions (where
  23. * <tt>AbstractFunctionPtg.field_2_fnc_index</tt> == 255)
  24. *
  25. * @author Josh Micich
  26. * @author Petr Udalau - Improved resolving of UDFs through the ToolPacks.
  27. */
  28. final class UserDefinedFunction implements FreeRefFunction {
  29. public static final FreeRefFunction instance = new UserDefinedFunction();
  30. private UserDefinedFunction() {
  31. // enforce singleton
  32. }
  33. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  34. int nIncomingArgs = args.length;
  35. if(nIncomingArgs < 1) {
  36. throw new RuntimeException("function name argument missing");
  37. }
  38. ValueEval nameArg = args[0];
  39. String functionName;
  40. if (nameArg instanceof FunctionNameEval) {
  41. functionName = ((FunctionNameEval) nameArg).getFunctionName();
  42. } else {
  43. throw new RuntimeException("First argument should be a NameEval, but got ("
  44. + nameArg.getClass().getName() + ")");
  45. }
  46. FreeRefFunction targetFunc = ec.findUserDefinedFunction(functionName);
  47. if (targetFunc == null) {
  48. throw new NotImplementedFunctionException(functionName);
  49. }
  50. int nOutGoingArgs = nIncomingArgs -1;
  51. ValueEval[] outGoingArgs = new ValueEval[nOutGoingArgs];
  52. System.arraycopy(args, 1, outGoingArgs, 0, nOutGoingArgs);
  53. return targetFunc.evaluate(outGoingArgs, ec);
  54. }
  55. }