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  
18  package org.apache.commons.rng.examples.jmh.core;
19  
20  import org.apache.commons.rng.UniformRandomProvider;
21  
22  /**
23   * Defines baseline implementations for the {@link UniformRandomProvider}.
24   */
25  public final class BaselineUtils {
26      /** No public construction. */
27      private BaselineUtils() {}
28  
29      /**
30       * Default implementation of {@link UniformRandomProvider} that does nothing.
31       *
32       * <p>Note: This is not a good baseline as the JVM can optimise the predictable result
33       * of the method calls. This is here for convenience when implementing
34       * UniformRandomProvider.</p>
35       */
36      private abstract static class DefaultProvider implements UniformRandomProvider {
37          @Override
38          public void nextBytes(byte[] bytes) {
39              // Do nothing
40          }
41  
42          @Override
43          public void nextBytes(byte[] bytes, int start, int len) {
44              // Do nothing
45          }
46  
47          @Override
48          public int nextInt() {
49              return 0;
50          }
51  
52          @Override
53          public int nextInt(int n) {
54              return 0;
55          }
56  
57          @Override
58          public long nextLong() {
59              return 0;
60          }
61  
62          @Override
63          public long nextLong(long n) {
64              return 0;
65          }
66  
67          @Override
68          public boolean nextBoolean() {
69              return false;
70          }
71  
72          @Override
73          public float nextFloat() {
74              return 0;
75          }
76  
77          @Override
78          public double nextDouble() {
79              return 0;
80          }
81      }
82  
83      /**
84       * Baseline implementation for {@link UniformRandomProvider#nextBytes(byte[])} and
85       * {@link UniformRandomProvider#nextBytes(byte[], int, int)}.
86       */
87      private static final class BaselineNextBytes extends DefaultProvider {
88          /**
89           * The fixed value to fill the byte array.
90           *
91           * <p><strong>DON'T</strong> make this final!
92           * This must be a viewed by the JVM as something that cannot be optimised away.</p>
93           */
94          private byte value;
95  
96          @Override
97          public void nextBytes(byte[] bytes) {
98              for (int i = 0; i < bytes.length; i++) {
99                  bytes[i] = value;
100             }
101         }
102 
103         @Override
104         public void nextBytes(byte[] bytes, int start, int len) {
105             for (int i = start; i < len; i++) {
106                 bytes[i] = value;
107             }
108         }
109     }
110 
111     /**
112      * Baseline implementation for {@link UniformRandomProvider#nextInt()} and
113      * {@link UniformRandomProvider#nextInt(int)}.
114      */
115     private static final class BaselineNextInt extends DefaultProvider {
116         /**
117          * The fixed value to return.
118          *
119          * <p><strong>DON'T</strong> make this final!
120          * This must be a viewed by the JVM as something that cannot be optimised away.</p>
121          */
122         private int value;
123 
124         @Override
125         public int nextInt() {
126             return value;
127         }
128 
129         @Override
130         public int nextInt(int n) {
131             return value;
132         }
133     }
134 
135     /**
136      * Baseline implementation for {@link UniformRandomProvider#nextLong()} and
137      * {@link UniformRandomProvider#nextLong(long)}.
138      */
139     private static final class BaselineNextLong extends DefaultProvider {
140         /**
141          * The fixed value to return.
142          *
143          * <p><strong>DON'T</strong> make this final!
144          * This must be a viewed by the JVM as something that cannot be optimised away.</p>
145          */
146         private long value;
147 
148         @Override
149         public long nextLong() {
150             return value;
151         }
152 
153         @Override
154         public long nextLong(long n) {
155             return value;
156         }
157     }
158 
159     /**
160      * Baseline implementation for {@link UniformRandomProvider#nextBoolean()}.
161      */
162     private static final class BaselineNextBoolean extends DefaultProvider {
163         /**
164          * The fixed value to return.
165          *
166          * <p><strong>DON'T</strong> make this final!
167          * This must be a viewed by the JVM as something that cannot be optimised away.</p>
168          */
169         private boolean value;
170 
171         @Override
172         public boolean nextBoolean() {
173             return value;
174         }
175     }
176 
177     /**
178      * Baseline implementation for {@link UniformRandomProvider#nextFloat()}.
179      */
180     private static final class BaselineNextFloat extends DefaultProvider {
181         /**
182          * The fixed value to return.
183          *
184          * <p><strong>DON'T</strong> make this final!
185          * This must be a viewed by the JVM as something that cannot be optimised away.</p>
186          */
187         private float value;
188 
189         @Override
190         public float nextFloat() {
191             return value;
192         }
193     }
194 
195     /**
196      * Baseline implementation for {@link UniformRandomProvider#nextDouble()}.
197      */
198     private static final class BaselineNextDouble extends DefaultProvider {
199         /**
200          * The fixed value to return.
201          *
202          * <p><strong>DON'T</strong> make this final!
203          * This must be a viewed by the JVM as something that cannot be optimised away.</p>
204          */
205         private double value;
206 
207         @Override
208         public double nextDouble() {
209             return value;
210         }
211     }
212 
213     /**
214      * Gets a baseline provider for {@link UniformRandomProvider#nextBytes(byte[])} and
215      * {@link UniformRandomProvider#nextBytes(byte[], int, int)}.
216      *
217      * @return The baseline provider.
218      */
219     public static UniformRandomProvider getNextBytes() {
220         return new BaselineNextBytes();
221     }
222 
223     /**
224      * Gets a baseline provider for {@link UniformRandomProvider#nextInt()} and
225      * {@link UniformRandomProvider#nextInt(int)}.
226      *
227      * @return The baseline provider.
228      */
229     public static UniformRandomProvider getNextInt() {
230         return new BaselineNextInt();
231     }
232 
233     /**
234      * Gets a baseline provider for {@link UniformRandomProvider#nextLong()} and
235      * {@link UniformRandomProvider#nextLong(long)}.
236      *
237      * @return The baseline provider.
238      */
239     public static UniformRandomProvider getNextLong() {
240         return new BaselineNextLong();
241     }
242 
243     /**
244      * Gets a baseline provider for {@link UniformRandomProvider#nextBoolean()}.
245      *
246      * @return The baseline provider.
247      */
248     public static UniformRandomProvider getNextBoolean() {
249         return new BaselineNextBoolean();
250     }
251 
252     /**
253      * Gets a baseline provider for {@link UniformRandomProvider#nextFloat()}.
254      *
255      * @return The baseline provider.
256      */
257     public static UniformRandomProvider getNextFloat() {
258         return new BaselineNextFloat();
259     }
260 
261     /**
262      * Gets a baseline provider for {@link UniformRandomProvider#nextDouble()}.
263      *
264      * @return The baseline provider.
265      */
266     public static UniformRandomProvider getNextDouble() {
267         return new BaselineNextDouble();
268     }
269 }