Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Fields interpret null values as empty strings #12024

Merged
merged 4 commits into from
Jul 12, 2024
Merged
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 @@ -191,7 +191,7 @@ public List<String> getValuesOrEmpty(String name)
public void put(String name, String value)
{
// Preserve the case for the field name
Field field = new Field(name, value);
Field field = new Field(name, StringUtil.nonNull(value));
fields.put(name, field);
}

Expand Down Expand Up @@ -222,9 +222,9 @@ public void add(String name, String value)
{
if (f == null)
// Preserve the case for the field name
return new Field(name, value);
return new Field(name, StringUtil.nonNull(value));
else
return new Field(f.getName(), f.getValues(), value);
return new Field(f.getName(), f.getValues(), StringUtil.nonNull(value));
});
}

Expand All @@ -246,9 +246,9 @@ public void add(String name, String... values)
fields.compute(name, (k, f) ->
{
if (f == null)
return new Field(name, List.of(values));
return new Field(name, StringUtil.toListNonNull(values));
else
return new Field(f.getName(), f.getValues(), List.of(values));
return new Field(f.getName(), f.getValues(), StringUtil.toListNonNull(values));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,23 @@ public static String nonNull(String s)
return s;
}

/**
* Convert an array of strings to a list of non-null strings.
*
* @param strings the array
* @return The list of non-null strings.
* @see #nonNull(String)
*/
public static List<String> toListNonNull(String... strings)
{
List<String> result = new ArrayList<>(strings.length);
for (String s : strings)
{
result.add(nonNull(s));
}
return result;
}

public static boolean equals(String s, char[] buf, int offset, int length)
{
if (s.length() != length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,18 @@ public void testIterator()

assertThat(set, containsInAnyOrder("x", "y", "z"));
}

@Test
public void testNullValues()
{
Fields fields = new Fields();
fields.add("x", (String)null);
fields.add("y", "1", null, "2");
fields.put("z", null);

assertThat(fields.getSize(), equalTo(3));
assertThat(fields.getValues("x"), contains(""));
assertThat(fields.getValues("y"), contains("1", "", "2"));
assertThat(fields.getValues("z"), contains(""));
}
}
Loading