package game.command; import com.google.common.base.Predicate; public class StringParser extends DefaultingParser { private final boolean allowEmpty; private final Integer minLength; private final Integer maxLength; private final Predicate validator; public StringParser(String name, String def, boolean allowEmpty, Integer minLength, Integer maxLength, Predicate validator, Object ... completions) { super(name, def, completions); this.allowEmpty = allowEmpty; this.minLength = minLength; this.maxLength = maxLength; this.validator = validator; } public String parse(ScriptEnvironment env, String input) { if(!this.allowEmpty && input.isEmpty()) throw new ScriptException("Die Zeichenkette darf nicht leer sein"); if(this.minLength != null && input.length() < this.minLength) if(this.maxLength != null) throw new ScriptException("Die Zeichenkette muss zwischen %d .. %d Zeichen lang sein, habe %d ('%s')", this.minLength, this.maxLength, input.length(), input); else throw new ScriptException("Die Zeichenkette muss mindestens %d Zeichen lang sein, habe %d ('%s')", this.minLength, input.length(), input); if(this.maxLength != null && input.length() > this.maxLength) if(this.minLength != null) throw new ScriptException("Die Zeichenkette muss zwischen %d .. %d Zeichen lang sein, habe %d ('%s')", this.minLength, this.maxLength, input.length(), input); else throw new ScriptException("Die Zeichenkette darf höchstens %d Zeichen lang sein, habe %d ('%s')", this.maxLength, input.length(), input); return input; } }