From 96b417fd35bb6aae9e5e57af94cecb44aeeb9c22 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Tue, 14 Nov 2017 15:52:46 +0200 Subject: [PATCH] Added some null checks. --- .../java/eu/svjatoslav/commons/string/String2.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/svjatoslav/commons/string/String2.java b/src/main/java/eu/svjatoslav/commons/string/String2.java index 504c41d..4c1627f 100755 --- a/src/main/java/eu/svjatoslav/commons/string/String2.java +++ b/src/main/java/eu/svjatoslav/commons/string/String2.java @@ -16,13 +16,19 @@ public class String2 { private final List chars; - public String2(final String value) { + public String2(String value) { + if (value == null) + value = ""; + chars = new ArrayList<>(value.length()); for (final Character c : value.toCharArray()) chars.add(c); } public String2 addPrefix(final String prefix) { + if (prefix == null) + return this; + int i = 0; for (final char c : prefix.toCharArray()) chars.add(i++, c); @@ -31,6 +37,9 @@ public class String2 { } public String2 addSuffix(final String suffix) { + if (suffix == null) + return this; + for (final char c : suffix.toCharArray()) chars.add(c); @@ -56,6 +65,9 @@ public class String2 { } public String2 trimPrefixIfExists(String prefix){ + if (prefix == null) + return this; + if (hasPrefix(prefix)) trimPrefix(prefix.length()); -- 2.20.1