< prev index next >

src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 664,675 **** * The printed fraction will have the minimum width necessary between * the minimum and maximum widths - trailing zeroes are omitted. * No rounding occurs due to the maximum width - digits are simply dropped. * <p> * When parsing in strict mode, the number of parsed digits must be between ! * the minimum and maximum width. When parsing in lenient mode, the minimum ! * width is considered to be zero and the maximum is nine. * <p> * If the value cannot be obtained then an exception will be thrown. * If the value is negative an exception will be thrown. * If the field does not have a fixed set of valid values then an * exception will be thrown. --- 664,678 ---- * The printed fraction will have the minimum width necessary between * the minimum and maximum widths - trailing zeroes are omitted. * No rounding occurs due to the maximum width - digits are simply dropped. * <p> * When parsing in strict mode, the number of parsed digits must be between ! * the minimum and maximum width. In strict mode, if the minimum and maximum widths ! * are equal and there is no decimal point then the parser will ! * participate in adjacent value parsing, see ! * {@code appendValue(java.time.temporal.TemporalField, int)}. When parsing in lenient mode, ! * the minimum width is considered to be zero and the maximum is nine. * <p> * If the value cannot be obtained then an exception will be thrown. * If the value is negative an exception will be thrown. * If the field does not have a fixed set of valid values then an * exception will be thrown.
*** 684,694 **** --- 687,702 ---- * @throws IllegalArgumentException if the field has a variable set of valid values or * either width is invalid */ public DateTimeFormatterBuilder appendFraction( TemporalField field, int minWidth, int maxWidth, boolean decimalPoint) { + if (minWidth == maxWidth && decimalPoint == false) { + // adjacent parsing + appendValue(new FractionPrinterParser(field, minWidth, maxWidth, decimalPoint)); + } else { appendInternal(new FractionPrinterParser(field, minWidth, maxWidth, decimalPoint)); + } return this; } //----------------------------------------------------------------------- /**
*** 2917,2930 **** //----------------------------------------------------------------------- /** * Prints and parses a numeric date-time field with optional padding. */ ! static final class FractionPrinterParser implements DateTimePrinterParser { ! private final TemporalField field; ! private final int minWidth; ! private final int maxWidth; private final boolean decimalPoint; /** * Constructor. * --- 2925,2935 ---- //----------------------------------------------------------------------- /** * Prints and parses a numeric date-time field with optional padding. */ ! static final class FractionPrinterParser extends NumberPrinterParser { private final boolean decimalPoint; /** * Constructor. *
*** 2932,2941 **** --- 2937,2947 ---- * @param minWidth the minimum width to output, from 0 to 9 * @param maxWidth the maximum width to output, from 0 to 9 * @param decimalPoint whether to output the localized decimal point symbol */ FractionPrinterParser(TemporalField field, int minWidth, int maxWidth, boolean decimalPoint) { + this(field, minWidth, maxWidth, decimalPoint, 0); Objects.requireNonNull(field, "field"); if (field.range().isFixed() == false) { throw new IllegalArgumentException("Field must have a fixed set of values: " + field); } if (minWidth < 0 || minWidth > 9) {
*** 2946,2961 **** } if (maxWidth < minWidth) { throw new IllegalArgumentException("Maximum width must exceed or equal the minimum width but " + maxWidth + " < " + minWidth); } ! this.field = field; ! this.minWidth = minWidth; ! this.maxWidth = maxWidth; this.decimalPoint = decimalPoint; } @Override public boolean format(DateTimePrintContext context, StringBuilder buf) { Long value = context.getValue(field); if (value == null) { return false; --- 2952,3015 ---- } if (maxWidth < minWidth) { throw new IllegalArgumentException("Maximum width must exceed or equal the minimum width but " + maxWidth + " < " + minWidth); } ! } ! ! /** ! * Constructor. ! * ! * @param field the field to output, not null ! * @param minWidth the minimum width to output, from 0 to 9 ! * @param maxWidth the maximum width to output, from 0 to 9 ! * @param decimalPoint whether to output the localized decimal point symbol ! * @param subsequentWidth the subsequentWidth for this instance ! */ ! FractionPrinterParser(TemporalField field, int minWidth, int maxWidth, boolean decimalPoint, int subsequentWidth) { ! super(field, minWidth, maxWidth, SignStyle.NOT_NEGATIVE, subsequentWidth); this.decimalPoint = decimalPoint; } + /** + * Returns a new instance with fixed width flag set. + * + * @return a new updated printer-parser, not null + */ + @Override + FractionPrinterParser withFixedWidth() { + if (subsequentWidth == -1) { + return this; + } + return new FractionPrinterParser(field, minWidth, maxWidth, decimalPoint, -1); + } + + /** + * Returns a new instance with an updated subsequent width. + * + * @param subsequentWidth the width of subsequent non-negative numbers, 0 or greater + * @return a new updated printer-parser, not null + */ + @Override + FractionPrinterParser withSubsequentWidth(int subsequentWidth) { + return new FractionPrinterParser(field, minWidth, maxWidth, decimalPoint, this.subsequentWidth + subsequentWidth); + } + + /** + * + * @param context the context + * @return if the field is fixed width + * @see DateTimeFormatterBuilder#appendValueFraction(java.time.temporal.TemporalField, int, int, boolean) + */ + @Override + boolean isFixedWidth(DateTimeParseContext context) { + if (context.isStrict() && minWidth == maxWidth && decimalPoint == false) { + return true; + } + return false; + } + @Override public boolean format(DateTimePrintContext context, StringBuilder buf) { Long value = context.getValue(field); if (value == null) { return false;
*** 2984,2995 **** return true; } @Override public int parse(DateTimeParseContext context, CharSequence text, int position) { ! int effectiveMin = (context.isStrict() ? minWidth : 0); ! int effectiveMax = (context.isStrict() ? maxWidth : 9); int length = text.length(); if (position == length) { // valid if whole field is optional, invalid if minimum width return (effectiveMin > 0 ? ~position : position); } --- 3038,3049 ---- return true; } @Override public int parse(DateTimeParseContext context, CharSequence text, int position) { ! int effectiveMin = (context.isStrict() || isFixedWidth(context) ? minWidth : 0); ! int effectiveMax = (context.isStrict() || isFixedWidth(context) ? maxWidth : 9); int length = text.length(); if (position == length) { // valid if whole field is optional, invalid if minimum width return (effectiveMin > 0 ? ~position : position); }
< prev index next >