/** * <p>Creates a textual output of given duration. </p> * * @param duration duration object * @return textual representation of duration * @throws IllegalArgumentException if some aspects of duration * prevents printing (for example too many nanoseconds) */ /*[deutsch] * <p>Erzeugt eine textuelle Ausgabe der angegebenen Dauer. </p> * * @param duration duration object * @return textual representation of duration * @throws IllegalArgumentException if some aspects of duration * prevents printing (for example too many nanoseconds) */ public String format(TimeSpan<? super U> duration) { StringBuilder buffer = new StringBuilder(); try { this.print(duration, buffer); } catch (IOException ex) { throw new AssertionError(ex); // should never happen } return buffer.toString(); }
private void addLiteral( String literal, List<List<FormatItem<U>>> stack ) { lastOn(stack).add(new LiteralItem<>(literal)); }
/** * <p>Equivalent to {@code parse(text, 0)}. </p> * * @param text custom textual representation to be parsed * @return parsed duration * @throws ParseException (for example in case of mixed signs or if trailing unparsed characters exist) * @see #parse(CharSequence, int) */ /*[deutsch] * <p>Äquivalent zu {@code parse(text, 0)}. </p> * * @param text custom textual representation to be parsed * @return parsed duration * @throws ParseException (for example in case of mixed signs or if trailing unparsed characters exist) * @see #parse(CharSequence, int) */ public S parse(CharSequence text) throws ParseException { return this.parse(text, 0); }
} else if (isSymbol(c)) { int start = i++; while ((i < n) && pattern.charAt(i) == c) { i++; this.addSymbol(c, i - start, digits, stack); digits = 0; i--; this.addLiteral('\'', stack); } else { String s = pattern.substring(start + 1, i); this.addLiteral(s.replace("''", "'"), stack); startOptionalSection(stack); } else if (c == ']') { endOptionalSection(stack); } else if (c == '.') { lastOn(stack).add(new SeparatorItem<>('.', ',')); } else if (c == ',') { lastOn(stack).add(new SeparatorItem<>(',', '.')); } else if (c == '-') { lastOn(stack).add(new SignItem<>(false)); } else if (c == '+') { lastOn(stack).add(new SignItem<>(true)); } else if (c == '{') { int start = ++i; this.addPluralItem(pattern.substring(start, i), stack);
private void addSymbol( char symbol, int count, int digits, List<List<FormatItem<U>>> stack ) { U unit = this.getUnit(symbol); List<FormatItem<U>> items = stack.get(stack.size() - 1); if (symbol == 'f') { if (digits > 0) { throw new IllegalArgumentException("Combination of # and f-symbol not allowed."); } else { items.add(new FractionItem<>(0, count, this.getUnit(symbol))); } } else { items.add(new NumberItem<>(0, count, count + digits, unit)); } }
return this.convert(map, negative);
super.print(new Normalized(duration, patternUnits), buffer);