From 9d9ad6bd1e2bfaf08b7aa530cb101e1bd00118db Mon Sep 17 00:00:00 2001 From: Erik Broes Date: Wed, 2 Mar 2011 15:22:45 +0100 Subject: [PATCH] Java 1.5 compat. --- .../bukkit/craftbukkit/util/Java15Compat.java | 34 +++++++++++++++++++ .../bukkit/craftbukkit/util/LongHashset.java | 8 ++--- .../craftbukkit/util/LongHashtable.java | 3 +- 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/bukkit/craftbukkit/util/Java15Compat.java diff --git a/src/main/java/org/bukkit/craftbukkit/util/Java15Compat.java b/src/main/java/org/bukkit/craftbukkit/util/Java15Compat.java new file mode 100644 index 0000000000..a9ccc70bc5 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/util/Java15Compat.java @@ -0,0 +1,34 @@ +package org.bukkit.craftbukkit.util; + +import java.lang.reflect.Array; + +public class Java15Compat { + public static T[] Arrays_copyOf(T[] original, int newLength) { + if (0 <= newLength) { + return org.bukkit.util.Java15Compat.Arrays_copyOfRange(original, 0, newLength); + } + throw new NegativeArraySizeException(); + } + + public static long[] Arrays_copyOf(long[] original, int newLength) { + if (0 <= newLength) { + return Arrays_copyOfRange(original, 0, newLength); + } + throw new NegativeArraySizeException(); + } + + private static long[] Arrays_copyOfRange(long[] original, int start, int end) { + if (original.length >= start && 0 <= start) { + if (start <= end) { + int length = end - start; + int copyLength = Math.min( length, original.length - start); + long[] copy = (long[]) Array.newInstance(original.getClass().getComponentType(), length); + System.arraycopy(original, start, copy, 0, copyLength); + return copy; + } + throw new IllegalArgumentException(); + } + throw new ArrayIndexOutOfBoundsException(); + } + +} diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java b/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java index ecc698316f..2f8c2ccb3b 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java @@ -1,5 +1,5 @@ package org.bukkit.craftbukkit.util; -import java.util.Arrays; +import static org.bukkit.craftbukkit.util.Java15Compat.Arrays_copyOf; public class LongHashset extends LongHash { long values[][][] = new long[256][][]; @@ -31,7 +31,7 @@ public class LongHashset extends LongHash { return; } } - outer[outerIdx] = inner = Arrays.copyOf(inner, i+1); + outer[outerIdx] = inner = Arrays_copyOf(inner, i+1); inner[i] = key; count++; } @@ -66,7 +66,7 @@ public class LongHashset extends LongHash { if(i != max) { inner[i] = inner[max]; } - outer[(int) ((key >> 32) & 255)] = (max == 0 ? null : Arrays.copyOf(inner, max)); + outer[(int) ((key >> 32) & 255)] = (max == 0 ? null : Arrays_copyOf(inner, max)); return; } } @@ -80,7 +80,7 @@ public class LongHashset extends LongHash { if(inner == null || inner.length == 0) continue; count--; long ret = inner[inner.length - 1]; - outer[i] = Arrays.copyOf(inner, inner.length - 1); + outer[i] = Arrays_copyOf(inner, inner.length - 1); return ret; } diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHashtable.java b/src/main/java/org/bukkit/craftbukkit/util/LongHashtable.java index 5ff9b59514..1dffee0795 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/LongHashtable.java +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHashtable.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Arrays; import net.minecraft.server.Chunk; import net.minecraft.server.MinecraftServer; +import static org.bukkit.craftbukkit.util.Java15Compat.Arrays_copyOf; public class LongHashtable extends LongHash { @@ -55,7 +56,7 @@ public class LongHashtable extends LongHash return; } } - outer[outerIdx] = inner = Arrays.copyOf(inner, i + i); + outer[outerIdx] = inner = Arrays_copyOf(inner, i + i); inner[i] = new Entry(key, value); } }