Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CellFormatter.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.format;
  16. import java.util.Locale;
  17. import java.util.logging.Logger;
  18. import org.apache.poi.util.LocaleUtil;
  19. /**
  20. * This is the abstract supertype for the various cell formatters.
  21. */
  22. public abstract class CellFormatter {
  23. /** The logger to use in the formatting code. */
  24. private static final Logger LOG = Logger.getLogger(CellFormatter.class.getName());
  25. /** The original specified format. */
  26. protected final String format;
  27. protected final Locale locale;
  28. /**
  29. * Creates a new formatter object, storing the format in {@link #format}.
  30. *
  31. * @param format The format.
  32. */
  33. public CellFormatter(String format) {
  34. this(LocaleUtil.getUserLocale(), format);
  35. }
  36. /**
  37. * Creates a new formatter object, storing the format in {@link #format}.
  38. *
  39. * @param locale The locale.
  40. * @param format The format.
  41. */
  42. public CellFormatter(Locale locale, String format) {
  43. this.locale = locale;
  44. this.format = format;
  45. }
  46. /**
  47. * Format a value according the format string.
  48. * <p/>
  49. * NOTE: this method must be thread safe! In particular, if it uses a
  50. * Format instance that is not thread safe, i.e. DateFormat, this method
  51. * must be synchronized, either on the method, if the format is a final
  52. * property, or on the format instance itself.
  53. *
  54. * @param toAppendTo The buffer to append to.
  55. * @param value The value to format.
  56. */
  57. public abstract void formatValue(StringBuffer toAppendTo, Object value);
  58. /**
  59. * Format a value according to the type, in the most basic way.
  60. * <p/>
  61. * NOTE: this method must be thread safe! In particular, if it uses a
  62. * Format instance that is not thread safe, i.e. DateFormat, this method
  63. * must be synchronized, either on the method, if the format is a final
  64. * property, or on the format instance itself.
  65. *
  66. * @param toAppendTo The buffer to append to.
  67. * @param value The value to format.
  68. */
  69. public abstract void simpleValue(StringBuffer toAppendTo, Object value);
  70. /**
  71. * Formats the value, returning the resulting string.
  72. *
  73. * @param value The value to format.
  74. *
  75. * @return The value, formatted.
  76. */
  77. public String format(Object value) {
  78. StringBuffer sb = new StringBuffer();
  79. formatValue(sb, value);
  80. return sb.toString();
  81. }
  82. /**
  83. * Formats the value in the most basic way, returning the resulting string.
  84. *
  85. * @param value The value to format.
  86. *
  87. * @return The value, formatted.
  88. */
  89. public String simpleFormat(Object value) {
  90. StringBuffer sb = new StringBuffer();
  91. simpleValue(sb, value);
  92. return sb.toString();
  93. }
  94. /**
  95. * Returns the input string, surrounded by quotes.
  96. *
  97. * @param str The string to quote.
  98. *
  99. * @return The input string, surrounded by quotes.
  100. */
  101. static String quote(String str) {
  102. return '"' + str + '"';
  103. }
  104. }