Added ListUtils#toSingletonList

This commit is contained in:
Auxilor
2021-12-12 12:17:45 +00:00
parent 8190660b8f
commit 9b83f6eab4

View File

@@ -1,8 +1,10 @@
package com.willfp.eco.util;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -55,6 +57,22 @@ public final class ListUtils {
return frequencyMap;
}
/**
* Convert nullable object to either singleton list or empty list.
*
* @param object The object.
* @param <T> The type of the object.
* @return The list.
*/
@NotNull
public static <T> List<T> toSingletonList(@Nullable final T object) {
if (object == null) {
return Collections.emptyList();
} else {
return Collections.singletonList(object);
}
}
private ListUtils() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}