Skip to content

Commit

Permalink
introduce hudson.Util#fixNull(value, defaultValue)
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Sep 28, 2018
1 parent 9801bd9 commit 179d597
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions core/src/main/java/hudson/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -1195,8 +1195,14 @@ public static void copyFile(@Nonnull File src, @Nonnull File dst) throws BuildEx
*/
@Nonnull
public static String fixNull(@CheckForNull String s) {
if(s==null) return "";
else return s;
return fixNull(s, "");
}

/**
* Convert null to defaultValue.
*/
public static <T> T fixNull(@CheckForNull T s, T defaultValue) {
return s != null ? s : defaultValue;
}

/**
Expand All @@ -1221,22 +1227,22 @@ public static String fixEmptyAndTrim(@CheckForNull String s) {

@Nonnull
public static <T> List<T> fixNull(@CheckForNull List<T> l) {
return l!=null ? l : Collections.<T>emptyList();
return fixNull(l, Collections.<T>emptyList());
}

@Nonnull
public static <T> Set<T> fixNull(@CheckForNull Set<T> l) {
return l!=null ? l : Collections.<T>emptySet();
return fixNull(l, Collections.<T>emptySet());
}

@Nonnull
public static <T> Collection<T> fixNull(@CheckForNull Collection<T> l) {
return l!=null ? l : Collections.<T>emptySet();
return fixNull(l, Collections.<T>emptySet());
}

@Nonnull
public static <T> Iterable<T> fixNull(@CheckForNull Iterable<T> l) {
return l!=null ? l : Collections.<T>emptySet();
return fixNull(l, Collections.<T>emptySet());
}

/**
Expand Down

0 comments on commit 179d597

Please sign in to comment.