@@ -1713,12 +1713,7 @@ private Format formatFromPattern(String type, String style) {
17131713 throw new IllegalArgumentException ("unknown format type: " + type );
17141714 }
17151715 // Get the style if recognized, otherwise treat style as a SubformatPattern
1716- FormatStyle fStyle ;
1717- try {
1718- fStyle = FormatStyle .fromString (style );
1719- } catch (IllegalArgumentException iae ) {
1720- fStyle = FormatStyle .SUBFORMATPATTERN ;
1721- }
1716+ FormatStyle fStyle = FormatStyle .fromString (style );
17221717 return switch (fType ) {
17231718 case NUMBER -> switch (fStyle ) {
17241719 case DEFAULT -> NumberFormat .getInstance (locale );
@@ -1976,41 +1971,43 @@ private enum FormatType {
19761971 }
19771972
19781973 // Corresponding to the FormatStyle pattern
1974+ // WARNING: fromString is dependent on ordinal positioning and Enum names.
19791975 private enum FormatStyle {
1980- DEFAULT ("" ),
1981- SHORT ("short" ),
1982- MEDIUM ("medium" ),
1983- LONG ("long" ),
1984- FULL ("full" ),
1985- INTEGER ("integer" ),
1986- CURRENCY ("currency" ),
1987- PERCENT ("percent" ),
1988- COMPACT_SHORT ("compact_short" ),
1989- COMPACT_LONG ("compact_long" ),
1990- OR ("or" ),
1991- UNIT ("unit" ),
1992- SUBFORMATPATTERN (null );
1993-
1994- private final String text ;
1995-
1996- // Differs from FormatType in that the text String is
1997- // not guaranteed to match the Enum name, thus a text field is used
1998- FormatStyle (String text ) {
1999- this .text = text ;
2000- }
2001-
2002- // This method returns a FormatStyle (excluding SUBFORMATPATTERN)
2003- // that matches the passed String. If no FormatStyle is found,
2004- // an IllegalArgumentException is thrown
1976+ // Special styles
1977+ DEFAULT ,
1978+ SUBFORMATPATTERN ,
1979+ // Pre-defined styles
1980+ SHORT ,
1981+ MEDIUM ,
1982+ LONG ,
1983+ FULL ,
1984+ INTEGER ,
1985+ CURRENCY ,
1986+ PERCENT ,
1987+ COMPACT_SHORT ,
1988+ COMPACT_LONG ,
1989+ OR ,
1990+ UNIT ;
1991+
1992+ // Returns a FormatStyle corresponding to the input text.
1993+ // DEFAULT is the empty String.
1994+ // Pre-defined styles are lower case versions of their enum name
1995+ // (but compared case-insensitive for historical compatibility).
1996+ // SUBFORMATPATTERN is anything else.
20051997 private static FormatStyle fromString (String text ) {
2006- for (FormatStyle style : values ()) {
2007- // Also check trimmed case-insensitive for historical reasons
2008- if (style != FormatStyle .SUBFORMATPATTERN &&
2009- text .trim ().compareToIgnoreCase (style .text ) == 0 ) {
2010- return style ;
1998+ var style = text .trim ();
1999+ if (style .isEmpty ()) {
2000+ return FormatStyle .DEFAULT ;
2001+ }
2002+ var styles = values ();
2003+ // Match starting at the pre-defined styles -> [SHORT:]
2004+ for (int i = SHORT .ordinal (); i < styles .length ; i ++) {
2005+ var fStyle = styles [i ];
2006+ if (style .compareToIgnoreCase (fStyle .name ()) == 0 ) {
2007+ return fStyle ;
20112008 }
20122009 }
2013- throw new IllegalArgumentException () ;
2010+ return FormatStyle . SUBFORMATPATTERN ;
20142011 }
20152012 }
20162013
0 commit comments