[java-shib-shared] 02/02: Add zero-arg and one-arg listOf and setOf
Ian Young
ian at iay.org.uk
Tue Apr 4 06:14:34 UTC 2023
This is an automated email from the git hooks/post-receive script.
iay pushed a commit to branch main
in repository java-shib-shared.
View the commit online:
http://git.shibboleth.net/view/?p=java-shib-shared.git;a=commit;h=893324fec4839311bf172b805bce650fdfa601db
commit 893324fec4839311bf172b805bce650fdfa601db
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Tue Apr 4 07:08:31 2023 +0100
Add zero-arg and one-arg listOf and setOf
These might seem superfluous, as we also have "empty" and "singleton" variants. The use case is
direct conversion from the equivalent, more modern, List.of(), List.of(x), etc.; if we don't implement
ofList() etc. we run the risk of accidentally and silently interposing e.g. the worst possible empty
list constructor involving varargs array construction.
---
.../shared/collection/CollectionSupport.java | 50 ++++++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git a/shib-support/src/main/java/net/shibboleth/shared/collection/CollectionSupport.java b/shib-support/src/main/java/net/shibboleth/shared/collection/CollectionSupport.java
index 2091ae2e..f8634e33 100644
--- a/shib-support/src/main/java/net/shibboleth/shared/collection/CollectionSupport.java
+++ b/shib-support/src/main/java/net/shibboleth/shared/collection/CollectionSupport.java
@@ -121,6 +121,31 @@ public final class CollectionSupport {
return Collections.singletonList(item);
}
+ /**
+ * Gets a zero member list with non-null guarantee.
+ *
+ * @param <T> list type
+ *
+ * @return zero member list
+ */
+ @SuppressWarnings("null")
+ @Nonnull @Unmodifiable @NotLive public static <T> List<T> listOf() {
+ return List.of();
+ }
+
+ /**
+ * Gets a one member list with non-null guarantee.
+ *
+ * @param <T> list type
+ * @param second the second item
+ *
+ * @return two member list
+ */
+ @SuppressWarnings("null")
+ @Nonnull @Unmodifiable @NotLive public static <T> List<T> listOf(@Nonnull final T first) {
+ return List.of(first);
+ }
+
/**
* Gets a two member list with non-null guarantee.
*
@@ -189,6 +214,31 @@ public final class CollectionSupport {
return Collections.singleton(item);
}
+ /**
+ * Gets a zero member set with non-null guarantee.
+ *
+ * @param <T> list type
+ *
+ * @return zero member set
+ */
+ @SuppressWarnings("null")
+ @Nonnull @Unmodifiable @NotLive public static <T> Set<T> setOf() {
+ return Set.of();
+ }
+
+ /**
+ * Gets a one member set with non-null guarantee.
+ *
+ * @param <T> list type
+ * @param first the first item
+ *
+ * @return one member set
+ */
+ @SuppressWarnings("null")
+ @Nonnull @Unmodifiable @NotLive public static <T> Set<T> setOf(@Nonnull final T first) {
+ return Set.of(first);
+ }
+
/**
* Gets a two member set with non-null guarantee.
*
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list