FunctionUtil.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.sagacloud.util.math;
  2. import java.lang.reflect.Method;
  3. public class FunctionUtil {
  4. public static ValueObject constant(String constant) {
  5. ValueObject result = new ValueObject();
  6. result.type = 1;
  7. if (constant.equals("PI")) {
  8. result.doubleValue = Math.PI;
  9. } else if (constant.equals("E")) {
  10. result.doubleValue = Math.E;
  11. }
  12. return result;
  13. }
  14. public static ValueObject compute(String function) {
  15. ValueObject result = new ValueObject();
  16. result.type = 1;
  17. Class<?> mathClass = Math.class;
  18. try {
  19. Method method = mathClass.getMethod(function, new Class[] {});
  20. result.doubleValue = (Double) method.invoke(mathClass, new Object[] {});
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. return result;
  25. }
  26. public static ValueObject compute(String function, ValueObject a) {
  27. if (a.is_null()) {
  28. return new ValueObject(null);
  29. }
  30. ValueObject result = new ValueObject();
  31. result.type = 1;
  32. double valuea;
  33. if (a.type == 0) {
  34. valuea = a.intValue;
  35. } else {
  36. valuea = a.doubleValue;
  37. }
  38. Class<?> mathClass = Math.class;
  39. try {
  40. Method method = mathClass.getMethod(function, new Class[] { double.class });
  41. result.doubleValue = (Double) method.invoke(mathClass, new Object[] { valuea });
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. return result;
  46. }
  47. public static ValueObject compute(String function, ValueObject a, ValueObject b) {
  48. if (a.is_null() || b.is_null()) {
  49. return new ValueObject(null);
  50. }
  51. ValueObject result = new ValueObject();
  52. result.type = 1;
  53. double valuea;
  54. if (a.type == 0) {
  55. valuea = a.intValue;
  56. } else {
  57. valuea = a.doubleValue;
  58. }
  59. double valueb;
  60. if (b.type == 0) {
  61. valueb = b.intValue;
  62. } else {
  63. valueb = b.doubleValue;
  64. }
  65. Class<?> mathClass = Math.class;
  66. try {
  67. Method method = mathClass.getMethod(function, new Class[] { double.class, double.class });
  68. result.doubleValue = (Double) method.invoke(mathClass, new Object[] { valuea, valueb });
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. return result;
  73. }
  74. }