mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-17 23:16:01 +00:00
33 lines
771 B
Java
33 lines
771 B
Java
package dev.tommyjs.futur.joiner;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
class ConcurrentResultArray<T> {
|
|
|
|
private final AtomicReference<T[]> ref;
|
|
|
|
public ConcurrentResultArray(int expectedSize) {
|
|
//noinspection unchecked
|
|
this.ref = new AtomicReference<>((T[]) new Object[expectedSize]);
|
|
}
|
|
|
|
public void set(int index, T element) {
|
|
ref.updateAndGet(array -> {
|
|
if (array.length <= index)
|
|
return Arrays.copyOf(array, index + 6);
|
|
|
|
array[index] = element;
|
|
return array;
|
|
});
|
|
}
|
|
|
|
public @NotNull List<T> toList() {
|
|
return Arrays.asList(ref.get());
|
|
}
|
|
|
|
}
|