PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagValue.java

508 lines
14 KiB
Java
Raw Normal View History

package com.intellectualcrafters.plot.flag;
import java.util.ArrayList;
2015-07-18 13:10:38 +02:00
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.intellectualcrafters.plot.object.PlotBlock;
/**
* Created 2014-11-17 for PlotSquared
*
* @author Citymonstret
*/
public abstract class FlagValue<T> {
2014-12-16 06:03:20 +01:00
private final Class<T> clazz;
2015-02-23 02:32:27 +01:00
@SuppressWarnings("unchecked")
2015-02-20 07:34:19 +01:00
public FlagValue() {
2014-11-21 23:45:46 +01:00
this.clazz = (Class<T>) getClass();
}
2015-02-23 02:32:27 +01:00
2014-12-16 06:03:20 +01:00
public FlagValue(final Class<T> clazz) {
if (clazz == null) {
throw new NullPointerException();
}
this.clazz = clazz;
}
2015-02-23 02:32:27 +01:00
2014-12-16 06:03:20 +01:00
public boolean validValue(final Object value) {
return (value != null) && (value.getClass() == this.clazz);
}
2015-02-23 02:32:27 +01:00
2015-02-20 07:34:19 +01:00
public String toString(final Object t) {
return t.toString();
}
2015-02-23 02:32:27 +01:00
public abstract T getValue(Object t);
2015-02-23 02:32:27 +01:00
public abstract T parse(String t);
2015-02-23 02:32:27 +01:00
public abstract String getDescription();
2015-02-23 02:32:27 +01:00
public static class BooleanValue extends FlagValue<Boolean> {
@Override
public Boolean getValue(final Object t) {
return (Boolean) t;
}
2015-02-23 02:32:27 +01:00
@Override
public Boolean parse(final String t) {
2015-03-29 08:12:32 +02:00
switch (t.toLowerCase()) {
case "1":
case "yes":
case "allow":
case "true": {
return true;
}
case "0":
case "no":
case "deny":
case "false": {
return false;
}
default: {
return null;
}
2015-02-20 07:34:19 +01:00
}
}
2015-02-23 02:32:27 +01:00
@Override
public String getDescription() {
2015-02-20 07:34:19 +01:00
return "Flag value must be a boolean (true|false)";
}
}
2015-02-23 02:32:27 +01:00
public static class IntegerValue extends FlagValue<Integer> {
@Override
public Integer getValue(final Object t) {
return (Integer) t;
}
2015-02-23 02:32:27 +01:00
@Override
public Integer parse(final String t) {
2015-02-20 07:34:19 +01:00
try {
return Integer.parseInt(t);
} catch (final IllegalArgumentException e) {
return null;
}
}
2015-02-23 02:32:27 +01:00
@Override
public String getDescription() {
2015-02-20 07:34:19 +01:00
return "Flag value must be a whole number";
}
}
2015-02-23 02:32:27 +01:00
public static class IntervalValue extends FlagValue<Integer[]> {
2015-02-20 07:34:19 +01:00
@Override
public String toString(final Object t) {
final Integer[] value = ((Integer[]) t);
return value[0] + " " + value[1];
}
2015-02-23 02:32:27 +01:00
@Override
public Integer[] getValue(final Object t) {
return (Integer[]) t;
}
2015-02-23 02:32:27 +01:00
@Override
public Integer[] parse(final String t) {
2015-02-20 07:34:19 +01:00
int seconds;
int amount;
final String[] values = t.split(" ");
if (values.length < 2) {
seconds = 1;
try {
amount = Integer.parseInt(values[0]);
} catch (final Exception e) {
return null;
}
} else {
try {
amount = Integer.parseInt(values[0]);
seconds = Integer.parseInt(values[1]);
} catch (final Exception e) {
return null;
}
}
2015-02-20 07:34:19 +01:00
return new Integer[] { amount, seconds };
}
2015-02-23 02:32:27 +01:00
@Override
public String getDescription() {
2015-07-09 17:31:34 +02:00
return "Value(s) must be numeric. /plot set flag <flag> <interval> [amount]";
}
}
2015-02-23 02:32:27 +01:00
public static class UnsignedIntegerValue extends FlagValue<Integer> {
@Override
public Integer getValue(final Object t) {
return (Integer) t;
}
2015-02-23 02:32:27 +01:00
@Override
public Integer parse(final String t) {
2015-02-20 07:34:19 +01:00
try {
final int value = Integer.parseInt(t);
2015-03-20 13:10:37 +01:00
if (value < 0) {
2015-02-20 07:34:19 +01:00
return null;
}
return value;
} catch (final IllegalArgumentException e) {
return null;
}
}
2015-02-23 02:32:27 +01:00
@Override
public String getDescription() {
2015-02-20 07:34:19 +01:00
return "Flag value must be a positive whole number (includes 0)";
}
}
2015-02-23 02:32:27 +01:00
public static class DoubleValue extends FlagValue<Double> {
@Override
public Double getValue(final Object t) {
return (Double) t;
}
2015-02-23 02:32:27 +01:00
@Override
public Double parse(final String t) {
2015-02-20 07:34:19 +01:00
try {
return Double.parseDouble(t);
} catch (final IllegalArgumentException e) {
return null;
}
}
2015-02-23 02:32:27 +01:00
@Override
public String getDescription() {
2015-02-20 07:34:19 +01:00
return "Flag value must be a number (negative decimals are allowed)";
}
}
2015-02-23 02:32:27 +01:00
public static class LongValue extends FlagValue<Long> {
@Override
public Long getValue(final Object t) {
return (Long) t;
}
2015-02-23 02:32:27 +01:00
@Override
public Long parse(final String t) {
2015-02-20 07:34:19 +01:00
try {
return Long.parseLong(t);
} catch (final IllegalArgumentException e) {
return null;
}
}
2015-02-23 02:32:27 +01:00
@Override
public String getDescription() {
2015-02-20 07:34:19 +01:00
return "Flag value must be a whole number (large numbers allowed)";
}
}
2015-02-23 02:32:27 +01:00
public static class UnsignedLongValue extends FlagValue<Long> {
@Override
public Long getValue(final Object t) {
return (Long) t;
}
2015-02-23 02:32:27 +01:00
@Override
public Long parse(final String t) {
2015-02-20 07:34:19 +01:00
try {
final long value = Long.parseLong(t);
if (value < 0) {
return null;
}
return value;
} catch (final IllegalArgumentException e) {
return null;
}
}
2015-02-23 02:32:27 +01:00
@Override
public String getDescription() {
2015-02-20 07:34:19 +01:00
return "Flag value must be a positive whole number (large numbers allowed)";
}
}
2015-02-23 02:32:27 +01:00
public static class UnsignedDoubleValue extends FlagValue<Double> {
@Override
public Double getValue(final Object t) {
return (Double) t;
}
2015-02-23 02:32:27 +01:00
@Override
public Double parse(final String t) {
2015-02-20 07:34:19 +01:00
try {
final double value = Double.parseDouble(t);
if (value < 0) {
return null;
}
return value;
} catch (final IllegalArgumentException e) {
return null;
}
}
2015-02-23 02:32:27 +01:00
@Override
public String getDescription() {
2015-02-20 07:34:19 +01:00
return "Flag value must be a positive number (decimals allowed)";
}
}
2015-02-23 02:32:27 +01:00
public static class PlotBlockValue extends FlagValue<PlotBlock> {
@Override
public PlotBlock getValue(final Object t) {
return (PlotBlock) t;
}
2015-02-23 02:32:27 +01:00
@Override
public PlotBlock parse(final String t) {
2015-02-20 07:34:19 +01:00
try {
final String[] split = t.split(":");
byte data;
if (split.length == 2) {
if ("*".equals(split[1])) {
2015-02-11 03:35:15 +01:00
data = -1;
2015-02-20 07:34:19 +01:00
} else {
2015-02-11 03:35:15 +01:00
data = Byte.parseByte(split[1]);
}
2015-02-20 07:34:19 +01:00
} else {
data = -1;
}
final short id = Short.parseShort(split[0]);
return new PlotBlock(id, data);
} catch (final Exception e) {
return null;
}
}
2015-02-23 02:32:27 +01:00
@Override
public String getDescription() {
2015-02-20 07:34:19 +01:00
return "Flag value must be a number (negative decimals are allowed)";
}
}
2015-02-23 02:32:27 +01:00
2015-01-29 10:59:32 +01:00
public interface ListValue {
public void add(Object t, String value);
2015-02-23 02:32:27 +01:00
2015-01-29 10:59:32 +01:00
public void remove(Object t, String value);
}
2015-02-23 02:32:27 +01:00
2015-01-29 10:59:32 +01:00
public static class PlotBlockListValue extends FlagValue<HashSet<PlotBlock>> implements ListValue {
2015-02-20 07:34:19 +01:00
@SuppressWarnings("unchecked")
2015-01-29 10:59:32 +01:00
@Override
2015-02-20 07:34:19 +01:00
public String toString(final Object t) {
return StringUtils.join((HashSet<PlotBlock>) t, ",");
}
2015-02-23 02:32:27 +01:00
@SuppressWarnings("unchecked")
2015-02-20 07:34:19 +01:00
@Override
public HashSet<PlotBlock> getValue(final Object t) {
2015-02-20 07:34:19 +01:00
return (HashSet<PlotBlock>) t;
}
2015-02-23 02:32:27 +01:00
@Override
public HashSet<PlotBlock> parse(final String t) {
2015-02-20 07:34:19 +01:00
final HashSet<PlotBlock> list = new HashSet<PlotBlock>();
for (final String item : t.split(",")) {
final String[] split = item.split(":");
byte data;
if (split.length == 2) {
if ("*".equals(split[1])) {
data = -1;
} else {
data = Byte.parseByte(split[1]);
}
} else {
data = -1;
}
final short id = Short.parseShort(split[0]);
final PlotBlock block = new PlotBlock(id, data);
list.add(block);
}
return list;
}
2015-02-23 02:32:27 +01:00
@Override
public String getDescription() {
2015-02-20 07:34:19 +01:00
return "Flag value must be a block list";
}
2015-02-23 02:32:27 +01:00
2015-01-29 10:59:32 +01:00
@Override
2015-02-20 07:34:19 +01:00
public void add(final Object t, final String value) {
try {
2015-02-20 07:34:19 +01:00
((HashSet<PlotBlock>) t).addAll(parse(value));
} catch (final Exception e) {
}
2015-01-29 10:59:32 +01:00
}
2015-02-23 02:32:27 +01:00
2015-01-29 10:59:32 +01:00
@Override
2015-02-20 07:34:19 +01:00
public void remove(final Object t, final String value) {
try {
2015-02-20 07:34:19 +01:00
for (final PlotBlock item : parse(value)) {
((HashSet<PlotBlock>) t).remove(item);
}
2015-02-20 07:34:19 +01:00
} catch (final Exception e) {
2015-01-29 10:59:32 +01:00
}
}
}
public static class IntegerListValue extends FlagValue<List<Integer>> implements ListValue {
@SuppressWarnings("unchecked")
@Override
public String toString(final Object t) {
return StringUtils.join((List<Integer>) t, ",");
}
@SuppressWarnings("unchecked")
@Override
public List<Integer> getValue(final Object t) {
return (List<Integer>) t;
}
@Override
public List<Integer> parse(final String t) {
String[] split = (t.split(","));
ArrayList<Integer> numbers = new ArrayList<Integer>();
for (String element : split) {
numbers.add(Integer.parseInt(element));
}
return numbers;
}
@Override
public String getDescription() {
return "Flag value must be a integer list";
}
@Override
public void add(final Object t, final String value) {
try {
((List<Integer>) t).addAll(parse(value));
} catch (final Exception e) {
}
}
@Override
public void remove(final Object t, final String value) {
try {
for (final Integer item : parse(value)) {
((List<Integer>) t).remove(item);
}
} catch (final Exception e) {
}
}
}
2015-07-18 13:10:38 +02:00
@SuppressWarnings("ALL")
public static class StringListValue extends FlagValue<List<String>> implements ListValue {
@Override
public String toString(final Object t) {
return StringUtils.join((List<String>) t, ",");
}
@Override
public List<String> getValue(final Object t) {
return (List<String>) t;
}
@Override
public List<String> parse(final String t) {
return Arrays.asList(t.split(","));
}
@Override
public String getDescription() {
return "Flag value must be a string list";
}
@Override
public void add(final Object t, final String value) {
try {
((List<String>) t).addAll(parse(value));
} catch (final Exception ignored) {}
}
@Override
public void remove(final Object t, final String value) {
try {
for (final String item : parse(value)) {
((List<String>) t).remove(item);
}
} catch (final Exception e) {
}
}
}
public static class DoubleListValue extends FlagValue<List<Double>> implements ListValue {
@SuppressWarnings("unchecked")
@Override
public String toString(final Object t) {
return StringUtils.join((List<Double>) t, ",");
}
@SuppressWarnings("unchecked")
@Override
public List<Double> getValue(final Object t) {
return (List<Double>) t;
}
@Override
public List<Double> parse(final String t) {
String[] split = (t.split(","));
ArrayList<Double> numbers = new ArrayList<Double>();
for (String element : split) {
numbers.add(Double.parseDouble(element));
}
return numbers;
}
@Override
public String getDescription() {
return "Flag value must be a integer list";
}
@Override
public void add(final Object t, final String value) {
try {
((List<Double>) t).addAll(parse(value));
} catch (final Exception e) {
}
}
@Override
public void remove(final Object t, final String value) {
try {
for (final Double item : parse(value)) {
((List<Double>) t).remove(item);
}
} catch (final Exception e) {
}
}
}
2015-02-23 02:32:27 +01:00
public static class StringValue extends FlagValue<String> {
@Override
2014-12-16 06:03:20 +01:00
public String parse(final String s) {
return s;
}
2015-02-23 02:32:27 +01:00
@Override
public String getDescription() {
return "Flag value must be alphanumeric. Some special characters are allowed.";
}
2015-02-23 02:32:27 +01:00
@Override
public String getValue(final Object t) {
return t.toString();
}
}
}