Added ListUtils#toSingletonList
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user