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.

DefaultNumberFunctions.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Copyright (c) 2017 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl.expr;
  14. import java.math.BigDecimal;
  15. import com.healthmarketscience.jackcess.expr.EvalContext;
  16. import com.healthmarketscience.jackcess.expr.EvalException;
  17. import com.healthmarketscience.jackcess.expr.Function;
  18. import com.healthmarketscience.jackcess.expr.Value;
  19. import static com.healthmarketscience.jackcess.impl.expr.DefaultFunctions.*;
  20. /**
  21. *
  22. * @author James Ahlborn
  23. */
  24. public class DefaultNumberFunctions
  25. {
  26. private DefaultNumberFunctions() {}
  27. static void init() {
  28. // dummy method to ensure this class is loaded
  29. }
  30. public static final Function ABS = registerFunc(new Func1NullIsNull("Abs") {
  31. @Override
  32. protected Value eval1(EvalContext ctx, Value param1) {
  33. Value.Type mathType = param1.getType();
  34. switch(mathType) {
  35. case DATE:
  36. case TIME:
  37. case DATE_TIME:
  38. // dates/times get converted to date doubles for arithmetic
  39. double result = Math.abs(param1.getAsDouble());
  40. return BuiltinOperators.toDateValue(ctx, mathType, result, param1, null);
  41. case LONG:
  42. return BuiltinOperators.toValue(Math.abs(param1.getAsLongInt()));
  43. case DOUBLE:
  44. return BuiltinOperators.toValue(Math.abs(param1.getAsDouble()));
  45. case STRING:
  46. case BIG_DEC:
  47. return BuiltinOperators.toValue(param1.getAsBigDecimal().abs());
  48. default:
  49. throw new EvalException("Unexpected type " + mathType);
  50. }
  51. }
  52. });
  53. public static final Function ATAN = registerFunc(new Func1("Atan") {
  54. @Override
  55. protected Value eval1(EvalContext ctx, Value param1) {
  56. return BuiltinOperators.toValue(Math.atan(param1.getAsDouble()));
  57. }
  58. });
  59. public static final Function COS = registerFunc(new Func1("Cos") {
  60. @Override
  61. protected Value eval1(EvalContext ctx, Value param1) {
  62. return BuiltinOperators.toValue(Math.cos(param1.getAsDouble()));
  63. }
  64. });
  65. public static final Function EXP = registerFunc(new Func1("Exp") {
  66. @Override
  67. protected Value eval1(EvalContext ctx, Value param1) {
  68. return BuiltinOperators.toValue(Math.exp(param1.getAsDouble()));
  69. }
  70. });
  71. public static final Function FIX = registerFunc(new Func1NullIsNull("Fix") {
  72. @Override
  73. protected Value eval1(EvalContext ctx, Value param1) {
  74. if(param1.getType().isIntegral()) {
  75. return param1;
  76. }
  77. return BuiltinOperators.toValue(param1.getAsDouble().intValue());
  78. }
  79. });
  80. public static final Function INT = registerFunc(new Func1NullIsNull("Int") {
  81. @Override
  82. protected Value eval1(EvalContext ctx, Value param1) {
  83. if(param1.getType().isIntegral()) {
  84. return param1;
  85. }
  86. return BuiltinOperators.toValue((int)Math.floor(param1.getAsDouble()));
  87. }
  88. });
  89. public static final Function LOG = registerFunc(new Func1("Log") {
  90. @Override
  91. protected Value eval1(EvalContext ctx, Value param1) {
  92. return BuiltinOperators.toValue(Math.log(param1.getAsDouble()));
  93. }
  94. });
  95. public static final Function RND = registerFunc(new FuncVar("Rnd", 0, 1) {
  96. @Override
  97. public boolean isPure() {
  98. return false;
  99. }
  100. @Override
  101. protected Value evalVar(EvalContext ctx, Value[] params) {
  102. Integer seed = ((params.length > 0) ? params[0].getAsLongInt() : null);
  103. return BuiltinOperators.toValue(ctx.getRandom(seed));
  104. }
  105. });
  106. public static final Function ROUND = registerFunc(new FuncVar("Round", 1, 2) {
  107. @Override
  108. protected Value evalVar(EvalContext ctx, Value[] params) {
  109. Value param1 = params[0];
  110. if(param1.isNull()) {
  111. return null;
  112. }
  113. if(param1.getType().isIntegral()) {
  114. return param1;
  115. }
  116. int scale = 0;
  117. if(params.length > 1) {
  118. scale = params[1].getAsLongInt();
  119. }
  120. BigDecimal bd = param1.getAsBigDecimal()
  121. .setScale(scale, BuiltinOperators.ROUND_MODE);
  122. return BuiltinOperators.toValue(bd);
  123. }
  124. });
  125. public static final Function SGN = registerFunc(new Func1NullIsNull("Sgn") {
  126. @Override
  127. protected Value eval1(EvalContext ctx, Value param1) {
  128. int signum = 0;
  129. if(param1.getType().isIntegral()) {
  130. int lv = param1.getAsLongInt();
  131. signum = ((lv > 0) ? 1 : ((lv < 0) ? -1 : 0));
  132. } else {
  133. signum = param1.getAsBigDecimal().signum();
  134. }
  135. return BuiltinOperators.toValue(signum);
  136. }
  137. });
  138. public static final Function SQR = registerFunc(new Func1("Sqr") {
  139. @Override
  140. protected Value eval1(EvalContext ctx, Value param1) {
  141. double dv = param1.getAsDouble();
  142. if(dv < 0.0d) {
  143. throw new EvalException("Invalid value '" + dv + "'");
  144. }
  145. return BuiltinOperators.toValue(Math.sqrt(dv));
  146. }
  147. });
  148. public static final Function SIN = registerFunc(new Func1("Sin") {
  149. @Override
  150. protected Value eval1(EvalContext ctx, Value param1) {
  151. return BuiltinOperators.toValue(Math.sin(param1.getAsDouble()));
  152. }
  153. });
  154. public static final Function TAN = registerFunc(new Func1("Tan") {
  155. @Override
  156. protected Value eval1(EvalContext ctx, Value param1) {
  157. return BuiltinOperators.toValue(Math.tan(param1.getAsDouble()));
  158. }
  159. });
  160. // public static final Function Val = registerFunc(new Func1("Val") {
  161. // @Override
  162. // protected Value eval1(EvalContext ctx, Value param1) {
  163. // // FIXME, maybe leverage ExpressionTokenizer.maybeParseNumberLiteral (note, leading - or + is valid, exponent form is valid)
  164. // }
  165. // });
  166. }