test/testlibrary/jdk/test/lib/Utils.java

Print this page
rev 9056 : [mq]: existing-directives

*** 401,410 **** --- 401,427 ---- } 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) {