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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 com.healthmarketscience.jackcess.impl.NumberFormatter;
  20. import static com.healthmarketscience.jackcess.impl.expr.DefaultFunctions.*;
  21. /**
  22. *
  23. * @author James Ahlborn
  24. */
  25. public class DefaultNumberFunctions
  26. {
  27. private DefaultNumberFunctions() {}
  28. static void init() {
  29. // dummy method to ensure this class is loaded
  30. }
  31. public static final Function ABS = registerFunc(new Func1NullIsNull("Abs") {
  32. @Override
  33. protected Value eval1(EvalContext ctx, Value param1) {
  34. Value.Type mathType = param1.getType();
  35. switch(mathType) {
  36. case DATE:
  37. case TIME:
  38. case DATE_TIME:
  39. // dates/times get converted to date doubles for arithmetic
  40. double result = Math.abs(param1.getAsDouble());
  41. return BuiltinOperators.toDateValue(ctx, mathType, result, param1, null);
  42. case LONG:
  43. return BuiltinOperators.toValue(Math.abs(param1.getAsLongInt()));
  44. case DOUBLE:
  45. return BuiltinOperators.toValue(Math.abs(param1.getAsDouble()));
  46. case STRING:
  47. case BIG_DEC:
  48. return BuiltinOperators.toValue(param1.getAsBigDecimal().abs(
  49. NumberFormatter.DEC_MATH_CONTEXT));
  50. default:
  51. throw new EvalException("Unexpected type " + mathType);
  52. }
  53. }
  54. });
  55. public static final Function ATAN = registerFunc(new Func1("Atan") {
  56. @Override
  57. protected Value eval1(EvalContext ctx, Value param1) {
  58. return BuiltinOperators.toValue(Math.atan(param1.getAsDouble()));
  59. }
  60. });
  61. public static final Function COS = registerFunc(new Func1("Cos") {
  62. @Override
  63. protected Value eval1(EvalContext ctx, Value param1) {
  64. return BuiltinOperators.toValue(Math.cos(param1.getAsDouble()));
  65. }
  66. });
  67. public static final Function EXP = registerFunc(new Func1("Exp") {
  68. @Override
  69. protected Value eval1(EvalContext ctx, Value param1) {
  70. return BuiltinOperators.toValue(Math.exp(param1.getAsDouble()));
  71. }
  72. });
  73. public static final Function FIX = registerFunc(new Func1NullIsNull("Fix") {
  74. @Override
  75. protected Value eval1(EvalContext ctx, Value param1) {
  76. if(param1.getType().isIntegral()) {
  77. return param1;
  78. }
  79. return BuiltinOperators.toValue(param1.getAsDouble().intValue());
  80. }
  81. });
  82. public static final Function INT = registerFunc(new Func1NullIsNull("Int") {
  83. @Override
  84. protected Value eval1(EvalContext ctx, Value param1) {
  85. if(param1.getType().isIntegral()) {
  86. return param1;
  87. }
  88. return BuiltinOperators.toValue((int)Math.floor(param1.getAsDouble()));
  89. }
  90. });
  91. public static final Function LOG = registerFunc(new Func1("Log") {
  92. @Override
  93. protected Value eval1(EvalContext ctx, Value param1) {
  94. return BuiltinOperators.toValue(Math.log(param1.getAsDouble()));
  95. }
  96. });
  97. public static final Function RND = registerFunc(new FuncVar("Rnd", 0, 1) {
  98. @Override
  99. public boolean isPure() {
  100. return false;
  101. }
  102. @Override
  103. protected Value evalVar(EvalContext ctx, Value[] params) {
  104. Integer seed = ((params.length > 0) ? params[0].getAsLongInt() : null);
  105. return BuiltinOperators.toValue(ctx.getRandom(seed));
  106. }
  107. });
  108. public static final Function ROUND = registerFunc(new FuncVar("Round", 1, 2) {
  109. @Override
  110. protected Value evalVar(EvalContext ctx, Value[] params) {
  111. Value param1 = params[0];
  112. if(param1.isNull()) {
  113. return null;
  114. }
  115. if(param1.getType().isIntegral()) {
  116. return param1;
  117. }
  118. int scale = 0;
  119. if(params.length > 1) {
  120. scale = params[1].getAsLongInt();
  121. }
  122. BigDecimal bd = param1.getAsBigDecimal()
  123. .setScale(scale, NumberFormatter.ROUND_MODE);
  124. return BuiltinOperators.toValue(bd);
  125. }
  126. });
  127. public static final Function SGN = registerFunc(new Func1NullIsNull("Sgn") {
  128. @Override
  129. protected Value eval1(EvalContext ctx, Value param1) {
  130. int signum = 0;
  131. if(param1.getType().isIntegral()) {
  132. int lv = param1.getAsLongInt();
  133. signum = ((lv > 0) ? 1 : ((lv < 0) ? -1 : 0));
  134. } else {
  135. signum = param1.getAsBigDecimal().signum();
  136. }
  137. return BuiltinOperators.toValue(signum);
  138. }
  139. });
  140. public static final Function SQR = registerFunc(new Func1("Sqr") {
  141. @Override
  142. protected Value eval1(EvalContext ctx, Value param1) {
  143. double dv = param1.getAsDouble();
  144. if(dv < 0.0d) {
  145. throw new EvalException("Invalid value '" + dv + "'");
  146. }
  147. return BuiltinOperators.toValue(Math.sqrt(dv));
  148. }
  149. });
  150. public static final Function SIN = registerFunc(new Func1("Sin") {
  151. @Override
  152. protected Value eval1(EvalContext ctx, Value param1) {
  153. return BuiltinOperators.toValue(Math.sin(param1.getAsDouble()));
  154. }
  155. });
  156. public static final Function TAN = registerFunc(new Func1("Tan") {
  157. @Override
  158. protected Value eval1(EvalContext ctx, Value param1) {
  159. return BuiltinOperators.toValue(Math.tan(param1.getAsDouble()));
  160. }
  161. });
  162. // public static final Function Val = registerFunc(new Func1("Val") {
  163. // @Override
  164. // protected Value eval1(EvalContext ctx, Value param1) {
  165. // // FIXME, maybe leverage ExpressionTokenizer.maybeParseNumberLiteral (note, leading - or + is valid, exponent form is valid)
  166. // }
  167. // });
  168. }