1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.testlibrary;
  25 
  26 import java.util.Random;
  27 import java.util.SplittableRandom;
  28 
  29 /**
  30  * Factory class which generates and prints to STDOUT a long-valued seed
  31  * for use in initializing a PRNG.  An instance of {@code Random} or
  32  * {@code SplittableRandom} may likewise be obtained.
  33  *
  34  * @deprecated  This class is deprecated. Use the one from
  35  *              {@code <root>/test/lib/jdk/test/lib}
  36  *
  37  */
  38 @Deprecated
  39 public class RandomFactory {
  40     /**
  41      * Attempt to obtain the seed from the value of the "seed" property.
  42      * @return The seed or {@code null} if the "seed" property was not set or
  43      *         could not be parsed.
  44      */
  45     private static Long getSystemSeed() {
  46         Long seed = null;
  47         try {
  48             // note that Long.valueOf(null) also throws a
  49             // NumberFormatException so if the property is undefined this
  50             // will still work correctly
  51             seed = Long.valueOf(System.getProperty("seed"));
  52         } catch (NumberFormatException e) {
  53             // do nothing: seed is still null
  54         }
  55 
  56         return seed;
  57     }
  58 
  59     /**
  60      * Obtain a seed from an independent PRNG.
  61      *
  62      * @return A random seed.
  63      */
  64     private static long getRandomSeed() {
  65         return new Random().nextLong();
  66     }
  67 
  68     /**
  69      * Obtain and print to STDOUT a seed appropriate for initializing a PRNG.
  70      * If the system property "seed" is set and has value which may be correctly
  71      * parsed it is used, otherwise a seed is generated using an independent
  72      * PRNG.
  73      *
  74      * @return The seed.
  75      */
  76     public static long getSeed() {
  77         Long seed = getSystemSeed();
  78         if (seed == null) {
  79             seed = getRandomSeed();
  80         }
  81         System.out.println("Seed from RandomFactory = "+seed+"L");
  82         return seed;
  83     }
  84 
  85     /**
  86      * Obtain and print to STDOUT a seed and use it to initialize a new
  87      * {@code Random} instance which is returned. If the system
  88      * property "seed" is set and has value which may be correctly parsed it
  89      * is used, otherwise a seed is generated using an independent PRNG.
  90      *
  91      * @return The {@code Random} instance.
  92      */
  93     public static Random getRandom() {
  94         return new Random(getSeed());
  95     }
  96 
  97     /**
  98      * Obtain and print to STDOUT a seed and use it to initialize a new
  99      * {@code SplittableRandom} instance which is returned. If the system
 100      * property "seed" is set and has value which may be correctly parsed it
 101      * is used, otherwise a seed is generated using an independent PRNG.
 102      *
 103      * @return The {@code SplittableRandom} instance.
 104      */
 105     public static SplittableRandom getSplittableRandom() {
 106         return new SplittableRandom(getSeed());
 107     }
 108 }