jdk/src/java.base/share/classes/java/text/ChoiceFormat.java
changeset 43011 36edf207e84c
parent 37521 b6e0f285c998
child 44844 b2b4d98404ba
equal deleted inserted replaced
43010:f3c984a6f1d9 43011:36edf207e84c
     1 /*
     1 /*
     2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   170     private static final long serialVersionUID = 1795184449645032964L;
   170     private static final long serialVersionUID = 1795184449645032964L;
   171 
   171 
   172     /**
   172     /**
   173      * Sets the pattern.
   173      * Sets the pattern.
   174      * @param newPattern See the class description.
   174      * @param newPattern See the class description.
       
   175      * @exception NullPointerException if {@code newPattern}
       
   176      *            is {@code null}
   175      */
   177      */
   176     public void applyPattern(String newPattern) {
   178     public void applyPattern(String newPattern) {
   177         StringBuffer[] segments = new StringBuffer[2];
   179         StringBuffer[] segments = new StringBuffer[2];
   178         for (int i = 0; i < segments.length; ++i) {
   180         for (int i = 0; i < segments.length; ++i) {
   179             segments[i] = new StringBuffer();
   181             segments[i] = new StringBuffer();
   307 
   309 
   308     /**
   310     /**
   309      * Constructs with limits and corresponding formats based on the pattern.
   311      * Constructs with limits and corresponding formats based on the pattern.
   310      *
   312      *
   311      * @param newPattern the new pattern string
   313      * @param newPattern the new pattern string
       
   314      * @exception NullPointerExcpetion if {@code newPattern} is
       
   315      *            {@code null}
   312      * @see #applyPattern
   316      * @see #applyPattern
   313      */
   317      */
   314     public ChoiceFormat(String newPattern)  {
   318     public ChoiceFormat(String newPattern)  {
   315         applyPattern(newPattern);
   319         applyPattern(newPattern);
   316     }
   320     }
   318     /**
   322     /**
   319      * Constructs with the limits and the corresponding formats.
   323      * Constructs with the limits and the corresponding formats.
   320      *
   324      *
   321      * @param limits limits in ascending order
   325      * @param limits limits in ascending order
   322      * @param formats corresponding format strings
   326      * @param formats corresponding format strings
       
   327      * @exception NullPointerException if {@code limits} or {@code formats}
       
   328      *            is {@code null}
   323      * @see #setChoices
   329      * @see #setChoices
   324      */
   330      */
   325     public ChoiceFormat(double[] limits, String[] formats) {
   331     public ChoiceFormat(double[] limits, String[] formats) {
   326         setChoices(limits, formats);
   332         setChoices(limits, formats);
   327     }
   333     }
   337      * @param formats are the formats you want to use for each limit.
   343      * @param formats are the formats you want to use for each limit.
   338      * They can be either Format objects or Strings.
   344      * They can be either Format objects or Strings.
   339      * When formatting with object Y,
   345      * When formatting with object Y,
   340      * if the object is a NumberFormat, then ((NumberFormat) Y).format(X)
   346      * if the object is a NumberFormat, then ((NumberFormat) Y).format(X)
   341      * is called. Otherwise Y.toString() is called.
   347      * is called. Otherwise Y.toString() is called.
       
   348      * @exception NullPointerException if {@code limits} or
       
   349      *            {@code formats} is {@code null}
   342      */
   350      */
   343     public void setChoices(double[] limits, String formats[]) {
   351     public void setChoices(double[] limits, String formats[]) {
   344         if (limits.length != formats.length) {
   352         if (limits.length != formats.length) {
   345             throw new IllegalArgumentException(
   353             throw new IllegalArgumentException(
   346                 "Array and limit arrays must be of the same length.");
   354                 "Array and limit arrays must be of the same length.");
   384     /**
   392     /**
   385      * Returns pattern with formatted double.
   393      * Returns pattern with formatted double.
   386      * @param number number to be formatted and substituted.
   394      * @param number number to be formatted and substituted.
   387      * @param toAppendTo where text is appended.
   395      * @param toAppendTo where text is appended.
   388      * @param status ignore no useful status is returned.
   396      * @param status ignore no useful status is returned.
       
   397      * @exception NullPointerException if {@code toAppendTo}
       
   398      *            is {@code null}
   389      */
   399      */
   390    public StringBuffer format(double number, StringBuffer toAppendTo,
   400    public StringBuffer format(double number, StringBuffer toAppendTo,
   391                                FieldPosition status) {
   401                                FieldPosition status) {
   392         // find the number
   402         // find the number
   393         int i;
   403         int i;
   412      * occurred, status.index is set to the first unparsed character
   422      * occurred, status.index is set to the first unparsed character
   413      * in the source text.  On exit, if an error did occur,
   423      * in the source text.  On exit, if an error did occur,
   414      * status.index is unchanged and status.errorIndex is set to the
   424      * status.index is unchanged and status.errorIndex is set to the
   415      * first index of the character that caused the parse to fail.
   425      * first index of the character that caused the parse to fail.
   416      * @return A Number representing the value of the number parsed.
   426      * @return A Number representing the value of the number parsed.
       
   427      * @exception NullPointerException if {@code status} is {@code null}
       
   428      *            or if {@code text} is {@code null} and the list of
       
   429      *            choice strings is not empty.
   417      */
   430      */
   418     public Number parse(String text, ParsePosition status) {
   431     public Number parse(String text, ParsePosition status) {
   419         // find the best number (defined as the one with the longest parse)
   432         // find the best number (defined as the one with the longest parse)
   420         int start = status.index;
   433         int start = status.index;
   421         int furthest = start;
   434         int furthest = start;