Begun making the flag values... generic, and really abstract.

This commit is contained in:
Sauilitired 2014-11-17 19:18:05 +01:00
parent 6997f8675b
commit 69b8f6080b
2 changed files with 77 additions and 3 deletions

View File

@ -32,6 +32,11 @@ import org.apache.commons.lang.StringUtils;
public class AbstractFlag { public class AbstractFlag {
private final String key; private final String key;
private final FlagValue<?> value;
public AbstractFlag(final String key) {
this(key, new FlagValue.StringValue());
}
/** /**
* AbstractFlag is a parameter used in creating a new Flag * AbstractFlag is a parameter used in creating a new Flag
@ -39,7 +44,7 @@ public class AbstractFlag {
* @param key The key must be alphabetical characters and <= 16 characters * @param key The key must be alphabetical characters and <= 16 characters
* in length * in length
*/ */
public AbstractFlag(final String key) { public AbstractFlag(final String key, final FlagValue<?> value) {
if (!StringUtils.isAlpha(key.replaceAll("_", "").replaceAll("-", ""))) { if (!StringUtils.isAlpha(key.replaceAll("_", "").replaceAll("-", ""))) {
throw new IllegalArgumentException("Flag must be alphabetic characters"); throw new IllegalArgumentException("Flag must be alphabetic characters");
} }
@ -47,14 +52,15 @@ public class AbstractFlag {
throw new IllegalArgumentException("Key must be <= 16 characters"); throw new IllegalArgumentException("Key must be <= 16 characters");
} }
this.key = key.toLowerCase(); this.key = key.toLowerCase();
this.value = new FlagValue.StringValue();
} }
public String parseValue(final String value) { public String parseValue(final String value) {
return value; return this.value.parse(value).toString();
} }
public String getValueDesc() { public String getValueDesc() {
return "Flag value must be alphanumeric"; return this.value.getDescription();
} }
/** /**

View File

@ -0,0 +1,68 @@
package com.intellectualcrafters.plot.flag;
/**
* Created 2014-11-17 for PlotSquared
*
* @author Citymonstret
*/
public abstract class FlagValue<T> {
private Class<T> clazz;
public FlagValue() {
this.clazz = (Class<T>) this.getClass();
}
public FlagValue(Class<T> clazz) {
if (clazz == null) {
throw new NullPointerException();
}
this.clazz = clazz;
}
public boolean validValue(Object value) {
return value != null && value.getClass() == clazz;
}
public abstract T getValue(String t);
public abstract String parse(String t);
public abstract String getDescription();
public static class BooleanValue extends FlagValue<Boolean> {
@Override
public Boolean getValue(String t) {
return null;
}
@Override
public String parse(String t) {
return null;
}
@Override
public String getDescription() {
return null;
}
}
public static class StringValue extends FlagValue<String> {
@Override
public String parse(String s) {
return s;
}
@Override
public String getDescription() {
return "Flag value must be alphanumeric";
}
@Override
public String getValue(String t) {
return t;
}
}
}