1/* 2 * Copyright (c) 2015, 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 24package compiler.compilercontrol.share.scenario; 25 26import compiler.compilercontrol.share.method.MethodDescriptor; 27import jdk.test.lib.Utils; 28 29import java.util.List; 30import java.util.Random; 31import java.util.stream.Collectors; 32import java.util.stream.Stream; 33 34/** 35 * Generates random commands 36 */ 37public class CommandGenerator { 38 private static final int MAX_COMMANDS = Integer.getInteger( 39 "compiler.compilercontrol.share.scenario.CommandGenerator.commands", 40 100); 41 private static final Random RANDOM = Utils.getRandomInstance(); 42 43 /** 44 * Generates random command 45 * 46 * @return command 47 */ 48 public Command generateCommand() { 49 return Utils.getRandomElement(Command.values()); 50 } 51 52 /** 53 * Generates random number of random command 54 * 55 * @return a list of random commands 56 */ 57 public List<Command> generateCommands() { 58 int amount = 1 + RANDOM.nextInt(MAX_COMMANDS - 1); 59 return generateCommands(amount); 60 } 61 62 /** 63 * Generates specified amount of random command 64 * 65 * @param amount amount of commands to generate 66 * @return a list of random commands 67 */ 68 public List<Command> generateCommands(int amount) { 69 return Stream.generate(this::generateCommand) 70 .limit(amount) 71 .collect(Collectors.toList()); 72 } 73 74 /** 75 * Generates random compile command {@link CompileCommand} with specified 76 * command and method descriptor 77 * 78 * @param command a command type 79 * @param md a method descriptor 80 * @param type a type of the command, or null to generate any 81 * @return the generated compile command 82 */ 83 public CompileCommand generateCompileCommand(Command command, 84 MethodDescriptor md, Scenario.Type type) { 85 if (type == null) { 86 type = Utils.getRandomElement(Scenario.Type.values()); 87 } 88 return type.createCompileCommand(command, md, generateCompiler()); 89 } 90 91 /** 92 * Generates type of compiler that should be used for the command, or null 93 * if any or all compilers should be used 94 * 95 * @return Compiler value, or null 96 */ 97 public Scenario.Compiler generateCompiler() { 98 Scenario.Compiler[] compilers = Scenario.Compiler.values(); 99 int compiler = RANDOM.nextInt(compilers.length + 1) - 1; 100 return (compiler != -1) ? compilers[compiler] : null; 101 } 102 103 /** 104 * Generates random diagnostic command 105 * {@link Scenario.Type} 106 */ 107 public Scenario.JcmdType generateJcmdType() { 108 return Utils.getRandomElement(Scenario.JcmdType.values()); 109 } 110} 111