Brian Stanke
Committed by Gerrit Code Review

ONOS-1770 DefaultDistributedSet.toArray(T[]) does not support array size different than set size.

Change-Id: Ibf3e32ac466e1142a17457b52e091897326d8ba8
...@@ -24,6 +24,7 @@ import org.onosproject.store.service.MapEventListener; ...@@ -24,6 +24,7 @@ import org.onosproject.store.service.MapEventListener;
24 import org.onosproject.store.service.SetEvent; 24 import org.onosproject.store.service.SetEvent;
25 import org.onosproject.store.service.SetEventListener; 25 import org.onosproject.store.service.SetEventListener;
26 26
27 +import java.lang.reflect.Array;
27 import java.util.Collection; 28 import java.util.Collection;
28 import java.util.Iterator; 29 import java.util.Iterator;
29 import java.util.Map; 30 import java.util.Map;
...@@ -117,7 +118,13 @@ public class DefaultDistributedSet<E> implements DistributedSet<E> { ...@@ -117,7 +118,13 @@ public class DefaultDistributedSet<E> implements DistributedSet<E> {
117 public <T> T[] toArray(T[] a) { 118 public <T> T[] toArray(T[] a) {
118 final MeteringAgent.Context timer = monitor.startTimer(TO_ARRAY); 119 final MeteringAgent.Context timer = monitor.startTimer(TO_ARRAY);
119 try { 120 try {
120 - return backingMap.keySet().stream().toArray(size -> a); 121 + // TODO: Optimize this to only allocate a new array if the set size
122 + // is larger than the array.length. If the set size is smaller than
123 + // the array.length then copy the data into the array and set the
124 + // last element in the array to be null.
125 + final T[] resizedArray =
126 + (T[]) Array.newInstance(a.getClass().getComponentType(), backingMap.keySet().size());
127 + return (T[]) backingMap.keySet().toArray(resizedArray);
121 } finally { 128 } finally {
122 timer.stop(null); 129 timer.stop(null);
123 } 130 }
......