diff options
author | Travis Watkins <amaranth@ubuntu.com> | 2012-05-31 20:14:28 -0500 |
---|---|---|
committer | Travis Watkins <amaranth@ubuntu.com> | 2012-06-01 13:03:59 -0500 |
commit | 70c91c161ee3e70bb18a409347a6353b93a691e8 (patch) | |
tree | ff54e949289aff4f4c726ff6e0d033716e655586 /src | |
parent | 894ea065676cbf32f47c99ce91d605fab52a7a5a (diff) | |
download | craftbukkit-70c91c161ee3e70bb18a409347a6353b93a691e8.tar craftbukkit-70c91c161ee3e70bb18a409347a6353b93a691e8.tar.gz craftbukkit-70c91c161ee3e70bb18a409347a6353b93a691e8.tar.lz craftbukkit-70c91c161ee3e70bb18a409347a6353b93a691e8.tar.xz craftbukkit-70c91c161ee3e70bb18a409347a6353b93a691e8.zip |
Resize UnsafeList to initial capacity when cleared and implement clone.
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/bukkit/craftbukkit/util/UnsafeList.java | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/util/UnsafeList.java b/src/main/java/org/bukkit/craftbukkit/util/UnsafeList.java index 818ca5e0..c6deec14 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/UnsafeList.java +++ b/src/main/java/org/bukkit/craftbukkit/util/UnsafeList.java @@ -5,21 +5,22 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.AbstractList; -import java.util.Arrays; import java.util.List; import java.util.RandomAccess; // implementation of an ArrayList that offers a getter without range checks public class UnsafeList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable { - private static final long serialVersionUID = 8683452581112892189L; + private static final long serialVersionUID = 8683452581112892190L; private transient Object[] data; private int size; + private int initialCapacity; public UnsafeList(int capacity) { super(); if (capacity < 0) capacity = 128; int rounded = Integer.highestOneBit(capacity - 1) << 1; data = new Object[rounded]; + initialCapacity = rounded; } public UnsafeList() { @@ -95,11 +96,9 @@ public class UnsafeList<E> extends AbstractList<E> implements List<E>, RandomAcc } public void clear() { - for (int i = 0; i < size; i++) { - data[i] = null; - } - + // Create new array to reset memory usage to initial capacity size = 0; + data = new Object[initialCapacity]; } // actually rounds up to nearest power of two @@ -137,6 +136,7 @@ public class UnsafeList<E> extends AbstractList<E> implements List<E>, RandomAcc os.defaultWriteObject(); os.writeInt(size); + os.writeInt(initialCapacity); for (int i = 0; i < size; i++) { os.writeObject(data[i]); } @@ -146,9 +146,23 @@ public class UnsafeList<E> extends AbstractList<E> implements List<E>, RandomAcc is.defaultReadObject(); size = is.readInt(); + initialCapacity = is.readInt(); data = new Object[Integer.highestOneBit(size - 1) << 1]; for (int i = 0; i < size; i++) { data[i] = is.readObject(); } } + + public UnsafeList<E> clone() { + try { + UnsafeList<E> copy = (UnsafeList<E>) super.clone(); + copy.data = Java15Compat.Arrays_copyOf(data, size); + copy.size = size; + copy.initialCapacity = initialCapacity; + return copy; + } catch (CloneNotSupportedException ex) { + // This should never happen + return null; + } + } } |