From 87be4f0914c05278fa8b6de854055040ba06fa90 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sat, 22 Aug 2015 13:31:48 +0200 Subject: [PATCH] removeAll retainAll performance fix in MassiveSet. Added isSynchronous convenience method. --- .../massivecore/collections/MassiveSet.java | 21 +++++++++++++++++++ .../massivecore/event/EventMassiveCore.java | 9 ++++++++ 2 files changed, 30 insertions(+) diff --git a/src/com/massivecraft/massivecore/collections/MassiveSet.java b/src/com/massivecraft/massivecore/collections/MassiveSet.java index 5fa3341e..b0124dba 100644 --- a/src/com/massivecraft/massivecore/collections/MassiveSet.java +++ b/src/com/massivecraft/massivecore/collections/MassiveSet.java @@ -3,7 +3,9 @@ package com.massivecraft.massivecore.collections; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.LinkedHashSet; +import java.util.List; /** * This subclass adds better constructors. @@ -51,5 +53,24 @@ public class MassiveSet extends LinkedHashSet { this(Arrays.asList(elements)); } + + // -------------------------------------------- // + // OPTIMIZE: REMOVE ALL & RETAIN ALL + // -------------------------------------------- // + // This will greatly reduce the complexity in cases with big sizes. + + @Override + public boolean removeAll(Collection c) + { + if (c instanceof List) c = new HashSet(c); + return super.removeAll(c); + } + + @Override + public boolean retainAll(Collection c) + { + if (c instanceof List) c = new HashSet(c); + return super.retainAll(c); + } } diff --git a/src/com/massivecraft/massivecore/event/EventMassiveCore.java b/src/com/massivecraft/massivecore/event/EventMassiveCore.java index 7bd8a4be..f9e75bcd 100644 --- a/src/com/massivecraft/massivecore/event/EventMassiveCore.java +++ b/src/com/massivecraft/massivecore/event/EventMassiveCore.java @@ -44,4 +44,13 @@ public abstract class EventMassiveCore extends Event implements Runnable, Cancel super(isAsync); } + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // + + public boolean isSynchronous() + { + return !this.isAsynchronous(); + } + }