diff --git a/src/main/java/uk/ac/ebi/utils/collections/CollectionsUtils.java b/src/main/java/uk/ac/ebi/utils/collections/CollectionsUtils.java index c4ce8e964..5e86131cd 100644 --- a/src/main/java/uk/ac/ebi/utils/collections/CollectionsUtils.java +++ b/src/main/java/uk/ac/ebi/utils/collections/CollectionsUtils.java @@ -8,7 +8,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.function.Function; import java.util.function.Supplier; @@ -26,7 +25,7 @@ public class CollectionsUtils { /** - * Utility for as$Collection( value ) methods. + * Utility for asCollection( value ) methods. * * @return a collection containing the value parameter. If value is null, * an empty collection, if it's already of type C/collClass, return the @@ -182,11 +181,15 @@ public static T asValue ( Object value ) return asValue ( value, false ); } - + /** - * TODO: newXXX() and unmodifiableXXX(), comment and test + * Returns a new collection containing the elements in the parameter, using + * the provider. + * + * If the param is null or empty, returns a new empty collection. + * + * This is the base for newXXX() methods. */ - public static > C newCollection ( Collection coll, Supplier provider ) { C result = provider.get (); @@ -194,16 +197,42 @@ public static > C newCollection ( Collection Set newSet ( Collection coll, Supplier> provider ) + { + return newCollection ( coll, provider ); + } + + /** + * Defaults to {@link HashSet} as provider. + */ public static Set newSet ( Collection coll ) { - return newCollection ( coll, HashSet::new ); + return newSet ( coll, HashSet::new ); } + + /** + * @see #newCollection(Collection, Supplier) + */ + public static List newList ( Collection coll, Supplier> provider ) + { + return newCollection ( coll, provider ); + } + + /** + * Defaults to {@link ArrayList} as provider + */ public static List newList ( Collection coll ) { - return newCollection ( coll, ArrayList::new ); + return newList ( coll, ArrayList::new ); } + /** + * @see #newCollection(Collection, Supplier) + */ public static Map newMap ( Map map, Supplier> provider ) { Map result = provider.get (); @@ -211,12 +240,20 @@ public static Map newMap ( Map map, Supplie return result; } + /** + * Defaults to {@link HashMap} as provider + */ public static Map newMap ( Map map ) { return newMap ( map, HashMap::new ); } - + /** + * If coll is not null, returns, it, else returns a new empty collection, + * using the provider. + * + * This is the base for newXXXIfNull() methods. + */ public static > C newCollectionIfNull ( C coll, Supplier provider ) { @@ -224,28 +261,63 @@ public static Map newMap ( Map map ) return provider.get (); } + /** + * @see #newCollectionIfNull(Collection, Supplier) + */ + public static Set newSetIfNull ( Set set, Supplier> provider ) + { + return newCollectionIfNull ( set, provider ); + } + + /** + * Defaults to {@link HashSet} as provider. + */ public static Set newSetIfNull ( Set set ) { - return newCollectionIfNull ( set, HashSet::new ); + return newSetIfNull ( set, HashSet::new ); } + /** + * @see #newCollectionIfNull(Collection, Supplier) + */ + public static List newListIfNull ( List list, Supplier> provider ) + { + return newCollectionIfNull ( list, provider ); + } + + /** + * Defaults to {@link ArrayList} as provider. + */ public static List newListIfNull ( List list ) { - return newCollectionIfNull ( list, ArrayList::new ); + return newListIfNull ( list, ArrayList::new ); } + /** + * @see #newCollectionIfNull(Collection, Supplier) + */ public static > M newMapIfNull ( M map, Supplier provider ) { if ( map != null && !map.isEmpty () ) return map; return provider.get (); } + /** + * Defaults to {@link HashMap} as provider. + */ public static Map newMapIfNull ( Map map ) { return newMapIfNull ( map, HashMap::new ); } - + /** + * Always returns an unmodifiable collection wrapping the parameter. + * If the input collection is null or empty, it provides an unmodifiable + * collection via {@code emptyProvider}, else, it uses the {@code wrapper} + * to make the parameter read-only. + * + * This is the base for unmodifiableXXX() methods. + */ public static , CP extends Collection> C unmodifiableCollection ( CP coll, @@ -257,16 +329,27 @@ C unmodifiableCollection ( return wrapper.apply ( coll ); } + /** + * {@link #unmodifiableCollection(Collection, Function, Supplier)} + */ public static Set unmodifiableSet ( Set set ) { return unmodifiableCollection ( set, Collections::unmodifiableSet, Set::of ); } + /** + * {@link #unmodifiableCollection(Collection, Function, Supplier)} + */ public static List unmodifiableList ( List list ) { return unmodifiableCollection ( list, Collections::unmodifiableList, List::of ); } + /** + * This doesn't use + * {@link #unmodifiableCollection(Collection, Function, Supplier)}, but + * has an implementation based on the same logic. + */ public static Map unmodifiableMap ( Map map, Supplier> emptyProvider