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.

StringValue.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright (c) 2016 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.util.regex.Pattern;
  17. /**
  18. *
  19. * @author James Ahlborn
  20. */
  21. public class StringValue extends BaseValue
  22. {
  23. private static final Object NOT_A_NUMBER = new Object();
  24. private static final char NUMBER_BASE_PREFIX = '&';
  25. private static final Pattern OCTAL_PAT =
  26. Pattern.compile(NUMBER_BASE_PREFIX + "[oO][0-7]+");
  27. private static final Pattern HEX_PAT =
  28. Pattern.compile(NUMBER_BASE_PREFIX + "[hH]\\p{XDigit}+");
  29. private final String _val;
  30. private Object _num;
  31. public StringValue(String val)
  32. {
  33. _val = val;
  34. }
  35. public Type getType() {
  36. return Type.STRING;
  37. }
  38. public Object get() {
  39. return _val;
  40. }
  41. @Override
  42. public boolean getAsBoolean() {
  43. // ms access seems to treat strings as "true"
  44. return true;
  45. }
  46. @Override
  47. public String getAsString() {
  48. return _val;
  49. }
  50. @Override
  51. public Integer getAsLongInt() {
  52. return roundToLongInt();
  53. }
  54. @Override
  55. public Double getAsDouble() {
  56. return getNumber().doubleValue();
  57. }
  58. @Override
  59. public BigDecimal getAsBigDecimal() {
  60. return getNumber();
  61. }
  62. protected BigDecimal getNumber() {
  63. if(_num instanceof BigDecimal) {
  64. return (BigDecimal)_num;
  65. }
  66. if(_num == null) {
  67. // see if it is parseable as a number
  68. try {
  69. // ignore extraneous whitespace whitespace and handle "&[hH]" or
  70. // "&[oO]" prefix (only supports integers)
  71. String tmpVal = _val.trim();
  72. if(tmpVal.length() > 0) {
  73. if(tmpVal.charAt(0) != NUMBER_BASE_PREFIX) {
  74. // parse using standard numeric support
  75. _num = ValueSupport.normalize(new BigDecimal(tmpVal));
  76. return (BigDecimal)_num;
  77. }
  78. // parse as hex/octal symbolic value
  79. if(HEX_PAT.matcher(tmpVal).matches()) {
  80. return parseIntegerString(tmpVal, 16);
  81. } else if(OCTAL_PAT.matcher(tmpVal).matches()) {
  82. return parseIntegerString(tmpVal, 8);
  83. }
  84. // fall through to NaN
  85. }
  86. } catch(NumberFormatException nfe) {
  87. // fall through to NaN...
  88. }
  89. _num = NOT_A_NUMBER;
  90. }
  91. throw new NumberFormatException("Invalid number '" + _val + "'");
  92. }
  93. private BigDecimal parseIntegerString(String tmpVal, int radix) {
  94. _num = new BigDecimal(new BigInteger(tmpVal.substring(2), radix));
  95. return (BigDecimal)_num;
  96. }
  97. }