mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2024-12-23 01:37:37 +00:00
Make custom toArray methods fulfill the method contract (#2089)
* Make PropertyKeySet#toArray(T) fulfill the method contract * Make LocalBlockVectorSet#toArray(T) fulfill the method contract
This commit is contained in:
parent
a63037b92d
commit
90baa790c3
@ -6,6 +6,7 @@ import com.sk89q.worldedit.math.BlockVector3;
|
|||||||
import com.zaxxer.sparsebits.SparseBitSet;
|
import com.zaxxer.sparsebits.SparseBitSet;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
@ -235,12 +236,15 @@ public class LocalBlockVectorSet implements BlockVector3Set {
|
|||||||
return toArray((Object[]) null);
|
return toArray((Object[]) null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public <T> T[] toArray(T[] array) {
|
public <T> T[] toArray(T[] array) {
|
||||||
int size = size();
|
int size = size();
|
||||||
if (array == null || array.length < size) {
|
if (array.length < size) {
|
||||||
array = (T[]) new BlockVector3[size];
|
array = Arrays.copyOf(array, size);
|
||||||
|
} else if (array.length > size) {
|
||||||
|
array[size] = null; // mark as end to comply with the method contract
|
||||||
}
|
}
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
|
@ -73,7 +73,16 @@ public class PropertyKeySet implements Set<PropertyKey> {
|
|||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public <T> T[] toArray(@Nonnull T[] a) {
|
public <T> T[] toArray(@Nonnull T[] a) {
|
||||||
T[] array = Arrays.copyOf(a, this.bits.cardinality());
|
T[] array;
|
||||||
|
final int cardinality = this.bits.cardinality();
|
||||||
|
if (cardinality > a.length) {
|
||||||
|
array = Arrays.copyOf(a, cardinality);
|
||||||
|
} else {
|
||||||
|
array = a;
|
||||||
|
if (a.length > cardinality) {
|
||||||
|
array[cardinality] = null; // mark as end to comply with the method contract
|
||||||
|
}
|
||||||
|
}
|
||||||
Iterator<PropertyKey> iter = iterator();
|
Iterator<PropertyKey> iter = iterator();
|
||||||
for (int i = 0; i < array.length && iter.hasNext(); i++) {
|
for (int i = 0; i < array.length && iter.hasNext(); i++) {
|
||||||
array[i] = (T) iter.next();
|
array[i] = (T) iter.next();
|
||||||
|
Loading…
Reference in New Issue
Block a user