1 /*
2  * Copyright (c) 2013, 2020, 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 package vm.share;
24 
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Random;
28 import java.util.function.Predicate;
29 import java.util.function.Supplier;
30 
31 import jdk.test.lib.Utils;
32 
33 public class RandomEx extends Random {
34     private final Map<Class<?>, Supplier<?>> map = new HashMap<>();
35 
36     {
map.put(Boolean.class, this::nextBoolean)37         map.put(Boolean.class, this::nextBoolean);
map.put(boolean.class, this::nextBoolean)38         map.put(boolean.class, this::nextBoolean);
map.put(Byte.class, this::nextByte)39         map.put(Byte.class, this::nextByte);
map.put(byte.class, this::nextByte)40         map.put(byte.class, this::nextByte);
map.put(Short.class, this::nextShort)41         map.put(Short.class, this::nextShort);
map.put(short.class, this::nextShort)42         map.put(short.class, this::nextShort);
map.put(Character.class, this::nextChar)43         map.put(Character.class, this::nextChar);
map.put(char.class, this::nextChar)44         map.put(char.class, this::nextChar);
map.put(Integer.class, this::nextInt)45         map.put(Integer.class, this::nextInt);
map.put(int.class, this::nextInt)46         map.put(int.class, this::nextInt);
map.put(Long.class, this::nextLong)47         map.put(Long.class, this::nextLong);
map.put(long.class, this::nextLong)48         map.put(long.class, this::nextLong);
map.put(Float.class, this::nextFloat)49         map.put(Float.class, this::nextFloat);
map.put(float.class, this::nextFloat)50         map.put(float.class, this::nextFloat);
map.put(Double.class, this::nextDouble)51         map.put(Double.class, this::nextDouble);
map.put(double.class, this::nextDouble)52         map.put(double.class, this::nextDouble);
53     }
54 
RandomEx()55     public RandomEx() {
56         super(Utils.getRandomInstance().nextLong());
57     }
58 
RandomEx(long seed)59     public RandomEx(long seed) {
60         super(seed);
61     }
62 
nextByte()63     public byte nextByte() {
64         return (byte) next(Byte.SIZE);
65     }
66 
nextShort()67     public short nextShort() {
68         return (short) next(Short.SIZE);
69     }
70 
nextChar()71     public char nextChar() {
72         return (char) next(Character.SIZE);
73     }
74 
next(Predicate<T> p, T dummy)75     public <T> T next(Predicate<T> p, T dummy) {
76         T result;
77         do {
78             result = next(dummy);
79         } while (!p.test(result));
80         return result;
81     }
82 
83     @SuppressWarnings("unchecked")
next(T dummy)84     public <T> T next(T dummy) {
85         Supplier<?> supplier = map.get(dummy.getClass());
86         if (supplier == null) {
87             throw new IllegalArgumentException("supplier for <" +
88                     dummy.getClass() + ">is not found");
89         }
90         return (T) supplier.get();
91     }
92 }
93