kotlin.jvm.internal.CollectionToArray Maven / Gradle / Ivy
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.jvm.internal;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
public class CollectionToArray {
// Based on the implementation from AbstractCollection
@SuppressWarnings("SSBasedInspection")
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
public static Object[] toArray(Collection> collection) {
int size = collection.size();
if (size == 0) return EMPTY_OBJECT_ARRAY;
Object[] r = new Object[size];
Iterator> it = collection.iterator();
for (int i = 0; i < size; i++) {
if (!it.hasNext()) {
// fewer elements than expected
return Arrays.copyOf(r, i);
}
r[i] = it.next();
}
return it.hasNext() ? finishToArray(r, it) : r;
}
public static T[] toArray(Collection collection, T[] a) {
// Estimate size of array; be prepared to see more or fewer elements
int size = collection.size();
T[] r = a.length >= size ? a :
(T[])java.lang.reflect.Array
.newInstance(a.getClass().getComponentType(), size);
Iterator it = collection.iterator();
for (int i = 0; i < r.length; i++) {
if (! it.hasNext()) { // fewer elements than expected
if (a != r)
return Arrays.copyOf(r, i);
r[i] = null; // null-terminate
return r;
}
r[i] = (T)it.next();
}
return it.hasNext() ? finishToArray(r, it) : r;
}
private static T[] finishToArray(T[] r, Iterator> it) {
int i = r.length;
while (it.hasNext()) {
int cap = r.length;
if (i == cap) {
int newCap = ((cap / 2) + 1) * 3;
if (newCap <= cap) { // integer overflow
if (cap == Integer.MAX_VALUE)
throw new OutOfMemoryError
("Required array size too large");
newCap = Integer.MAX_VALUE;
}
r = Arrays.copyOf(r, newCap);
}
r[i++] = (T)it.next();
}
// trim if overallocated
return (i == r.length) ? r : Arrays.copyOf(r, i);
}
private CollectionToArray() {
}
}