mirror of
https://github.com/maxkratz/fastutil.git
synced 2024-09-16 11:58:09 +00:00
toArray() now returns a fixed zero-length array
This commit is contained in:
parent
0b8b49000f
commit
c29b109fe3
5 changed files with 13 additions and 6 deletions
|
@ -346,6 +346,8 @@ public abstract class ABSTRACT_LIST KEY_GENERIC extends ABSTRACT_COLLECTION KEY_
|
|||
@Override
|
||||
public Object[] toArray() {
|
||||
final int size = size();
|
||||
// A subtle part of the spec says the returned array must be Object[] exactly.
|
||||
if (size == 0) return it.unimi.dsi.fastutil.objects.ObjectArrays.EMPTY_ARRAY;
|
||||
Object[] ret = new Object[size];
|
||||
getElements(0, ret, 0, size);
|
||||
return ret;
|
||||
|
@ -484,6 +486,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC extends ABSTRACT_COLLECTION KEY_
|
|||
@Override
|
||||
public KEY_TYPE[] TO_KEY_ARRAY() {
|
||||
final int size = size();
|
||||
if (size == 0) return ARRAYS.EMPTY_ARRAY;
|
||||
KEY_TYPE[] ret = new KEY_TYPE[size];
|
||||
getElements(0, ret, 0, size);
|
||||
return ret;
|
||||
|
|
|
@ -918,8 +918,10 @@ public class ARRAY_LIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements
|
|||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
final int size = size();
|
||||
// A subtle part of the spec says the returned array must be Object[] exactly.
|
||||
return Arrays.copyOf(a, size(), Object[].class);
|
||||
if (size == 0) return it.unimi.dsi.fastutil.objects.ObjectArrays.EMPTY_ARRAY;
|
||||
return Arrays.copyOf(a, size, Object[].class);
|
||||
}
|
||||
|
||||
SUPPRESS_WARNINGS_KEY_UNCHECKED
|
||||
|
|
|
@ -373,6 +373,7 @@ public class ARRAY_SET KEY_GENERIC extends ABSTRACT_SET KEY_GENERIC implements j
|
|||
#if KEYS_PRIMITIVE
|
||||
@Override
|
||||
public KEY_TYPE[] TO_KEY_ARRAY() {
|
||||
if (size == 0) return ARRAYS.EMPTY_ARRAY;
|
||||
return java.util.Arrays.copyOf(a, size);
|
||||
}
|
||||
|
||||
|
@ -385,7 +386,9 @@ public class ARRAY_SET KEY_GENERIC extends ABSTRACT_SET KEY_GENERIC implements j
|
|||
#else // KEYS_REFERENCE
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
final int size = size();
|
||||
// A subtle part of the spec says the returned array must be Object[] exactly.
|
||||
if (size == 0) return it.unimi.dsi.fastutil.objects.ObjectArrays.EMPTY_ARRAY;
|
||||
return java.util.Arrays.copyOf(a, size, Object[].class);
|
||||
}
|
||||
|
||||
|
|
|
@ -252,8 +252,7 @@ public final class BIG_LISTS {
|
|||
|
||||
@Override
|
||||
public KEY_TYPE[] TO_KEY_ARRAY() {
|
||||
KEY_TYPE a[] = new KEY_TYPE[1];
|
||||
a[0] = element;
|
||||
KEY_TYPE a[] = {element};
|
||||
return a;
|
||||
}
|
||||
|
||||
|
|
|
@ -264,6 +264,7 @@ public class IMMUTABLE_LIST KEY_GENERIC extends LISTS.ImmutableListBase KEY_GENE
|
|||
#if KEYS_PRIMITIVE
|
||||
@Override
|
||||
public KEY_TYPE[] TO_KEY_ARRAY() {
|
||||
if (a.length == 0) return ARRAYS.EMPTY_ARRAY;
|
||||
return a.clone();
|
||||
}
|
||||
|
||||
|
@ -277,9 +278,8 @@ public class IMMUTABLE_LIST KEY_GENERIC extends LISTS.ImmutableListBase KEY_GENE
|
|||
@Override
|
||||
public Object[] toArray() {
|
||||
// A subtle part of the spec says the returned array must be Object[] exactly.
|
||||
if (a.getClass().equals(Object[].class)) {
|
||||
return a.clone();
|
||||
}
|
||||
if (a.length == 0) return it.unimi.dsi.fastutil.objects.ObjectArrays.EMPTY_ARRAY;
|
||||
if (a.getClass() == Object[].class) return a.clone();
|
||||
return java.util.Arrays.copyOf(a, a.length, Object[].class);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue