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.

NumericConfig.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.expr;
  14. import java.text.DecimalFormatSymbols;
  15. import java.util.Locale;
  16. import com.healthmarketscience.jackcess.impl.expr.FormatUtil;
  17. /**
  18. * A NumericConfig encapsulates number formatting options for expression
  19. * evaluation. The default {@link #US_NUMERIC_CONFIG} instance provides US
  20. * specific locale configuration. Databases which have been built for other
  21. * locales can utilize custom implementations of NumericConfig in order to
  22. * evaluate expressions correctly.
  23. *
  24. * @author James Ahlborn
  25. */
  26. public class NumericConfig
  27. {
  28. public static final NumericConfig US_NUMERIC_CONFIG = new NumericConfig(
  29. 2, true, false, true, 3, Locale.US);
  30. public enum Type {
  31. CURRENCY, FIXED, STANDARD, PERCENT, SCIENTIFIC, EURO;
  32. }
  33. private final int _numDecDigits;
  34. private final boolean _incLeadingDigit;
  35. private final boolean _useNegParens;
  36. private final boolean _useNegCurrencyParens;
  37. private final int _numGroupDigits;
  38. private final DecimalFormatSymbols _symbols;
  39. private final String _currencyFormat;
  40. private final String _fixedFormat;
  41. private final String _standardFormat;
  42. private final String _percentFormat;
  43. private final String _scientificFormat;
  44. private final String _euroFormat;
  45. public NumericConfig(int numDecDigits, boolean incLeadingDigit,
  46. boolean useNegParens, boolean useNegCurrencyParens,
  47. int numGroupDigits, Locale locale) {
  48. _numDecDigits = numDecDigits;
  49. _incLeadingDigit = incLeadingDigit;
  50. _useNegParens = useNegParens;
  51. _useNegCurrencyParens = useNegCurrencyParens;
  52. _numGroupDigits = numGroupDigits;
  53. _symbols = DecimalFormatSymbols.getInstance(locale);
  54. _currencyFormat = FormatUtil.createNumberFormatPattern(
  55. FormatUtil.NumPatternType.CURRENCY, _numDecDigits, _incLeadingDigit,
  56. _useNegCurrencyParens, _numGroupDigits);
  57. _fixedFormat = FormatUtil.createNumberFormatPattern(
  58. FormatUtil.NumPatternType.GENERAL, _numDecDigits, true,
  59. _useNegParens, 0);
  60. _standardFormat = FormatUtil.createNumberFormatPattern(
  61. FormatUtil.NumPatternType.GENERAL, _numDecDigits, _incLeadingDigit,
  62. _useNegParens, _numGroupDigits);
  63. _percentFormat = FormatUtil.createNumberFormatPattern(
  64. FormatUtil.NumPatternType.PERCENT, _numDecDigits, _incLeadingDigit,
  65. _useNegParens, 0);
  66. _scientificFormat = FormatUtil.createNumberFormatPattern(
  67. FormatUtil.NumPatternType.SCIENTIFIC, _numDecDigits, true,
  68. false, 0);
  69. _euroFormat = FormatUtil.createNumberFormatPattern(
  70. FormatUtil.NumPatternType.EURO, _numDecDigits, _incLeadingDigit,
  71. _useNegCurrencyParens, _numGroupDigits);
  72. }
  73. public int getNumDecimalDigits() {
  74. return _numDecDigits;
  75. }
  76. public boolean includeLeadingDigit() {
  77. return _incLeadingDigit;
  78. }
  79. public boolean useParensForNegatives() {
  80. return _useNegParens;
  81. }
  82. public boolean useParensForCurrencyNegatives() {
  83. return _useNegCurrencyParens;
  84. }
  85. public int getNumGroupingDigits() {
  86. return _numGroupDigits;
  87. }
  88. public String getNumberFormat(Type type) {
  89. switch(type) {
  90. case CURRENCY:
  91. return _currencyFormat;
  92. case FIXED:
  93. return _fixedFormat;
  94. case STANDARD:
  95. return _standardFormat;
  96. case PERCENT:
  97. return _percentFormat;
  98. case SCIENTIFIC:
  99. return _scientificFormat;
  100. case EURO:
  101. return _euroFormat;
  102. default:
  103. throw new IllegalArgumentException("unknown number type " + type);
  104. }
  105. }
  106. public DecimalFormatSymbols getDecimalFormatSymbols() {
  107. return _symbols;
  108. }
  109. }