Skip to content

Commit f7b98e6

Browse files
committed
fix: add null input validation to AlternativeStringArrange.arrange()
The arrange() method previously threw a NullPointerException when either input string was null. This change explicitly validates the inputs and throws IllegalArgumentException with a clear message, matching the fail-fast pattern used by other utility classes in this package (e.g. HammingDistance). - Add null guard at the start of arrange() - Update Javadoc with @throws and contract notes - Add parameterized test covering all three null-input combinations
1 parent 8848ed1 commit f7b98e6

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/main/java/com/thealgorithms/strings/AlternativeStringArrange.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ private AlternativeStringArrange() {
2121

2222
/**
2323
* Arranges two strings by alternating their characters.
24+
* If one string is longer than the other, the remaining characters of the longer string
25+
* are appended at the end of the result.
2426
*
25-
* @param firstString the first input string
26-
* @param secondString the second input string
27+
* @param firstString the first input string, must not be {@code null}
28+
* @param secondString the second input string, must not be {@code null}
2729
* @return a new string with characters from both strings arranged alternately
30+
* @throws IllegalArgumentException if {@code firstString} or {@code secondString} is {@code null}
2831
*/
2932
public static String arrange(String firstString, String secondString) {
33+
if (firstString == null || secondString == null) {
34+
throw new IllegalArgumentException("Input strings must not be null");
35+
}
36+
3037
StringBuilder result = new StringBuilder();
3138
int length1 = firstString.length();
3239
int length2 = secondString.length();

src/test/java/com/thealgorithms/strings/AlternativeStringArrangeTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.thealgorithms.strings;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import java.util.stream.Stream;
7+
import org.junit.jupiter.api.Test;
68
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
710
import org.junit.jupiter.params.provider.MethodSource;
811

912
class AlternativeStringArrangeTest {
@@ -20,4 +23,15 @@ private static Stream<Object[]> provideTestData() {
2023
void arrangeTest(String input1, String input2, String expected) {
2124
assertEquals(expected, AlternativeStringArrange.arrange(input1, input2));
2225
}
26+
27+
@ParameterizedTest(name = "null input ({0}, {1}) should throw IllegalArgumentException")
28+
@MethodSource("provideNullInputs")
29+
void arrangeThrowsOnNullInput(String input1, String input2) {
30+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> AlternativeStringArrange.arrange(input1, input2));
31+
assertEquals("Input strings must not be null", ex.getMessage());
32+
}
33+
34+
private static Stream<Arguments> provideNullInputs() {
35+
return Stream.of(Arguments.of(null, "abc"), Arguments.of("abc", null), Arguments.of(null, null));
36+
}
2337
}

0 commit comments

Comments
 (0)