TF-PlotSquared/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/Flag.java

126 lines
4.6 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;
import org.apache.commons.lang.StringUtils;
public class Flag {
2015-01-29 18:45:14 +11:00
private AbstractFlag key;
private Object value;
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
/**
2014-12-17 20:15:11 -06:00
* Flag object used to store basic information for a Plot. Flags are a key/value pair. For a flag to be usable by a
* player, you need to register it with PlotSquared.
2014-11-05 14:42:08 +11:00
*
2014-12-17 20:15:11 -06:00
* @param key AbstractFlag
2015-03-13 18:26:08 +11:00
* @param value Value must be alphanumerical (can have spaces) and be <= 48 characters
2014-12-17 20:15:11 -06:00
*
* @throws IllegalArgumentException if you provide inadequate inputs
2014-11-05 14:42:08 +11:00
*/
public Flag(final AbstractFlag key, final String value) {
2015-05-04 19:52:37 +10:00
if (!StringUtils.isAsciiPrintable(value)) {
throw new IllegalArgumentException("Flag must be ascii");
2014-11-05 14:42:08 +11:00
}
2015-04-11 21:34:00 +10:00
if (value.length() > 128) {
throw new IllegalArgumentException("Value must be <= 128 characters");
2014-11-05 14:42:08 +11:00
}
this.key = key;
this.value = key.parseValueRaw(value);
2014-11-05 14:42:08 +11:00
if (this.value == null) {
throw new IllegalArgumentException(key.getValueDesc());
}
}
2015-02-23 12:32:27 +11:00
2015-02-20 17:34:19 +11:00
public void setKey(final AbstractFlag key) {
2015-01-29 18:45:14 +11:00
this.key = key;
if (this.value instanceof String) {
this.value = key.parseValueRaw((String) this.value);
}
}
2015-02-23 12:32:27 +11:00
/**
* Warning: Unchecked
*/
public Flag(final AbstractFlag key, final Object value) {
2015-02-20 17:34:19 +11:00
this.key = key;
this.value = value;
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
/**
* Get the AbstractFlag used in creating the flag
*
* @return AbstractFlag
*/
public AbstractFlag getAbstractFlag() {
return this.key;
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
/**
* Get the key for the AbstractFlag
*
* @return String
*/
public String getKey() {
return this.key.getKey();
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
/**
* Get the value
*
* @return String
*/
public Object getValue() {
2014-11-05 14:42:08 +11:00
return this.value;
}
2015-02-23 12:32:27 +11:00
public String getValueString() {
2015-02-20 17:34:19 +11:00
return this.key.toString(this.value);
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public String toString() {
if (this.value.equals("")) {
return this.key.getKey();
}
return this.key + ":" + getValueString();
2014-11-05 14:42:08 +11:00
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Flag other = (Flag) obj;
return (this.key.getKey().equals(other.key.getKey()) && this.value.equals(other.value));
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public int hashCode() {
return this.key.getKey().hashCode();
}
}