[java-shib-shared] branch main updated: Add singleton and copy wrappers.

Scott Cantor cantor.2 at osu.edu
Fri Jan 20 19:23:01 UTC 2023


This is an automated email from the git hooks/post-receive script.

scantor 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=4d3fc27c5fd32b1653d70174f22af63d594d8fac

The following commit(s) were added to refs/heads/main by this push:
     new 4d3fc27c Add singleton and copy wrappers.
4d3fc27c is described below

commit 4d3fc27c5fd32b1653d70174f22af63d594d8fac
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Fri Jan 20 14:22:58 2023 -0500

    Add singleton and copy wrappers.
---
 .../shared/collection/CollectionSupport.java       | 99 +++++++++++++++++++++-
 1 file changed, 96 insertions(+), 3 deletions(-)

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 96033156..7be80019 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
@@ -17,6 +17,7 @@
 
 package net.shibboleth.shared.collection;
 
+import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
@@ -28,6 +29,9 @@ import javax.annotation.Nonnull;
 
 import org.slf4j.Logger;
 
+import net.shibboleth.shared.annotation.constraint.NonnullElements;
+import net.shibboleth.shared.annotation.constraint.NotLive;
+import net.shibboleth.shared.annotation.constraint.Unmodifiable;
 import net.shibboleth.shared.primitive.LoggerFactory;
 
 /**
@@ -71,10 +75,38 @@ public final class CollectionSupport {
      * @return empty list
      */
     @SuppressWarnings("null")
-    @Nonnull public static <T> List<T> emptyList() {
+    @Nonnull @Unmodifiable @NotLive public static <T> List<T> emptyList() {
         return Collections.emptyList();
     }
 
+    /**
+     * Gets a singleton list with non-null guarantee.
+     * 
+     * @param <T> list type
+     * @param item the single item
+     * 
+     * @return singleton list
+     */
+    @SuppressWarnings("null")
+    @Nonnull @Unmodifiable @NotLive public static <T> List<T> singletonList(@Nonnull final T item) {
+        return Collections.singletonList(item);
+    }
+    
+    /**
+     * Copies a collection to a list with non-null guarantee.
+     * 
+     * @param <T> list type
+     * 
+     * @param coll collection to copy
+     * 
+     * @return immutable copied list
+     */
+    @SuppressWarnings("null")
+    @Nonnull @NonnullElements @Unmodifiable @NotLive static <T> List<T> copyToList(
+            @Nonnull @NonnullElements final Collection<? extends T> coll) {
+        return List.copyOf(coll);
+    }
+
     /**
      * Gets an empty set with non-null guarantee.
      * 
@@ -83,10 +115,38 @@ public final class CollectionSupport {
      * @return empty set
      */
     @SuppressWarnings("null")
-    @Nonnull public static <T> Set<T> emptySet() {
+    @Nonnull @Unmodifiable @NotLive public static <T> Set<T> emptySet() {
         return Collections.emptySet();
     }
 
+    /**
+     * Gets a singleton set with non-null guarantee.
+     * 
+     * @param <T> set type
+     * @param item the single item
+     * 
+     * @return singleton set
+     */
+    @SuppressWarnings("null")
+    @Nonnull @Unmodifiable @NotLive public static <T> Set<T> singleton(@Nonnull final T item) {
+        return Collections.singleton(item);
+    }
+
+    /**
+     * Copies a collection to a set with non-null guarantee.
+     * 
+     * @param <T> set type
+     * 
+     * @param coll collection to copy
+     * 
+     * @return immutable copied set
+     */
+    @SuppressWarnings("null")
+    @Nonnull @NonnullElements @Unmodifiable @NotLive static <T> Set<T> copyToSet(
+            @Nonnull @NonnullElements final Collection<? extends T> coll) {
+        return Set.copyOf(coll);
+    }
+    
     /**
      * Gets an empty map with non-null guarantee.
      * 
@@ -96,8 +156,41 @@ public final class CollectionSupport {
      * @return empty amp
      */
     @SuppressWarnings("null")
-    @Nonnull public static <T,U> Map<T,U> emptyMap() {
+    @Nonnull @Unmodifiable @NotLive public static <T,U> Map<T,U> emptyMap() {
         return Collections.emptyMap();
     }
+    
+    /**
+     * Gets a singleton map with non-null guarantee.
+     * 
+     * @param <T> key type
+     * @param <U> value type
+     * 
+     * @param key map key
+     * @param value map value
+     * 
+     * @return singleton amp
+     */
+    @SuppressWarnings("null")
+    @Nonnull @NonnullElements @Unmodifiable @NotLive public static <T,U> Map<T,U> singletonMap(@Nonnull final T key,
+            @Nonnull final U value) {
+        return Collections.singletonMap(key, value);
+    }
 
+    /**
+     * Copies a map to a map with non-null guarantee.
+     * 
+     * @param <U> key type
+     * @param <T> value type
+     * 
+     * @param map map to copy
+     * 
+     * @return immutable copied map
+     */
+    @SuppressWarnings("null")
+    @Nonnull @NonnullElements @Unmodifiable @NotLive static <U,T> Map<U,T> copyToMap(
+            @Nonnull final Map<? extends U, ? extends T> map) {
+        return Map.copyOf(map);
+    }
+    
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list