View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.rng.examples.stress;
18  
19  import org.apache.commons.rng.UniformRandomProvider;
20  import org.apache.commons.rng.simple.RandomSource;
21  
22  /**
23   * Encapsulate the data needed to create and test a random generator. This includes:
24   *
25   * <ul>
26   *   <li>An identifier for the random source
27   *   <li>The random source
28   *   <li>The constructor arguments
29   *   <li>The number of trials for each random source
30   * </ul>
31   */
32  class StressTestData {
33      /** The identifier. */
34      private final String id;
35      /** The random source. */
36      private final RandomSource randomSource;
37      /** The arguments used to construct the random source. */
38      private final Object[] args;
39      /** The number of trials. */
40      private final int trials;
41  
42      /**
43       * Creates a new instance.
44       *
45       * @param randomSource The random source.
46       * @param args The arguments used to construct the random source (can be {@code null}).
47       */
48      StressTestData(RandomSource randomSource,
49                     Object[] args) {
50          // The default ID is defined by the enum order.
51          this(Integer.toString(randomSource.ordinal() + 1),
52               randomSource,
53               args,
54               // Ignore by default (trials = 0) any source that has arguments
55               args == null ? 1 : 0);
56      }
57  
58      /**
59       * Creates a new instance.
60       *
61       * @param id The identifier.
62       * @param randomSource The random source.
63       * @param args The arguments used to construct the random source (can be
64       * {@code null}).
65       * @param trials The number of trials.
66       */
67      StressTestData(String id,
68                     RandomSource randomSource,
69                     Object[] args,
70                     int trials) {
71          this.id = id;
72          this.randomSource = randomSource;
73          this.args = args;
74          this.trials = trials;
75      }
76  
77      /**
78       * Create a new instance with the given ID prefix prepended to the current ID.
79       *
80       * @param idPrefix The ID prefix.
81       * @return the stress test data
82       */
83      StressTestData withIDPrefix(String idPrefix) {
84          return new StressTestData(idPrefix + id, randomSource, args, trials);
85      }
86  
87      /**
88       * Create a new instance with the given number of trials.
89       *
90       * @param numberOfTrials The number of trials.
91       * @return the stress test data
92       */
93      StressTestData withTrials(int numberOfTrials) {
94          return new StressTestData(id, randomSource, args, numberOfTrials);
95      }
96  
97      /**
98       * Creates the random generator.
99       *
100      * <p>It is recommended the seed is generated using {@link RandomSource#createSeed()}.</p>
101      *
102      * @param seed the seed (use {@code null} to automatically create a seed)
103      * @return the uniform random provider
104      */
105     UniformRandomProvider createRNG(byte[] seed) {
106         return randomSource.create(seed, args);
107     }
108 
109     /**
110      * Gets the identifier.
111      *
112      * @return the id
113      */
114     String getId() {
115         return id;
116     }
117 
118     /**
119      * Gets the random source.
120      *
121      * @return the random source
122      */
123     RandomSource getRandomSource() {
124         return randomSource;
125     }
126 
127     /**
128      * Gets the arguments used to construct the random source.
129      *
130      * @return the arguments
131      */
132     Object[] getArgs() {
133         return args;
134     }
135 
136     /**
137      * Gets the number of trials.
138      *
139      * @return the number of trials
140      */
141     int getTrials() {
142         return trials;
143     }
144 }