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