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

111 lines
4.1 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
2014-11-16 10:48:18 +01:00
package com.intellectualcrafters.plot.flag;
2014-09-23 19:02:17 +02:00
import org.apache.commons.lang.StringUtils;
2014-09-23 19:02:17 +02:00
/**
2014-11-16 09:56:42 +01:00
* Created 2014-09-23 for PlotSquared
*
* @author Citymonstret
* @author Empire92
2014-09-23 19:02:17 +02:00
*/
public class AbstractFlag {
2015-01-29 10:59:32 +01:00
public final String key;
public final FlagValue<?> value;
public AbstractFlag(final String key) {
this(key, new FlagValue.StringValue());
}
2014-11-05 04:42:08 +01:00
/**
* AbstractFlag is a parameter used in creating a new Flag<br>
* The key must be alphabetical characters and <= 16 characters in length
* @param key
2014-11-05 04:42:08 +01:00
*/
public AbstractFlag(final String key, final FlagValue<?> value) {
2014-11-05 04:42:08 +01:00
if (!StringUtils.isAlpha(key.replaceAll("_", "").replaceAll("-", ""))) {
throw new IllegalArgumentException("Flag must be alphabetic characters");
}
if (key.length() > 16) {
throw new IllegalArgumentException("Key must be <= 16 characters");
}
this.key = key.toLowerCase();
if (value == null) {
this.value = new FlagValue.StringValue();
2014-12-18 03:15:11 +01:00
} else {
this.value = value;
}
2014-11-05 04:42:08 +01:00
}
2015-01-29 10:59:32 +01:00
public boolean isList() {
return this.value instanceof FlagValue.ListValue;
}
2014-11-05 04:42:08 +01:00
public Object parseValueRaw(final String value) {
try {
return this.value.parse(value);
}
catch (Exception e) {
return null;
}
2014-11-05 04:42:08 +01:00
}
public String toString(final Object t) {
return this.value.toString(t);
}
2014-11-05 04:42:08 +01:00
public String getValueDesc() {
return this.value.getDescription();
2014-11-05 04:42:08 +01:00
}
/**
* AbstractFlag key
*
* @return String
*/
public String getKey() {
return this.key;
}
@Override
public String toString() {
return this.key;
}
@Override
public boolean equals(final Object other) {
if (other == null) {
return false;
}
if (other == this) {
return true;
}
if (!(other instanceof AbstractFlag)) {
return false;
}
final AbstractFlag otherObj = (AbstractFlag) other;
return (otherObj.key.equals(this.key));
}
2014-09-23 19:02:17 +02:00
}