Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ValueSupport.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright (c) 2018 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 java.text.DateFormat;
  16. import java.util.Date;
  17. import com.healthmarketscience.jackcess.expr.LocaleContext;
  18. import com.healthmarketscience.jackcess.expr.EvalException;
  19. import com.healthmarketscience.jackcess.expr.Value;
  20. import com.healthmarketscience.jackcess.impl.ColumnImpl;
  21. /**
  22. *
  23. * @author James Ahlborn
  24. */
  25. public class ValueSupport
  26. {
  27. public static final Value NULL_VAL = new BaseValue() {
  28. @Override public boolean isNull() {
  29. return true;
  30. }
  31. public Type getType() {
  32. return Type.NULL;
  33. }
  34. public Object get() {
  35. return null;
  36. }
  37. };
  38. // access seems to like -1 for true and 0 for false (boolean values are
  39. // basically an illusion)
  40. public static final Value TRUE_VAL = new LongValue(-1);
  41. public static final Value FALSE_VAL = new LongValue(0);
  42. public static final Value EMPTY_STR_VAL = new StringValue("");
  43. public static final Value ZERO_VAL = FALSE_VAL;
  44. public static final Value NEG_ONE_VAL = TRUE_VAL;
  45. public static final Value ONE_VAL = new LongValue(1);
  46. private ValueSupport() {}
  47. public static Value toValue(boolean b) {
  48. return (b ? TRUE_VAL : FALSE_VAL);
  49. }
  50. public static Value toValue(String s) {
  51. return new StringValue(s);
  52. }
  53. public static Value toValue(int i) {
  54. return new LongValue(i);
  55. }
  56. public static Value toValue(Integer i) {
  57. return new LongValue(i);
  58. }
  59. public static Value toValue(float f) {
  60. return new DoubleValue((double)f);
  61. }
  62. public static Value toValue(double s) {
  63. return new DoubleValue(s);
  64. }
  65. public static Value toValue(Double s) {
  66. return new DoubleValue(s);
  67. }
  68. public static Value toValue(BigDecimal s) {
  69. return new BigDecimalValue(normalize(s));
  70. }
  71. public static Value toValue(Value.Type type, double dd, DateFormat fmt) {
  72. return toValue(type, new Date(ColumnImpl.fromDateDouble(
  73. dd, fmt.getCalendar())), fmt);
  74. }
  75. public static Value toValue(LocaleContext ctx, Value.Type type, Date d) {
  76. return toValue(type, d, getDateFormatForType(ctx, type));
  77. }
  78. public static Value toValue(Value.Type type, Date d, DateFormat fmt) {
  79. switch(type) {
  80. case DATE:
  81. return new DateValue(d, fmt);
  82. case TIME:
  83. return new TimeValue(d, fmt);
  84. case DATE_TIME:
  85. return new DateTimeValue(d, fmt);
  86. default:
  87. throw new EvalException("Unexpected date/time type " + type);
  88. }
  89. }
  90. static Value toDateValue(LocaleContext ctx, Value.Type type, double v,
  91. Value param1, Value param2)
  92. {
  93. DateFormat fmt = null;
  94. if((param1 instanceof BaseDateValue) && (param1.getType() == type)) {
  95. fmt = ((BaseDateValue)param1).getFormat();
  96. } else if((param2 instanceof BaseDateValue) && (param2.getType() == type)) {
  97. fmt = ((BaseDateValue)param2).getFormat();
  98. } else {
  99. fmt = getDateFormatForType(ctx, type);
  100. }
  101. Date d = new Date(ColumnImpl.fromDateDouble(v, fmt.getCalendar()));
  102. return toValue(type, d, fmt);
  103. }
  104. static DateFormat getDateFormatForType(LocaleContext ctx, Value.Type type) {
  105. String fmtStr = null;
  106. switch(type) {
  107. case DATE:
  108. fmtStr = ctx.getTemporalConfig().getDefaultDateFormat();
  109. break;
  110. case TIME:
  111. fmtStr = ctx.getTemporalConfig().getDefaultTimeFormat();
  112. break;
  113. case DATE_TIME:
  114. fmtStr = ctx.getTemporalConfig().getDefaultDateTimeFormat();
  115. break;
  116. default:
  117. throw new EvalException("Unexpected date/time type " + type);
  118. }
  119. return ctx.createDateFormat(fmtStr);
  120. }
  121. /**
  122. * Converts the given BigDecimal to the minimal scale >= 0;
  123. */
  124. static BigDecimal normalize(BigDecimal bd) {
  125. if(bd.scale() == 0) {
  126. return bd;
  127. }
  128. // handle a bug in the jdk which doesn't strip zero values
  129. if(bd.compareTo(BigDecimal.ZERO) == 0) {
  130. return BigDecimal.ZERO;
  131. }
  132. bd = bd.stripTrailingZeros();
  133. if(bd.scale() < 0) {
  134. bd = bd.setScale(0);
  135. }
  136. return bd;
  137. }
  138. }