From 9fdf3304951ac1291294f062666f66610fe10c19 Mon Sep 17 00:00:00 2001 From: Prototik Date: Sat, 27 Jun 2015 02:12:55 +0700 Subject: [PATCH] Fix #67 --- .../bukkit/craftbukkit/util/LongHashSet.java | 88 +++++++++++++++++-- 1 file changed, 83 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java b/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java index 7f659b7..4277d36 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java @@ -16,11 +16,13 @@ package org.bukkit.craftbukkit.util; +import java.util.Collection; import java.util.Iterator; import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; +import java.util.Set; -public class LongHashSet { +public class LongHashSet implements Set { private final static int INITIAL_SIZE = 3; private final static double LOAD_FACTOR = 0.75; @@ -44,14 +46,17 @@ public class LongHashSet { modCount = 0; } - public Iterator iterator() { + @Override + public Iterator iterator() { return new Itr(); } + @Override public int size() { return elements; } + @Override public boolean isEmpty() { return elements == 0; } @@ -183,6 +188,7 @@ public class LongHashSet { } } + @Override public void clear() { elements = 0; for (int ix = 0; ix < values.length; ix++) { @@ -194,7 +200,7 @@ public class LongHashSet { flat = new org.spigotmc.FlatMap(); } - public long[] toArray() { + public long[] toPrimitiveArray() { long[] result = new long[elements]; long[] values = Java15Compat.Arrays_copyOf(this.values, this.values.length); int pos = 0; @@ -208,6 +214,26 @@ public class LongHashSet { return result; } + @Override + public Long[] toArray() { + Long[] result = new Long[elements]; + long[] values = Java15Compat.Arrays_copyOf(this.values, this.values.length); + int pos = 0; + + for (long value : values) { + if (value != FREE && value != REMOVED) { + result[pos++] = value; + } + } + + return result; + } + + @Override + public T[] toArray(T[] arg0) { + throw new UnsupportedOperationException(); + } + public long popFirst() { for (long value : values) { if (value != FREE && value != REMOVED) { @@ -220,7 +246,7 @@ public class LongHashSet { } public long[] popAll() { - long[] ret = toArray(); + long[] ret = toPrimitiveArray(); clear(); return ret; } @@ -273,7 +299,7 @@ public class LongHashSet { freeEntries = values.length - elements; } - private class Itr implements Iterator { + private class Itr implements Iterator { private int index; private int lastReturned = -1; private int expectedModCount; @@ -285,10 +311,12 @@ public class LongHashSet { expectedModCount = modCount; } + @Override public boolean hasNext() { return index != values.length; } + @Override public Long next() { if (modCount != expectedModCount) { throw new ConcurrentModificationException(); @@ -312,6 +340,7 @@ public class LongHashSet { } } + @Override public void remove() { if (modCount != expectedModCount) { throw new ConcurrentModificationException(); @@ -329,4 +358,53 @@ public class LongHashSet { } } } + + @Override + public boolean add(Long value) { + return add(value.longValue()); + } + + @Override + public boolean addAll(Collection collection) { + boolean result = false; + for (Long value : collection) result |= add(value.longValue()); + return result; + } + + @Override + public boolean contains(Object o) { + return o instanceof Long ? contains(((Long) o).longValue()) : false; + } + + @Override + public boolean containsAll(Collection collection) { + for (Object value : collection) if (!contains(value)) return false; + return true; + } + + @Override + public boolean remove(Object o) { + return o instanceof Long ? remove(((Long) o).longValue()) : false; + } + + @Override + public boolean removeAll(Collection collection) { + boolean result = false; + for (Object value : collection) result |= remove(value); + return result; + } + + @Override + public boolean retainAll(Collection collection) { + boolean result = false; + Iterator iterator = iterator(); + while(iterator.hasNext()) { + Long l = iterator.next(); + if (!collection.contains(l)) { + iterator.remove(); + result = true; + } + } + return result; + } }