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