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

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