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.

BaseValue.java 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.time.LocalDateTime;
  16. import com.healthmarketscience.jackcess.expr.EvalException;
  17. import com.healthmarketscience.jackcess.expr.LocaleContext;
  18. import com.healthmarketscience.jackcess.expr.Value;
  19. /**
  20. *
  21. * @author James Ahlborn
  22. */
  23. public abstract class BaseValue implements Value
  24. {
  25. @Override
  26. public boolean isNull() {
  27. return(getType() == Type.NULL);
  28. }
  29. @Override
  30. public boolean getAsBoolean(LocaleContext ctx) {
  31. throw invalidConversion(Type.LONG);
  32. }
  33. @Override
  34. public String getAsString(LocaleContext ctx) {
  35. throw invalidConversion(Type.STRING);
  36. }
  37. @Override
  38. public LocalDateTime getAsLocalDateTime(LocaleContext ctx) {
  39. return (LocalDateTime)getAsDateTimeValue(ctx).get();
  40. }
  41. @Override
  42. public Value getAsDateTimeValue(LocaleContext ctx) {
  43. throw invalidConversion(Type.DATE_TIME);
  44. }
  45. @Override
  46. public Integer getAsLongInt(LocaleContext ctx) {
  47. throw invalidConversion(Type.LONG);
  48. }
  49. @Override
  50. public Double getAsDouble(LocaleContext ctx) {
  51. throw invalidConversion(Type.DOUBLE);
  52. }
  53. @Override
  54. public BigDecimal getAsBigDecimal(LocaleContext ctx) {
  55. throw invalidConversion(Type.BIG_DEC);
  56. }
  57. protected EvalException invalidConversion(Type newType) {
  58. return new EvalException(
  59. this + " cannot be converted to " + newType);
  60. }
  61. protected Integer roundToLongInt(LocaleContext ctx) {
  62. return getAsBigDecimal(ctx).setScale(0, NumberFormatter.ROUND_MODE)
  63. .intValueExact();
  64. }
  65. @Override
  66. public String toString() {
  67. return "Value[" + getType() + "] '" + get() + "'";
  68. }
  69. }