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.

Complex.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package org.apache.poi.ss.formula.functions;
  2. import org.apache.poi.ss.formula.OperationEvaluationContext;
  3. import org.apache.poi.ss.formula.eval.*;
  4. /**
  5. * Implementation for Excel COMPLEX () function.<p/>
  6. * <p/>
  7. * <b>Syntax</b>:<br/> <b>COMPLEX </b>(<b>real_num</b>,<b>i_num</b>,<b>suffix </b> )<br/>
  8. * <p/>
  9. * Converts real and imaginary coefficients into a complex number of the form x + yi or x + yj.
  10. * <p/>
  11. * <p/>
  12. * All complex number functions accept "i" and "j" for suffix, but neither "I" nor "J".
  13. * Using uppercase results in the #VALUE! error value. All functions that accept two
  14. * or more complex numbers require that all suffixes match.
  15. * <p/>
  16. * <b>real_num</b> The real coefficient of the complex number.
  17. * If this argument is nonnumeric, this function returns the #VALUE! error value.
  18. * <p/>
  19. * <p/>
  20. * <b>i_num</b> The imaginary coefficient of the complex number.
  21. * If this argument is nonnumeric, this function returns the #VALUE! error value.
  22. * <p/>
  23. * <p/>
  24. * <b>suffix</b> The suffix for the imaginary component of the complex number.
  25. * <ul>
  26. * <li>If omitted, suffix is assumed to be "i".</li>
  27. * <li>If suffix is neither "i" nor "j", COMPLEX returns the #VALUE! error value.</li>
  28. * </ul>
  29. *
  30. * @author cedric dot walter @ gmail dot com
  31. */
  32. public class Complex extends Var2or3ArgFunction implements FreeRefFunction {
  33. public static final FreeRefFunction instance = new Complex();
  34. public static final String DEFAULT_SUFFIX = "i";
  35. public static final String SUPPORTED_SUFFIX = "j";
  36. @Override
  37. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval real_num, ValueEval i_num) {
  38. return this.evaluate(srcRowIndex, srcColumnIndex, real_num, i_num, new StringEval(DEFAULT_SUFFIX));
  39. }
  40. @Override
  41. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval real_num, ValueEval i_num, ValueEval suffix) {
  42. ValueEval veText1;
  43. try {
  44. veText1 = OperandResolver.getSingleValue(real_num, srcRowIndex, srcColumnIndex);
  45. } catch (EvaluationException e) {
  46. return e.getErrorEval();
  47. }
  48. double realNum = 0;
  49. try {
  50. realNum = OperandResolver.coerceValueToDouble(veText1);
  51. } catch (EvaluationException e) {
  52. return ErrorEval.VALUE_INVALID;
  53. }
  54. ValueEval veINum;
  55. try {
  56. veINum = OperandResolver.getSingleValue(i_num, srcRowIndex, srcColumnIndex);
  57. } catch (EvaluationException e) {
  58. return e.getErrorEval();
  59. }
  60. double realINum = 0;
  61. try {
  62. realINum = OperandResolver.coerceValueToDouble(veINum);
  63. } catch (EvaluationException e) {
  64. return ErrorEval.VALUE_INVALID;
  65. }
  66. String suffixValue = OperandResolver.coerceValueToString(suffix);
  67. if (suffixValue.isEmpty()) {
  68. suffixValue = DEFAULT_SUFFIX;
  69. }
  70. if (suffixValue.equals(DEFAULT_SUFFIX.toUpperCase()) || suffixValue.equals(SUPPORTED_SUFFIX.toUpperCase())) {
  71. return ErrorEval.VALUE_INVALID;
  72. }
  73. if (!(suffixValue.equals(DEFAULT_SUFFIX) || suffixValue.equals(SUPPORTED_SUFFIX))) {
  74. return ErrorEval.VALUE_INVALID;
  75. }
  76. StringBuffer strb = new StringBuffer("");
  77. if (realNum != 0) {
  78. if (isDoubleAnInt(realNum)) {
  79. strb.append(new Double(realNum).intValue());
  80. } else {
  81. strb.append(realNum);
  82. }
  83. }
  84. if (realINum != 0) {
  85. if (strb.length() != 0) {
  86. if (realINum > 0) {
  87. strb.append("+");
  88. }
  89. }
  90. if (realINum != 1 && realINum != -1) {
  91. if (isDoubleAnInt(realINum)) {
  92. strb.append(new Double(realINum).intValue());
  93. } else {
  94. strb.append(realINum);
  95. }
  96. }
  97. strb.append(suffixValue);
  98. }
  99. return new StringEval(strb.toString());
  100. }
  101. /**
  102. * @param number
  103. * @return
  104. */
  105. private boolean isDoubleAnInt(double number) {
  106. return (number == Math.floor(number)) && !Double.isInfinite(number);
  107. }
  108. @Override
  109. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  110. if (args.length == 2) {
  111. return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]);
  112. }
  113. if (args.length == 3) {
  114. return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1], args[2]);
  115. }
  116. return ErrorEval.VALUE_INVALID;
  117. }
  118. }