SHIRO-566 Use Collections wrappers to save memory and cpu. Fixes #19 cherry-picked from: 109e6248353c3c9b8792233d2d824ff97de5b8fb
Project: http://git-wip-us.apache.org/repos/asf/shiro/repo Commit: http://git-wip-us.apache.org/repos/asf/shiro/commit/fd161e0b Tree: http://git-wip-us.apache.org/repos/asf/shiro/tree/fd161e0b Diff: http://git-wip-us.apache.org/repos/asf/shiro/diff/fd161e0b Branch: refs/heads/1.3.x Commit: fd161e0be34246e20b423a67803da5c94a372030 Parents: cb0c225 Author: Andreas Kohn <[email protected]> Authored: Tue Jun 28 18:47:18 2016 +0200 Committer: Brian Demers <[email protected]> Committed: Wed Jun 29 14:59:59 2016 -0700 ---------------------------------------------------------------------- .../java/org/apache/shiro/util/CollectionUtils.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/shiro/blob/fd161e0b/core/src/main/java/org/apache/shiro/util/CollectionUtils.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/shiro/util/CollectionUtils.java b/core/src/main/java/org/apache/shiro/util/CollectionUtils.java index 0956efa..af33121 100644 --- a/core/src/main/java/org/apache/shiro/util/CollectionUtils.java +++ b/core/src/main/java/org/apache/shiro/util/CollectionUtils.java @@ -35,6 +35,11 @@ public class CollectionUtils { if (elements == null || elements.length == 0) { return Collections.emptySet(); } + + if (elements.length == 1) { + return Collections.singleton(elements[0]); + } + LinkedHashSet<E> set = new LinkedHashSet<E>(elements.length * 4 / 3 + 1); Collections.addAll(set, elements); return set; @@ -106,11 +111,9 @@ public class CollectionUtils { if (elements == null || elements.length == 0) { return Collections.emptyList(); } - // Avoid integer overflow when a large array is passed in - int capacity = computeListCapacity(elements.length); - ArrayList<E> list = new ArrayList<E>(capacity); - Collections.addAll(list, elements); - return list; + + // Integer overflow does not occur when a large array is passed in because the list array already exists + return Arrays.asList(elements); } /*public static <E> Deque<E> asDeque(E... elements) {
