From 4f8fd3072a61979d85e6b18f06647b144688e835 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Mon, 25 Jan 2016 15:36:11 +0100 Subject: [PATCH] Transform Iterables with internal support for speedup by Collection usage. --- .../massivecraft/massivecore/util/MUtil.java | 49 ++++++++++++++----- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/com/massivecraft/massivecore/util/MUtil.java b/src/com/massivecraft/massivecore/util/MUtil.java index 3073d431..2bdd632c 100644 --- a/src/com/massivecraft/massivecore/util/MUtil.java +++ b/src/com/massivecraft/massivecore/util/MUtil.java @@ -1363,17 +1363,40 @@ public class MUtil // TRANSFORM // -------------------------------------------- // - public static List transform(Collection items, Predicate where, Comparator orderby, Integer limit, Integer offset) + public static List transform(Iterable items, Predicate where, Comparator orderby, Integer limit, Integer offset) { - List ret = new ArrayList(items.size()); + // Collection + Collection collection = null; + if (items instanceof Collection) collection = (Collection)items; // WHERE + List ret; if (where == null) { - ret.addAll(items); + if (collection != null) + { + ret = new ArrayList(collection); + } + else + { + ret = new ArrayList(); + for (T item : items) + { + ret.add(item); + } + } } else { + if (collection != null) + { + ret = new ArrayList(collection.size()); + } + else + { + ret = new ArrayList(); + } + for (T item : items) { if (where.apply(item)) @@ -1427,16 +1450,16 @@ public class MUtil return new ArrayList(ret.subList(fromIndex, toIndex)); } - public static List transform(Collection items, Predicate where) { return transform(items, where, null, null, null); } - public static List transform(Collection items, Predicate where, Comparator orderby) { return transform(items, where, orderby, null, null); } - public static List transform(Collection items, Predicate where, Comparator orderby, Integer limit) { return transform(items, where, orderby, limit, null); } - public static List transform(Collection items, Predicate where, Integer limit) { return transform(items, where, null, limit, null); } - public static List transform(Collection items, Predicate where, Integer limit, Integer offset) { return transform(items, where, null, limit, offset); } - public static List transform(Collection items, Comparator orderby) { return transform(items, null, orderby, null, null); } - public static List transform(Collection items, Comparator orderby, Integer limit) { return transform(items, null, orderby, limit, null); } - public static List transform(Collection items, Comparator orderby, Integer limit, Integer offset) { return transform(items, null, orderby, limit, offset); } - public static List transform(Collection items, Integer limit) { return transform(items, null, null, limit, null); } - public static List transform(Collection items, Integer limit, Integer offset) { return transform(items, null, null, limit, offset); } + public static List transform(Iterable items, Predicate where) { return transform(items, where, null, null, null); } + public static List transform(Iterable items, Predicate where, Comparator orderby) { return transform(items, where, orderby, null, null); } + public static List transform(Iterable items, Predicate where, Comparator orderby, Integer limit) { return transform(items, where, orderby, limit, null); } + public static List transform(Iterable items, Predicate where, Integer limit) { return transform(items, where, null, limit, null); } + public static List transform(Iterable items, Predicate where, Integer limit, Integer offset) { return transform(items, where, null, limit, offset); } + public static List transform(Iterable items, Comparator orderby) { return transform(items, null, orderby, null, null); } + public static List transform(Iterable items, Comparator orderby, Integer limit) { return transform(items, null, orderby, limit, null); } + public static List transform(Iterable items, Comparator orderby, Integer limit, Integer offset) { return transform(items, null, orderby, limit, offset); } + public static List transform(Iterable items, Integer limit) { return transform(items, null, null, limit, null); } + public static List transform(Iterable items, Integer limit, Integer offset) { return transform(items, null, null, limit, offset); } // -------------------------------------------- // // SIMPLE CONSTRUCTORS