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.

DBEvalContext.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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;
  14. import java.text.DecimalFormat;
  15. import java.time.ZoneId;
  16. import java.time.format.DateTimeFormatter;
  17. import java.util.Map;
  18. import javax.script.Bindings;
  19. import javax.script.SimpleBindings;
  20. import com.healthmarketscience.jackcess.expr.EvalConfig;
  21. import com.healthmarketscience.jackcess.expr.FunctionLookup;
  22. import com.healthmarketscience.jackcess.expr.NumericConfig;
  23. import com.healthmarketscience.jackcess.expr.TemporalConfig;
  24. import com.healthmarketscience.jackcess.impl.expr.DefaultFunctions;
  25. import com.healthmarketscience.jackcess.impl.expr.Expressionator;
  26. import com.healthmarketscience.jackcess.impl.expr.NumberFormatter;
  27. import com.healthmarketscience.jackcess.impl.expr.RandomContext;
  28. /**
  29. *
  30. * @author James Ahlborn
  31. */
  32. public class DBEvalContext implements Expressionator.ParseContext, EvalConfig
  33. {
  34. private static final int MAX_CACHE_SIZE = 10;
  35. private final DatabaseImpl _db;
  36. private FunctionLookup _funcs = DefaultFunctions.LOOKUP;
  37. private Map<String,DateTimeFormatter> _sdfs;
  38. private Map<String,DecimalFormat> _dfs;
  39. private TemporalConfig _temporal = TemporalConfig.US_TEMPORAL_CONFIG;
  40. private NumericConfig _numeric = NumericConfig.US_NUMERIC_CONFIG;
  41. private final RandomContext _rndCtx = new RandomContext();
  42. private Bindings _bindings = new SimpleBindings();
  43. public DBEvalContext(DatabaseImpl db) {
  44. _db = db;
  45. }
  46. protected DatabaseImpl getDatabase() {
  47. return _db;
  48. }
  49. @Override
  50. public TemporalConfig getTemporalConfig() {
  51. return _temporal;
  52. }
  53. @Override
  54. public void setTemporalConfig(TemporalConfig temporal) {
  55. if(_temporal != temporal) {
  56. _temporal = temporal;
  57. _sdfs = null;
  58. }
  59. }
  60. @Override
  61. public ZoneId getZoneId() {
  62. return _db.getZoneId();
  63. }
  64. @Override
  65. public NumericConfig getNumericConfig() {
  66. return _numeric;
  67. }
  68. @Override
  69. public void setNumericConfig(NumericConfig numeric) {
  70. if(_numeric != numeric) {
  71. _numeric = numeric;
  72. _dfs = null;
  73. }
  74. }
  75. @Override
  76. public FunctionLookup getFunctionLookup() {
  77. return _funcs;
  78. }
  79. @Override
  80. public void setFunctionLookup(FunctionLookup lookup) {
  81. _funcs = lookup;
  82. }
  83. @Override
  84. public Bindings getBindings() {
  85. return _bindings;
  86. }
  87. @Override
  88. public void setBindings(Bindings bindings) {
  89. _bindings = bindings;
  90. }
  91. @Override
  92. public DateTimeFormatter createDateFormatter(String formatStr) {
  93. if(_sdfs == null) {
  94. _sdfs = new SimpleCache<String,DateTimeFormatter>(MAX_CACHE_SIZE);
  95. }
  96. DateTimeFormatter sdf = _sdfs.get(formatStr);
  97. if(sdf == null) {
  98. sdf = DateTimeFormatter.ofPattern(formatStr, _temporal.getLocale());
  99. _sdfs.put(formatStr, sdf);
  100. }
  101. return sdf;
  102. }
  103. @Override
  104. public DecimalFormat createDecimalFormat(String formatStr) {
  105. if(_dfs == null) {
  106. _dfs = new SimpleCache<String,DecimalFormat>(MAX_CACHE_SIZE);
  107. }
  108. DecimalFormat df = _dfs.get(formatStr);
  109. if(df == null) {
  110. df = new DecimalFormat(formatStr, _numeric.getDecimalFormatSymbols());
  111. df.setRoundingMode(NumberFormatter.ROUND_MODE);
  112. _dfs.put(formatStr, df);
  113. }
  114. return df;
  115. }
  116. public float getRandom(Integer seed) {
  117. return _rndCtx.getRandom(seed);
  118. }
  119. }