File tree Expand file tree Collapse file tree 3 files changed +15
-2
lines changed
main/java/org/apache/commons/math4/legacy/util
test/java/org/apache/commons/math4/legacy/util Expand file tree Collapse file tree 3 files changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -350,7 +350,7 @@ public NumberFormat getRealFormat() {
350350 public Complex parse (String source ) throws MathParseException {
351351 ParsePosition parsePosition = new ParsePosition (0 );
352352 Complex result = parse (source , parsePosition );
353- if (parsePosition . getIndex () == 0 ) {
353+ if (result == null ) {
354354 throw new MathParseException (source ,
355355 parsePosition .getErrorIndex (),
356356 Complex .class );
@@ -363,7 +363,8 @@ public Complex parse(String source) throws MathParseException {
363363 *
364364 * @param source the string to parse
365365 * @param pos input/output parsing parameter.
366- * @return the parsed {@link Complex} object.
366+ * @return the parsed {@link Complex} object, or {@code null} if
367+ * parsing failed.
367368 */
368369 public Complex parse (String source , ParsePosition pos ) {
369370 int initialIndex = pos .getIndex ();
Original file line number Diff line number Diff line change 2929import org .apache .commons .math4 .legacy .exception .MathIllegalArgumentException ;
3030import org .apache .commons .math4 .legacy .exception .NoDataException ;
3131import org .apache .commons .math4 .legacy .exception .NullArgumentException ;
32+ import org .apache .commons .math4 .legacy .exception .MathParseException ;
3233import org .apache .commons .math4 .core .jdkmath .JdkMath ;
3334
3435public abstract class ComplexFormatAbstractTest {
@@ -406,4 +407,12 @@ public void testForgottenImaginaryCharacter() {
406407 Assert .assertNull (new ComplexFormat ().parse ("1 + 1" , pos ));
407408 Assert .assertEquals (5 , pos .getErrorIndex ());
408409 }
410+
411+ // MATH-1681.
412+ @ Test (expected =MathParseException .class )
413+ public void testParseMissingImaginaryCharacter () {
414+ ComplexFormat format = new ComplexFormat ();
415+ String invalidComplex = "3 + 5" ;
416+ format .parse (invalidComplex );
417+ }
409418}
Original file line number Diff line number Diff line change @@ -96,6 +96,9 @@ Caveat:
9696 to support the whole codebase (it was one of the main reasons for
9797 creating more focused components).
9898" >
99+ <action dev =" erans" type =" fix" issue =" MATH-1681" due-to =" Ruiqi Dong" >
100+ "ComplexFormat": Throw exception when parsing fails.
101+ </action >
99102 <action dev =" erans" type =" fix" issue =" MATH-1683" due-to =" Ruiqi Dong" >
100103 "ElkanKMeansPlusPlusClusterer": More explicit error message.
101104 </action >
You can’t perform that action at this time.
0 commit comments