ONOS-1770 DefaultDistributedSet.toArray(T[]) does not support array size different than set size.
Change-Id: Ibf3e32ac466e1142a17457b52e091897326d8ba8
diff --git a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultDistributedSet.java b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultDistributedSet.java
index 677724d..42ed615 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultDistributedSet.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultDistributedSet.java
@@ -24,6 +24,7 @@
import org.onosproject.store.service.SetEvent;
import org.onosproject.store.service.SetEventListener;
+import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
@@ -117,7 +118,13 @@
public <T> T[] toArray(T[] a) {
final MeteringAgent.Context timer = monitor.startTimer(TO_ARRAY);
try {
- return backingMap.keySet().stream().toArray(size -> a);
+ // TODO: Optimize this to only allocate a new array if the set size
+ // is larger than the array.length. If the set size is smaller than
+ // the array.length then copy the data into the array and set the
+ // last element in the array to be null.
+ final T[] resizedArray =
+ (T[]) Array.newInstance(a.getClass().getComponentType(), backingMap.keySet().size());
+ return (T[]) backingMap.keySet().toArray(resizedArray);
} finally {
timer.stop(null);
}