Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*/
public class ComplexFormat {

/** The default imaginary character. */
/** The default imaginary character. */
private static final String DEFAULT_IMAGINARY_CHARACTER = "i";
/** The notation used to signify the imaginary part of the complex number. */
private final String imaginaryCharacter;
Expand All @@ -52,7 +52,7 @@ public class ComplexFormat {
*/
public ComplexFormat() {
this.imaginaryCharacter = DEFAULT_IMAGINARY_CHARACTER;
this.imaginaryFormat = CompositeFormat.getDefaultNumberFormat();
this.imaginaryFormat = getDefaultNumberFormat();
this.realFormat = imaginaryFormat;
}

Expand Down Expand Up @@ -93,6 +93,27 @@ public ComplexFormat(NumberFormat realFormat, NumberFormat imaginaryFormat)
this.realFormat = realFormat;
}

/**
* Creates the default number format used by complex values.
*
* @return a number format that does not accept grouping separators.
*/
private static NumberFormat getDefaultNumberFormat() {
return getDefaultNumberFormat(Locale.getDefault());
}

/**
* Creates the default number format used by complex values.
*
* @param locale the locale used by the format.
* @return a number format that does not accept grouping separators.
*/
private static NumberFormat getDefaultNumberFormat(final Locale locale) {
final NumberFormat format = CompositeFormat.getDefaultNumberFormat(locale);
format.setGroupingUsed(false);
return format;
}

/**
* Create an instance with a custom imaginary character, and the default
* number format for both real and imaginary parts.
Expand All @@ -104,7 +125,7 @@ public ComplexFormat(NumberFormat realFormat, NumberFormat imaginaryFormat)
*/
public ComplexFormat(String imaginaryCharacter)
throws NullArgumentException, NoDataException {
this(imaginaryCharacter, CompositeFormat.getDefaultNumberFormat());
this(imaginaryCharacter, getDefaultNumberFormat());
}

/**
Expand Down Expand Up @@ -311,7 +332,7 @@ public static ComplexFormat getInstance() {
* @return the complex format specific to the given locale.
*/
public static ComplexFormat getInstance(Locale locale) {
NumberFormat f = CompositeFormat.getDefaultNumberFormat(locale);
NumberFormat f = getDefaultNumberFormat(locale);
return new ComplexFormat(f);
}

Expand All @@ -327,7 +348,7 @@ public static ComplexFormat getInstance(Locale locale) {
*/
public static ComplexFormat getInstance(String imaginaryCharacter, Locale locale)
throws NullArgumentException, NoDataException {
NumberFormat f = CompositeFormat.getDefaultNumberFormat(locale);
NumberFormat f = getDefaultNumberFormat(locale);
return new ComplexFormat(imaginaryCharacter, f);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,19 @@ public void testParseMissingImaginaryCharacter() {
String invalidComplex = "3 + 5";
format.parse(invalidComplex);
}

// MATH-1688.
@Test
public void testParseRejectsInvalidGroupingSeparators() {
final ComplexFormat format = new ComplexFormat();
final String[] invalid = {",,7+,,,2i", ",8+,,3i", ",7", "7,,8"};
for (final String source : invalid) {
try {
format.parse(source);
Assert.fail("Expected parsing to reject: " + source);
} catch (MathParseException expected) {
// Expected.
}
}
}
}