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.

CellElapsedFormatter.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.ArrayList;
  17. import java.util.Formatter;
  18. import java.util.List;
  19. import java.util.ListIterator;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;
  22. /**
  23. * This class implements printing out an elapsed time format.
  24. *
  25. * @author Ken Arnold, Industrious Media LLC
  26. */
  27. public class CellElapsedFormatter extends CellFormatter {
  28. private final List<TimeSpec> specs;
  29. private TimeSpec topmost;
  30. private final String printfFmt;
  31. private static final Pattern PERCENTS = Pattern.compile("%");
  32. private static final double HOUR__FACTOR = 1.0 / 24.0;
  33. private static final double MIN__FACTOR = HOUR__FACTOR / 60.0;
  34. private static final double SEC__FACTOR = MIN__FACTOR / 60.0;
  35. private static class TimeSpec {
  36. final char type;
  37. final int pos;
  38. final int len;
  39. final double factor;
  40. double modBy;
  41. public TimeSpec(char type, int pos, int len, double factor) {
  42. this.type = type;
  43. this.pos = pos;
  44. this.len = len;
  45. this.factor = factor;
  46. modBy = 0;
  47. }
  48. public long valueFor(double elapsed) {
  49. double val;
  50. if (modBy == 0)
  51. val = elapsed / factor;
  52. else
  53. val = elapsed / factor % modBy;
  54. if (type == '0')
  55. return Math.round(val);
  56. else
  57. return (long) val;
  58. }
  59. }
  60. private class ElapsedPartHandler implements CellFormatPart.PartHandler {
  61. // This is the one class that's directly using printf, so it can't use
  62. // the default handling for quoted strings and special characters. The
  63. // only special character for this is '%', so we have to handle all the
  64. // quoting in this method ourselves.
  65. public String handlePart(Matcher m, String part, CellFormatType type,
  66. StringBuffer desc) {
  67. int pos = desc.length();
  68. char firstCh = part.charAt(0);
  69. switch (firstCh) {
  70. case '[':
  71. if (part.length() < 3)
  72. break;
  73. if (topmost != null)
  74. throw new IllegalArgumentException(
  75. "Duplicate '[' times in format");
  76. part = part.toLowerCase();
  77. int specLen = part.length() - 2;
  78. topmost = assignSpec(part.charAt(1), pos, specLen);
  79. return part.substring(1, 1 + specLen);
  80. case 'h':
  81. case 'm':
  82. case 's':
  83. case '0':
  84. part = part.toLowerCase();
  85. assignSpec(part.charAt(0), pos, part.length());
  86. return part;
  87. case '\n':
  88. return "%n";
  89. case '\"':
  90. part = part.substring(1, part.length() - 1);
  91. break;
  92. case '\\':
  93. part = part.substring(1);
  94. break;
  95. case '*':
  96. if (part.length() > 1)
  97. part = CellFormatPart.expandChar(part);
  98. break;
  99. // An escape we can let it handle because it can't have a '%'
  100. case '_':
  101. return null;
  102. }
  103. // Replace ever "%" with a "%%" so we can use printf
  104. return PERCENTS.matcher(part).replaceAll("%%");
  105. }
  106. }
  107. /**
  108. * Creates a elapsed time formatter.
  109. *
  110. * @param pattern The pattern to parse.
  111. */
  112. public CellElapsedFormatter(String pattern) {
  113. super(pattern);
  114. specs = new ArrayList<TimeSpec>();
  115. StringBuffer desc = CellFormatPart.parseFormat(pattern,
  116. CellFormatType.ELAPSED, new ElapsedPartHandler());
  117. ListIterator<TimeSpec> it = specs.listIterator(specs.size());
  118. while (it.hasPrevious()) {
  119. TimeSpec spec = it.previous();
  120. desc.replace(spec.pos, spec.pos + spec.len, "%0" + spec.len + "d");
  121. if (spec.type != topmost.type) {
  122. spec.modBy = modFor(spec.type, spec.len);
  123. }
  124. }
  125. printfFmt = desc.toString();
  126. }
  127. private TimeSpec assignSpec(char type, int pos, int len) {
  128. TimeSpec spec = new TimeSpec(type, pos, len, factorFor(type, len));
  129. specs.add(spec);
  130. return spec;
  131. }
  132. private static double factorFor(char type, int len) {
  133. switch (type) {
  134. case 'h':
  135. return HOUR__FACTOR;
  136. case 'm':
  137. return MIN__FACTOR;
  138. case 's':
  139. return SEC__FACTOR;
  140. case '0':
  141. return SEC__FACTOR / Math.pow(10, len);
  142. default:
  143. throw new IllegalArgumentException(
  144. "Uknown elapsed time spec: " + type);
  145. }
  146. }
  147. private static double modFor(char type, int len) {
  148. switch (type) {
  149. case 'h':
  150. return 24;
  151. case 'm':
  152. return 60;
  153. case 's':
  154. return 60;
  155. case '0':
  156. return Math.pow(10, len);
  157. default:
  158. throw new IllegalArgumentException(
  159. "Uknown elapsed time spec: " + type);
  160. }
  161. }
  162. /** {@inheritDoc} */
  163. public void formatValue(StringBuffer toAppendTo, Object value) {
  164. double elapsed = ((Number) value).doubleValue();
  165. if (elapsed < 0) {
  166. toAppendTo.append('-');
  167. elapsed = -elapsed;
  168. }
  169. Object[] parts = new Long[specs.size()];
  170. for (int i = 0; i < specs.size(); i++) {
  171. parts[i] = specs.get(i).valueFor(elapsed);
  172. }
  173. Formatter formatter = new Formatter(toAppendTo);
  174. formatter.format(printfFmt, parts);
  175. }
  176. /**
  177. * {@inheritDoc}
  178. * <p/>
  179. * For a date, this is <tt>"mm/d/y"</tt>.
  180. */
  181. public void simpleValue(StringBuffer toAppendTo, Object value) {
  182. formatValue(toAppendTo, value);
  183. }
  184. }