Newer
Older
ultramine_bukkit / src / main / java / org / bukkit / conversations / FixedSetPrompt.java
@vlad20012 vlad20012 on 24 Feb 2017 1 KB initial
package org.bukkit.conversations;

import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * FixedSetPrompt is the base class for any prompt that requires a fixed set
 * response from the user.
 */
public abstract class FixedSetPrompt extends ValidatingPrompt
{

	protected List<String> fixedSet;

	/**
	 * Creates a FixedSetPrompt from a set of strings.
	 * <p>
	 * foo = new FixedSetPrompt("bar", "cheese", "panda");
	 *
	 * @param fixedSet A fixed set of strings, one of which the user must
	 *                 type.
	 */
	public FixedSetPrompt(String... fixedSet)
	{
		super();
		this.fixedSet = Arrays.asList(fixedSet);
	}

	private FixedSetPrompt()
	{
	}

	@Override
	protected boolean isInputValid(ConversationContext context, String input)
	{
		return fixedSet.contains(input);
	}

	/**
	 * Utility function to create a formatted string containing all the
	 * options declared in the constructor.
	 *
	 * @return the options formatted like "[bar, cheese, panda]" if bar,
	 * cheese, and panda were the options used
	 */
	protected String formatFixedSet()
	{
		return "[" + StringUtils.join(fixedSet, ", ") + "]";
	}
}