Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/RandomEx.java
41153 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package vm.share;2324import java.util.HashMap;25import java.util.Map;26import java.util.Random;27import java.util.function.Predicate;28import java.util.function.Supplier;2930import jdk.test.lib.Utils;3132public class RandomEx extends Random {33private final Map<Class<?>, Supplier<?>> map = new HashMap<>();3435{36map.put(Boolean.class, this::nextBoolean);37map.put(boolean.class, this::nextBoolean);38map.put(Byte.class, this::nextByte);39map.put(byte.class, this::nextByte);40map.put(Short.class, this::nextShort);41map.put(short.class, this::nextShort);42map.put(Character.class, this::nextChar);43map.put(char.class, this::nextChar);44map.put(Integer.class, this::nextInt);45map.put(int.class, this::nextInt);46map.put(Long.class, this::nextLong);47map.put(long.class, this::nextLong);48map.put(Float.class, this::nextFloat);49map.put(float.class, this::nextFloat);50map.put(Double.class, this::nextDouble);51map.put(double.class, this::nextDouble);52}5354public RandomEx() {55super(Utils.getRandomInstance().nextLong());56}5758public RandomEx(long seed) {59super(seed);60}6162public byte nextByte() {63return (byte) next(Byte.SIZE);64}6566public short nextShort() {67return (short) next(Short.SIZE);68}6970public char nextChar() {71return (char) next(Character.SIZE);72}7374public <T> T next(Predicate<T> p, T dummy) {75T result;76do {77result = next(dummy);78} while (!p.test(result));79return result;80}8182@SuppressWarnings("unchecked")83public <T> T next(T dummy) {84Supplier<?> supplier = map.get(dummy.getClass());85if (supplier == null) {86throw new IllegalArgumentException("supplier for <" +87dummy.getClass() + ">is not found");88}89return (T) supplier.get();90}91}929394