Skip to content

Commit 2dd27e5

Browse files
committed
Add validation for number of extensions
1 parent f494ca4 commit 2dd27e5

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/main/java/io/github/malczuuu/problem4j/core/Problem.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ public Problem(
7676
this(type, title, status, detail, instance, buildMapFromExtensions(extensions));
7777
}
7878

79+
/**
80+
* @throws IllegalArgumentException if extensions are not key-value pairs (odd number of elements)
81+
*/
7982
public Problem(
80-
URI type, String title, int status, String detail, URI instance, Object... extensions) {
83+
URI type, String title, int status, String detail, URI instance, Object... extensions)
84+
throws IllegalArgumentException {
8185
this(type, title, status, detail, instance, buildMapFromRawArgs(extensions));
8286
}
8387

@@ -95,7 +99,12 @@ private static Map<String, Object> buildMapFromExtensions(Extension[] extensions
9599
return map;
96100
}
97101

98-
private static Map<String, Object> buildMapFromRawArgs(Object[] arguments) {
102+
private static Map<String, Object> buildMapFromRawArgs(Object[] arguments)
103+
throws IllegalArgumentException {
104+
if (arguments.length % 2 != 0) {
105+
throw new IllegalArgumentException("extensions arguments must be key-value pairs");
106+
}
107+
99108
Map<String, Object> map = new HashMap<>(arguments.length / 2);
100109

101110
List<Object> valuesAsList = new ArrayList<>(Arrays.asList(arguments));

0 commit comments

Comments
 (0)