Skip to content

Instantly share code, notes, and snippets.

@zencd
Last active March 23, 2023 14:22
Show Gist options
  • Save zencd/b2035b1249bcf6e376a9defb29ee0c8e to your computer and use it in GitHub Desktop.
Save zencd/b2035b1249bcf6e376a9defb29ee0c8e to your computer and use it in GitHub Desktop.
public static boolean match(@Nullable Pattern pattern, @Nullable String input) {
if (pattern == null || input == null) {
return false;
}
return pattern.matcher(input).matches();
}
@Nullable
public static String toStr(@Nullable Object value) {
return (value != null) ? value.toString() : null;
}
public static <T> Stream<T> safeStream(@Nullable T[] array) {
if (array == null || array.length == 0) {
return Stream.empty();
} else {
return Arrays.stream(array);
}
}
public static int toInt(Integer value, int fallback) {
return value != null ? value : fallback;
}
@Nullable
public static <T> T first(@Nullable Collection<T> collection) {
if (collection == null) {
return null;
}
for (T elem : collection) {
return elem;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment