From 9b83f6eab4170d926d4bdf04375efb04f10eb015 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 12 Dec 2021 12:17:45 +0000 Subject: [PATCH] Added ListUtils#toSingletonList --- .../java/com/willfp/eco/util/ListUtils.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/eco-api/src/main/java/com/willfp/eco/util/ListUtils.java b/eco-api/src/main/java/com/willfp/eco/util/ListUtils.java index 9027a1e8..92230e81 100644 --- a/eco-api/src/main/java/com/willfp/eco/util/ListUtils.java +++ b/eco-api/src/main/java/com/willfp/eco/util/ListUtils.java @@ -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 The type of the object. + * @return The list. + */ + @NotNull + public static List 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"); }