001 package org.bukkit.conversations;
002
003 import org.apache.commons.lang.StringUtils;
004
005 import java.util.Arrays;
006 import java.util.List;
007
008 /**
009 * FixedSetPrompt is the base class for any prompt that requires a fixed set
010 * response from the user.
011 */
012 public abstract class FixedSetPrompt extends ValidatingPrompt {
013
014 protected List<String> fixedSet;
015
016 /**
017 * Creates a FixedSetPrompt from a set of strings.
018 * <p>
019 * foo = new FixedSetPrompt("bar", "cheese", "panda");
020 *
021 * @param fixedSet A fixed set of strings, one of which the user must
022 * type.
023 */
024 public FixedSetPrompt(String... fixedSet) {
025 super();
026 this.fixedSet = Arrays.asList(fixedSet);
027 }
028
029 private FixedSetPrompt() {}
030
031 @Override
032 protected boolean isInputValid(ConversationContext context, String input) {
033 return fixedSet.contains(input);
034 }
035
036 /**
037 * Utility function to create a formatted string containing all the
038 * options declared in the constructor.
039 *
040 * @return the options formatted like "[bar, cheese, panda]" if bar,
041 * cheese, and panda were the options used
042 */
043 protected String formatFixedSet() {
044 return "[" + StringUtils.join(fixedSet, ", ") + "]";
045 }
046 }