Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
FixedSetPrompt.java
1 package org.bukkit.conversations;
2 
3 import org.apache.commons.lang.StringUtils;
4 
5 import java.util.Arrays;
6 import java.util.List;
7 
8 /**
9  * FixedSetPrompt is the base class for any prompt that requires a fixed set
10  * response from the user.
11  */
12 public abstract class FixedSetPrompt extends ValidatingPrompt {
13 
14  protected List<String> fixedSet;
15 
16  /**
17  * Creates a FixedSetPrompt from a set of strings.
18  * <p>
19  * foo = new FixedSetPrompt("bar", "cheese", "panda");
20  *
21  * @param fixedSet A fixed set of strings, one of which the user must
22  * type.
23  */
24  public FixedSetPrompt(String... fixedSet) {
25  super();
26  this.fixedSet = Arrays.asList(fixedSet);
27  }
28 
29  private FixedSetPrompt() {}
30 
31  @Override
32  protected boolean isInputValid(ConversationContext context, String input) {
33  return fixedSet.contains(input);
34  }
35 
36  /**
37  * Utility function to create a formatted string containing all the
38  * options declared in the constructor.
39  *
40  * @return the options formatted like "[bar, cheese, panda]" if bar,
41  * cheese, and panda were the options used
42  */
43  protected String formatFixedSet() {
44  return "[" + StringUtils.join(fixedSet, ", ") + "]";
45  }
46 }