386 * @param <T> a type of collection element
387 * @param collection collection of elements
388 * @return random element of collection
389 * @throws IllegalArgumentException if collection is empty
390 */
391 public static <T> T getRandomElement(Collection<T> collection)
392 throws IllegalArgumentException {
393 if (collection.isEmpty()) {
394 throw new IllegalArgumentException("Empty collection");
395 }
396 Random random = getRandomInstance();
397 int elementIndex = 1 + random.nextInt(collection.size() - 1);
398 Iterator<T> iterator = collection.iterator();
399 while (--elementIndex != 0) {
400 iterator.next();
401 }
402 return iterator.next();
403 }
404
405 /**
406 * Wait for condition to be true
407 *
408 * @param condition, a condition to wait for
409 */
410 public static final void waitForCondition(BooleanSupplier condition) {
411 waitForCondition(condition, -1L, 100L);
412 }
413
414 /**
415 * Wait until timeout for condition to be true
416 *
417 * @param condition, a condition to wait for
418 * @param timeout a time in milliseconds to wait for condition to be true
419 * specifying -1 will wait forever
420 * @return condition value, to determine if wait was successful
421 */
422 public static final boolean waitForCondition(BooleanSupplier condition,
423 long timeout) {
424 return waitForCondition(condition, timeout, 100L);
425 }
|
386 * @param <T> a type of collection element
387 * @param collection collection of elements
388 * @return random element of collection
389 * @throws IllegalArgumentException if collection is empty
390 */
391 public static <T> T getRandomElement(Collection<T> collection)
392 throws IllegalArgumentException {
393 if (collection.isEmpty()) {
394 throw new IllegalArgumentException("Empty collection");
395 }
396 Random random = getRandomInstance();
397 int elementIndex = 1 + random.nextInt(collection.size() - 1);
398 Iterator<T> iterator = collection.iterator();
399 while (--elementIndex != 0) {
400 iterator.next();
401 }
402 return iterator.next();
403 }
404
405 /**
406 * Returns random element of non empty array
407 *
408 * @param <T> a type of array element
409 * @param array array of elements
410 * @return random element of array
411 * @throws IllegalArgumentException if array is empty
412 */
413 public static <T> T getRandomElement(T[] array)
414 throws IllegalArgumentException {
415 if (array == null || array.length == 0) {
416 throw new IllegalArgumentException("Empty or null array");
417 }
418 Random random = getRandomInstance();
419 return array[random.nextInt(array.length)];
420 }
421
422 /**
423 * Wait for condition to be true
424 *
425 * @param condition, a condition to wait for
426 */
427 public static final void waitForCondition(BooleanSupplier condition) {
428 waitForCondition(condition, -1L, 100L);
429 }
430
431 /**
432 * Wait until timeout for condition to be true
433 *
434 * @param condition, a condition to wait for
435 * @param timeout a time in milliseconds to wait for condition to be true
436 * specifying -1 will wait forever
437 * @return condition value, to determine if wait was successful
438 */
439 public static final boolean waitForCondition(BooleanSupplier condition,
440 long timeout) {
441 return waitForCondition(condition, timeout, 100L);
442 }
|