GroupManager/src/main/java/org/anjocaido/groupmanager/data/Variables.java

198 lines
4.8 KiB
Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.anjocaido.groupmanager.data;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* A class that holds variables of a user/group.
* In groups, it holds the contents of INFO node.
* Like:
* prefix
* suffix
* build
*
* @author gabrielcouto
*/
public abstract class Variables implements Cloneable {
private final DataUnit owner;
protected final Map<String, Object> variables = Collections.synchronizedMap(new HashMap<String, Object>());
public Variables(final DataUnit owner) {
this.owner = owner;
}
public static Object parseVariableValue(final String value) {
try {
final Integer i = Integer.parseInt(value);
return i;
} catch (final NumberFormatException e) {
}
try {
final Double d = Double.parseDouble(value);
return d;
} catch (final NumberFormatException e) {
}
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("on")) {
return true;
} else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no") || value.equalsIgnoreCase("off")) {
return false;
}
return value;
}
/**
* Add var to the the INFO node.
* examples:
* addVar("build",true);
* addVar("prefix","c");
*
* @param name
* key name of the var
* @param o
* the object value of the var
*/
public void addVar(final String name, final Object o) {
if (o == null) {
return;
}
if (variables.containsKey(name)) {
variables.remove(name);
}
variables.put(name, o);
owner.flagAsChanged();
}
public void clearVars() {
variables.clear();
owner.flagAsChanged();
}
/**
* @return the owner
*/
public DataUnit getOwner() {
return owner;
}
/**
* Returns the quantity of vars this is holding
*
* @return the number of vars
*/
public int getSize() {
return variables.size();
}
/**
*
* @param name
* @return false if null. or a Boolean.parseBoolean of the string
*/
public Boolean getVarBoolean(final String name) {
final Object o = variables.get(name);
try {
return o == null ? false : Boolean.parseBoolean(o.toString());
} catch (final Exception e) {
return false;
}
}
/**
*
* @param name
* @return -1 if null. or a parseDouble of the string
*/
public Double getVarDouble(final String name) {
final Object o = variables.get(name);
try {
return o == null ? -1.0D : Double.parseDouble(o.toString());
} catch (final Exception e) {
return -1.0D;
}
}
/**
*
* @param name
* @return -1 if null. or a parseInt of the string
*/
public Integer getVarInteger(final String name) {
final Object o = variables.get(name);
try {
return o == null ? -1 : Integer.parseInt(o.toString());
} catch (final Exception e) {
return -1;
}
}
/**
* All variable keys this is holding
*
* @return Set of all variable names.
*/
public String[] getVarKeyList() {
synchronized (variables) {
return variables.keySet().toArray(new String[0]);
}
}
/**
* Returns the object inside the var
*
* @param name
* @return a Object if exists. null if doesn't exists
*/
public Object getVarObject(final String name) {
return variables.get(name);
}
/**
* Get the String value for the given var name
*
* @param name
* the var key name
* @return "" if null. or the toString() value of object
*/
public String getVarString(final String name) {
final Object o = variables.get(name);
try {
return o == null ? "" : o.toString();
} catch (final Exception e) {
return "";
}
}
/**
* verify is a var exists
*
* @param name
* the key name of the var
* @return true if that var exists
*/
public boolean hasVar(final String name) {
return variables.containsKey(name);
}
public boolean isEmpty() {
return variables.isEmpty();
}
/**
* Remove a var from the list
*
* @param name
*/
public void removeVar(final String name) {
try {
variables.remove(name);
} catch (final Exception e) {
}
owner.flagAsChanged();
}
}