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

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