test/testlibrary/jdk/test/lib/Utils.java
Print this page
@@ -410,10 +410,27 @@
}
return iterator.next();
}
/**
+ * Returns random element of non empty array
+ *
+ * @param <T> a type of array element
+ * @param array array of elements
+ * @return random element of array
+ * @throws IllegalArgumentException if array is empty
+ */
+ public static <T> T getRandomElement(T[] array)
+ throws IllegalArgumentException {
+ if (array == null || array.length == 0) {
+ throw new IllegalArgumentException("Empty or null array");
+ }
+ Random random = getRandomInstance();
+ return array[random.nextInt(array.length)];
+ }
+
+ /**
* Wait for condition to be true
*
* @param condition, a condition to wait for
*/
public static final void waitForCondition(BooleanSupplier condition) {