< prev index next >

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

Print this page
rev 16194 : imported patch XXXXXXX-Use-StringBuilder-appendN-method


2692                             buf.append(decimalStyle.getPositiveSign());
2693                         }
2694                         break;
2695                     case ALWAYS:
2696                         buf.append(decimalStyle.getPositiveSign());
2697                         break;
2698                 }
2699             } else {
2700                 switch (signStyle) {
2701                     case NORMAL:
2702                     case EXCEEDS_PAD:
2703                     case ALWAYS:
2704                         buf.append(decimalStyle.getNegativeSign());
2705                         break;
2706                     case NOT_NEGATIVE:
2707                         throw new DateTimeException("Field " + field +
2708                             " cannot be printed as the value " + value +
2709                             " cannot be negative according to the SignStyle");
2710                 }
2711             }
2712             for (int i = 0; i < minWidth - str.length(); i++) {
2713                 buf.append(decimalStyle.getZeroDigit());
2714             }
2715             buf.append(str);
2716             return true;
2717         }
2718 
2719         /**
2720          * Gets the value to output.
2721          *
2722          * @param context  the context
2723          * @param value  the value of the field, not null
2724          * @return the value
2725          */
2726         long getValue(DateTimePrintContext context, long value) {
2727             return value;
2728         }
2729 
2730         /**
2731          * For NumberPrinterParser, the width is fixed depending on the
2732          * minWidth, maxWidth, signStyle and whether subsequent fields are fixed.
2733          * @param context the context


3109         boolean isFixedWidth(DateTimeParseContext context) {
3110             if (context.isStrict() && minWidth == maxWidth && decimalPoint == false) {
3111                 return true;
3112             }
3113             return false;
3114         }
3115 
3116         @Override
3117         public boolean format(DateTimePrintContext context, StringBuilder buf) {
3118             Long value = context.getValue(field);
3119             if (value == null) {
3120                 return false;
3121             }
3122             DecimalStyle decimalStyle = context.getDecimalStyle();
3123             BigDecimal fraction = convertToFraction(value);
3124             if (fraction.scale() == 0) {  // scale is zero if value is zero
3125                 if (minWidth > 0) {
3126                     if (decimalPoint) {
3127                         buf.append(decimalStyle.getDecimalSeparator());
3128                     }
3129                     for (int i = 0; i < minWidth; i++) {
3130                         buf.append(decimalStyle.getZeroDigit());
3131                     }
3132                 }
3133             } else {
3134                 int outputScale = Math.min(Math.max(fraction.scale(), minWidth), maxWidth);
3135                 fraction = fraction.setScale(outputScale, RoundingMode.FLOOR);
3136                 String str = fraction.toPlainString().substring(2);
3137                 str = decimalStyle.convertNumberToI18N(str);
3138                 if (decimalPoint) {
3139                     buf.append(decimalStyle.getDecimalSeparator());
3140                 }
3141                 buf.append(str);
3142             }
3143             return true;
3144         }
3145 
3146         @Override
3147         public int parse(DateTimeParseContext context, CharSequence text, int position) {
3148             int effectiveMin = (context.isStrict() || isFixedWidth(context) ? minWidth : 0);
3149             int effectiveMax = (context.isStrict() || isFixedWidth(context) ? maxWidth : 9);
3150             int length = text.length();
3151             if (position == length) {




2692                             buf.append(decimalStyle.getPositiveSign());
2693                         }
2694                         break;
2695                     case ALWAYS:
2696                         buf.append(decimalStyle.getPositiveSign());
2697                         break;
2698                 }
2699             } else {
2700                 switch (signStyle) {
2701                     case NORMAL:
2702                     case EXCEEDS_PAD:
2703                     case ALWAYS:
2704                         buf.append(decimalStyle.getNegativeSign());
2705                         break;
2706                     case NOT_NEGATIVE:
2707                         throw new DateTimeException("Field " + field +
2708                             " cannot be printed as the value " + value +
2709                             " cannot be negative according to the SignStyle");
2710                 }
2711             }
2712             if (minWidth > str.length()) {
2713                 buf.appendN(decimalStyle.getZeroDigit(), minWidth - str.length());
2714             }
2715             buf.append(str);
2716             return true;
2717         }
2718 
2719         /**
2720          * Gets the value to output.
2721          *
2722          * @param context  the context
2723          * @param value  the value of the field, not null
2724          * @return the value
2725          */
2726         long getValue(DateTimePrintContext context, long value) {
2727             return value;
2728         }
2729 
2730         /**
2731          * For NumberPrinterParser, the width is fixed depending on the
2732          * minWidth, maxWidth, signStyle and whether subsequent fields are fixed.
2733          * @param context the context


3109         boolean isFixedWidth(DateTimeParseContext context) {
3110             if (context.isStrict() && minWidth == maxWidth && decimalPoint == false) {
3111                 return true;
3112             }
3113             return false;
3114         }
3115 
3116         @Override
3117         public boolean format(DateTimePrintContext context, StringBuilder buf) {
3118             Long value = context.getValue(field);
3119             if (value == null) {
3120                 return false;
3121             }
3122             DecimalStyle decimalStyle = context.getDecimalStyle();
3123             BigDecimal fraction = convertToFraction(value);
3124             if (fraction.scale() == 0) {  // scale is zero if value is zero
3125                 if (minWidth > 0) {
3126                     if (decimalPoint) {
3127                         buf.append(decimalStyle.getDecimalSeparator());
3128                     }
3129                     buf.appendN(decimalStyle.getZeroDigit(), minWidth);


3130                 }
3131             } else {
3132                 int outputScale = Math.min(Math.max(fraction.scale(), minWidth), maxWidth);
3133                 fraction = fraction.setScale(outputScale, RoundingMode.FLOOR);
3134                 String str = fraction.toPlainString().substring(2);
3135                 str = decimalStyle.convertNumberToI18N(str);
3136                 if (decimalPoint) {
3137                     buf.append(decimalStyle.getDecimalSeparator());
3138                 }
3139                 buf.append(str);
3140             }
3141             return true;
3142         }
3143 
3144         @Override
3145         public int parse(DateTimeParseContext context, CharSequence text, int position) {
3146             int effectiveMin = (context.isStrict() || isFixedWidth(context) ? minWidth : 0);
3147             int effectiveMax = (context.isStrict() || isFixedWidth(context) ? maxWidth : 9);
3148             int length = text.length();
3149             if (position == length) {


< prev index next >