From 0a9099c8dd67e438388e71791df562a5c6ad9490 Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 10 May 2014 13:36:24 -0400 Subject: [PATCH] Add a helper for casting some common collection types into generic form --- .../functions/CollectionWrapperFactory.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 fml/src/main/java/cpw/mods/fml/common/functions/CollectionWrapperFactory.java diff --git a/fml/src/main/java/cpw/mods/fml/common/functions/CollectionWrapperFactory.java b/fml/src/main/java/cpw/mods/fml/common/functions/CollectionWrapperFactory.java new file mode 100644 index 0000000000..1211b7f306 --- /dev/null +++ b/fml/src/main/java/cpw/mods/fml/common/functions/CollectionWrapperFactory.java @@ -0,0 +1,32 @@ +package cpw.mods.fml.common.functions; + +import java.util.Collection; +import java.util.List; +import com.google.common.collect.Collections2; +import com.google.common.collect.Lists; + +public class CollectionWrapperFactory { + /** + * Return a read only cast view of the supplied ungeneric collection, based on the element type given + * @param coll The collection to cast + * @param elementType the supertype contained in the collection + * @return a collection asserting that type relationship + */ + public static Collection wrap(@SuppressWarnings("rawtypes") Collection coll, Class elementType) + { + Collection asGeneric = coll; + return Collections2.transform(asGeneric, new TypeCastFunction(elementType)); + } + + /** + * Return a read only cast view of the supplied ungeneric list, based on the element type given + * @param list The list to cast + * @param elementType the supertype contained in the list + * @return a list asserting that type relationship + */ + public static List wrap(@SuppressWarnings("rawtypes") List list, Class elementType) + { + List asGeneric = list; + return Lists.transform(asGeneric, new TypeCastFunction(elementType)); + } +}