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.

ValueSupport.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.math.BigInteger;
  16. import java.time.LocalDate;
  17. import java.time.LocalDateTime;
  18. import java.time.LocalTime;
  19. import java.time.format.DateTimeFormatter;
  20. import java.util.regex.Pattern;
  21. import com.healthmarketscience.jackcess.expr.EvalException;
  22. import com.healthmarketscience.jackcess.expr.LocaleContext;
  23. import com.healthmarketscience.jackcess.expr.Value;
  24. import com.healthmarketscience.jackcess.impl.ColumnImpl;
  25. /**
  26. *
  27. * @author James Ahlborn
  28. */
  29. public class ValueSupport
  30. {
  31. public static final Value NULL_VAL = new BaseValue() {
  32. @Override public boolean isNull() {
  33. return true;
  34. }
  35. @Override
  36. public Type getType() {
  37. return Type.NULL;
  38. }
  39. @Override
  40. public Object get() {
  41. return null;
  42. }
  43. };
  44. // access seems to like -1 for true and 0 for false (boolean values are
  45. // basically an illusion)
  46. public static final Value TRUE_VAL = new LongValue(-1);
  47. public static final Value FALSE_VAL = new LongValue(0);
  48. public static final Value EMPTY_STR_VAL = new StringValue("");
  49. public static final Value ZERO_VAL = FALSE_VAL;
  50. public static final Value NEG_ONE_VAL = TRUE_VAL;
  51. public static final Value ONE_VAL = new LongValue(1);
  52. public static final Value ZERO_D_VAL = new DoubleValue(0d);
  53. static final char NUMBER_BASE_PREFIX = '&';
  54. static final Pattern OCTAL_PAT =
  55. Pattern.compile("^" + NUMBER_BASE_PREFIX + "[oO][0-7]+");
  56. static final Pattern HEX_PAT =
  57. Pattern.compile("^" + NUMBER_BASE_PREFIX + "[hH]\\p{XDigit}+");
  58. static final char CANON_DEC_SEP = '.';
  59. static final Pattern NUMBER_PAT =
  60. Pattern.compile("^[+-]?(([0-9]+[.]?[0-9]*)|([.][0-9]+))([eE][+-]?[0-9]+)?");
  61. static final Pattern WHITESPACE_PAT = Pattern.compile("[ \\t\\r\\n]+");
  62. private ValueSupport() {}
  63. public static Value toValue(boolean b) {
  64. return (b ? TRUE_VAL : FALSE_VAL);
  65. }
  66. public static Value toValue(String s) {
  67. return new StringValue(s);
  68. }
  69. public static Value toValue(int i) {
  70. return new LongValue(i);
  71. }
  72. public static Value toValue(Integer i) {
  73. return new LongValue(i);
  74. }
  75. public static Value toValue(float f) {
  76. return new DoubleValue((double)f);
  77. }
  78. public static Value toValue(double s) {
  79. return new DoubleValue(s);
  80. }
  81. public static Value toValue(Double s) {
  82. return new DoubleValue(s);
  83. }
  84. public static Value toValue(BigDecimal s) {
  85. return new BigDecimalValue(normalize(s));
  86. }
  87. static Value toDateValueIfPossible(Value.Type dateType, double dd) {
  88. if(DefaultDateFunctions.isValidDateDouble(dd)) {
  89. return ValueSupport.toValue(
  90. dateType, ColumnImpl.ldtFromLocalDateDouble(dd));
  91. }
  92. return ValueSupport.toValue(dd);
  93. }
  94. public static Value toValue(LocalDate ld) {
  95. return new DateTimeValue(
  96. Value.Type.DATE, LocalDateTime.of(ld, ColumnImpl.BASE_LT));
  97. }
  98. public static Value toValue(LocalTime lt) {
  99. return new DateTimeValue(
  100. Value.Type.TIME, LocalDateTime.of(ColumnImpl.BASE_LD, lt));
  101. }
  102. public static Value toValue(LocalDateTime ldt) {
  103. return new DateTimeValue(getDateTimeType(ldt), ldt);
  104. }
  105. public static Value.Type getDateTimeType(LocalDateTime ldt) {
  106. boolean hasDate = !ColumnImpl.BASE_LD.equals(ldt.toLocalDate());
  107. boolean hasTime = !ColumnImpl.BASE_LT.equals(ldt.toLocalTime());
  108. return (hasDate ?
  109. (hasTime ? Value.Type.DATE_TIME : Value.Type.DATE) :
  110. Value.Type.TIME);
  111. }
  112. public static Value toValue(Value.Type type, LocalDateTime ldt) {
  113. return new DateTimeValue(type, ldt);
  114. }
  115. public static DateTimeFormatter getDateFormatForType(LocaleContext ctx, Value.Type type) {
  116. String fmtStr = null;
  117. switch(type) {
  118. case DATE:
  119. fmtStr = ctx.getTemporalConfig().getDefaultDateFormat();
  120. break;
  121. case TIME:
  122. fmtStr = ctx.getTemporalConfig().getDefaultTimeFormat();
  123. break;
  124. case DATE_TIME:
  125. fmtStr = ctx.getTemporalConfig().getDefaultDateTimeFormat();
  126. break;
  127. default:
  128. throw new EvalException("Unexpected date/time type " + type);
  129. }
  130. return ctx.createDateFormatter(fmtStr);
  131. }
  132. /**
  133. * Converts the given BigDecimal to the minimal scale >= 0;
  134. */
  135. static BigDecimal normalize(BigDecimal bd) {
  136. if(bd.scale() == 0) {
  137. return bd;
  138. }
  139. // handle a bug in the jdk which doesn't strip zero values
  140. if(bd.compareTo(BigDecimal.ZERO) == 0) {
  141. return BigDecimal.ZERO;
  142. }
  143. bd = bd.stripTrailingZeros();
  144. if(bd.scale() < 0) {
  145. bd = bd.setScale(0);
  146. }
  147. return bd;
  148. }
  149. static BigInteger parseIntegerString(String val, int radix) {
  150. return new BigInteger(val.substring(2), radix);
  151. }
  152. }