< prev index next >

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

Print this page

480      }
481 
482      /**
483       * Returns byte array of hex view
484       *
485       * @param hex hexadecimal string representation
486       * @return byte array
487       */
488      public static byte[] toByteArray(String hex) {
489          return HexFormat.of().parseHex(hex);
490      }
491 
492     /**
493      * Returns {@link java.util.Random} generator initialized with particular seed.
494      * The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}.
495      * In case no seed is provided and the build under test is "promotable"
496      * (its build number ({@code $BUILD} in {@link Runtime.Version}) is greater than 0,
497      * the seed based on string representation of {@link Runtime#version()} is used.
498      * Otherwise, the seed is randomly generated.
499      * The used seed printed to stdout.
500      *
501      * @return {@link java.util.Random} generator with particular seed.
502      */
503     public static Random getRandomInstance() {
504         if (RANDOM_GENERATOR == null) {
505             synchronized (Utils.class) {
506                 if (RANDOM_GENERATOR == null) {
507                     RANDOM_GENERATOR = new Random(SEED);
508                     System.out.printf("For random generator using seed: %d%n", SEED);
509                     System.out.printf("To re-run test with same seed value please add \"-D%s=%d\" to command line.%n", SEED_PROPERTY_NAME, SEED);
510                 }
511             }


512         }
513         return RANDOM_GENERATOR;
514     }
515 
516     /**
517      * Returns random element of non empty collection
518      *
519      * @param <T> a type of collection element
520      * @param collection collection of elements
521      * @return random element of collection
522      * @throws IllegalArgumentException if collection is empty
523      */
524     public static <T> T getRandomElement(Collection<T> collection)
525             throws IllegalArgumentException {
526         if (collection.isEmpty()) {
527             throw new IllegalArgumentException("Empty collection");
528         }
529         Random random = getRandomInstance();
530         int elementIndex = 1 + random.nextInt(collection.size() - 1);
531         Iterator<T> iterator = collection.iterator();

480      }
481 
482      /**
483       * Returns byte array of hex view
484       *
485       * @param hex hexadecimal string representation
486       * @return byte array
487       */
488      public static byte[] toByteArray(String hex) {
489          return HexFormat.of().parseHex(hex);
490      }
491 
492     /**
493      * Returns {@link java.util.Random} generator initialized with particular seed.
494      * The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}.
495      * In case no seed is provided and the build under test is "promotable"
496      * (its build number ({@code $BUILD} in {@link Runtime.Version}) is greater than 0,
497      * the seed based on string representation of {@link Runtime#version()} is used.
498      * Otherwise, the seed is randomly generated.
499      * The used seed printed to stdout.
500      * The printing is not in the synchronized block so as to prevent carrier threads starvation.
501      * @return {@link java.util.Random} generator with particular seed.
502      */
503     public static Random getRandomInstance() {
504         if (RANDOM_GENERATOR == null) {
505             synchronized (Utils.class) {
506                 if (RANDOM_GENERATOR == null) {
507                     RANDOM_GENERATOR = new Random(SEED);
508                 } else {
509                     return RANDOM_GENERATOR;
510                 }
511             }
512             System.out.printf("For random generator using seed: %d%n", SEED);
513             System.out.printf("To re-run test with same seed value please add \"-D%s=%d\" to command line.%n", SEED_PROPERTY_NAME, SEED);
514         }
515         return RANDOM_GENERATOR;
516     }
517 
518     /**
519      * Returns random element of non empty collection
520      *
521      * @param <T> a type of collection element
522      * @param collection collection of elements
523      * @return random element of collection
524      * @throws IllegalArgumentException if collection is empty
525      */
526     public static <T> T getRandomElement(Collection<T> collection)
527             throws IllegalArgumentException {
528         if (collection.isEmpty()) {
529             throw new IllegalArgumentException("Empty collection");
530         }
531         Random random = getRandomInstance();
532         int elementIndex = 1 + random.nextInt(collection.size() - 1);
533         Iterator<T> iterator = collection.iterator();
< prev index next >