Skip to content

Commit ee57e73

Browse files
author
Justin Lu
committed
8317612: ChoiceFormat and MessageFormat constructors call non-final public method
Reviewed-by: naoto, lancea
1 parent f262f06 commit ee57e73

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

src/java.base/share/classes/java/text/ChoiceFormat.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,17 @@ public class ChoiceFormat extends NumberFormat {
246246
* @see #ChoiceFormat(String)
247247
*/
248248
public void applyPattern(String newPattern) {
249+
applyPatternImpl(newPattern);
250+
}
251+
252+
/**
253+
* Implementation of applying a pattern to this ChoiceFormat.
254+
* This method processes a String pattern in accordance with the ChoiceFormat
255+
* pattern syntax and populates the internal {@code limits} and {@code formats}
256+
* array variables. See the {@linkplain ##patterns} section for
257+
* further understanding of certain special characters: "#", "<", "\u2264", "|".
258+
*/
259+
private void applyPatternImpl(String newPattern) {
249260
StringBuilder[] segments = new StringBuilder[2];
250261
for (int i = 0; i < segments.length; ++i) {
251262
segments[i] = new StringBuilder();
@@ -326,8 +337,8 @@ public void applyPattern(String newPattern) {
326337
}
327338

328339
/**
329-
* {@return a pattern {@code string} that represents the the limits and formats
330-
* of this ChoiceFormat object}
340+
* {@return a pattern {@code string} that represents the {@code limits} and
341+
* {@code formats} of this ChoiceFormat object}
331342
*
332343
* The {@code string} returned is not guaranteed to be the same input
333344
* {@code string} passed to either {@link #applyPattern(String)} or
@@ -396,7 +407,7 @@ public String toPattern() {
396407
* @see #applyPattern
397408
*/
398409
public ChoiceFormat(String newPattern) {
399-
applyPattern(newPattern);
410+
applyPatternImpl(newPattern);
400411
}
401412

402413
/**
@@ -411,11 +422,12 @@ public ChoiceFormat(String newPattern) {
411422
* @see #setChoices
412423
*/
413424
public ChoiceFormat(double[] limits, String[] formats) {
414-
setChoices(limits, formats);
425+
setChoicesImpl(limits, formats);
415426
}
416427

417428
/**
418429
* Set the choices to be used in formatting.
430+
*
419431
* @param limits contains the top value that you want
420432
* parsed with that format, and should be in ascending sorted order. When
421433
* formatting X, the choice will be the i, where
@@ -429,6 +441,14 @@ public ChoiceFormat(double[] limits, String[] formats) {
429441
* and {@code formats} are not equal
430442
*/
431443
public void setChoices(double[] limits, String[] formats) {
444+
setChoicesImpl(limits, formats);
445+
}
446+
447+
/**
448+
* Implementation of populating the {@code limits} and
449+
* {@code formats} of this ChoiceFormat. Defensive copies are made.
450+
*/
451+
private void setChoicesImpl(double[] limits, String[] formats) {
432452
if (limits.length != formats.length) {
433453
throw new IllegalArgumentException(
434454
"Input arrays must be of the same length.");
@@ -441,16 +461,14 @@ public void setChoices(double[] limits, String[] formats) {
441461
* {@return the limits of this ChoiceFormat}
442462
*/
443463
public double[] getLimits() {
444-
double[] newLimits = Arrays.copyOf(choiceLimits, choiceLimits.length);
445-
return newLimits;
464+
return Arrays.copyOf(choiceLimits, choiceLimits.length);
446465
}
447466

448467
/**
449468
* {@return the formats of this ChoiceFormat}
450469
*/
451470
public Object[] getFormats() {
452-
Object[] newFormats = Arrays.copyOf(choiceFormats, choiceFormats.length);
453-
return newFormats;
471+
return Arrays.copyOf(choiceFormats, choiceFormats.length);
454472
}
455473

456474
// Overrides

src/java.base/share/classes/java/text/MessageFormat.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public class MessageFormat extends Format {
371371
* The constructor first sets the locale, then parses the pattern and
372372
* creates a list of subformats for the format elements contained in it.
373373
* Patterns and their interpretation are specified in the
374-
* <a href="#patterns">class description</a>.
374+
* {@linkplain ##patterns class description}.
375375
*
376376
* @param pattern the pattern for this message format
377377
* @throws IllegalArgumentException if the pattern is invalid
@@ -380,7 +380,7 @@ public class MessageFormat extends Format {
380380
*/
381381
public MessageFormat(String pattern) {
382382
this.locale = Locale.getDefault(Locale.Category.FORMAT);
383-
applyPattern(pattern);
383+
applyPatternImpl(pattern);
384384
}
385385

386386
/**
@@ -389,7 +389,7 @@ public MessageFormat(String pattern) {
389389
* The constructor first sets the locale, then parses the pattern and
390390
* creates a list of subformats for the format elements contained in it.
391391
* Patterns and their interpretation are specified in the
392-
* <a href="#patterns">class description</a>.
392+
* {@linkplain ##patterns class description}.
393393
*
394394
* @implSpec The default implementation throws a
395395
* {@code NullPointerException} if {@code locale} is {@code null}
@@ -408,7 +408,7 @@ public MessageFormat(String pattern) {
408408
*/
409409
public MessageFormat(String pattern, Locale locale) {
410410
this.locale = locale;
411-
applyPattern(pattern);
411+
applyPatternImpl(pattern);
412412
}
413413

414414
/**
@@ -447,15 +447,29 @@ public Locale getLocale() {
447447
* The method parses the pattern and creates a list of subformats
448448
* for the format elements contained in it.
449449
* Patterns and their interpretation are specified in the
450-
* <a href="#patterns">class description</a>.
450+
* {@linkplain ##patterns class description}.
451451
*
452452
* @param pattern the pattern for this message format
453453
* @throws IllegalArgumentException if the pattern is invalid
454454
* @throws NullPointerException if {@code pattern} is
455455
* {@code null}
456456
*/
457-
@SuppressWarnings("fallthrough") // fallthrough in switch is expected, suppress it
458457
public void applyPattern(String pattern) {
458+
applyPatternImpl(pattern);
459+
}
460+
461+
/**
462+
* Implementation of applying a pattern to this MessageFormat.
463+
* This method processes a String pattern in accordance with the MessageFormat
464+
* pattern syntax and sets the internal {@code pattern} variable as well as
465+
* populating the {@code formats} array with the subformats defined in the
466+
* pattern. See the {@linkplain ##patterns} section for further understanding
467+
* of certain special characters: "{", "}", ",". See {@linkplain
468+
* ##makeFormat(int, int, StringBuilder[])} for the implementation of setting
469+
* a subformat.
470+
*/
471+
@SuppressWarnings("fallthrough") // fallthrough in switch is expected, suppress it
472+
private void applyPatternImpl(String pattern) {
459473
StringBuilder[] segments = new StringBuilder[4];
460474
// Allocate only segments[SEG_RAW] here. The rest are
461475
// allocated on demand.
@@ -509,6 +523,7 @@ public void applyPattern(String pattern) {
509523
case '}':
510524
if (braceStack == 0) {
511525
part = SEG_RAW;
526+
// Set the subformat
512527
makeFormat(i, formatNumber, segments);
513528
formatNumber++;
514529
// throw away other segments
@@ -1592,7 +1607,7 @@ private void makeFormat(int position, int offsetNumber,
15921607
formats[offsetNumber] = newFormat;
15931608
}
15941609

1595-
private static final int findKeyword(String s, String[] list) {
1610+
private static int findKeyword(String s, String[] list) {
15961611
for (int i = 0; i < list.length; ++i) {
15971612
if (s.equals(list[i]))
15981613
return i;
@@ -1609,8 +1624,8 @@ private static final int findKeyword(String s, String[] list) {
16091624
return -1;
16101625
}
16111626

1612-
private static final void copyAndFixQuotes(String source, int start, int end,
1613-
StringBuilder target) {
1627+
private static void copyAndFixQuotes(String source, int start, int end,
1628+
StringBuilder target) {
16141629
boolean quoted = false;
16151630

16161631
for (int i = start; i < end; ++i) {

0 commit comments

Comments
 (0)