summaryrefslogtreecommitdiffstats
path: root/EssentialsGroupManager/src/org/anjocaido/groupmanager/data
diff options
context:
space:
mode:
authorIaccidentally <coryhuckaby@gmail.com>2013-01-11 19:59:22 -0500
committerIaccidentally <coryhuckaby@gmail.com>2013-01-11 19:59:22 -0500
commiteb7cedd6fd5dae2353bdd47b2ce09942460fd8a6 (patch)
tree4c8a85944a28daeffd50b47b8646aaa71b10e71d /EssentialsGroupManager/src/org/anjocaido/groupmanager/data
parent859ca6e9199b2fe3ce652d3cb56a76559c2b4aca (diff)
downloadEssentials-eb7cedd6fd5dae2353bdd47b2ce09942460fd8a6.tar
Essentials-eb7cedd6fd5dae2353bdd47b2ce09942460fd8a6.tar.gz
Essentials-eb7cedd6fd5dae2353bdd47b2ce09942460fd8a6.tar.lz
Essentials-eb7cedd6fd5dae2353bdd47b2ce09942460fd8a6.tar.xz
Essentials-eb7cedd6fd5dae2353bdd47b2ce09942460fd8a6.zip
Revert "Remove GM from 3.0"
This reverts commit a4c93fef05493e6210e8d3d72af7b6d492f4e121.
Diffstat (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/data')
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java181
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Group.java187
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/data/GroupVariables.java94
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/data/User.java273
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/data/UserVariables.java53
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Variables.java208
6 files changed, 996 insertions, 0 deletions
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java
new file mode 100644
index 000000000..bb04fa3d7
--- /dev/null
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java
@@ -0,0 +1,181 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.anjocaido.groupmanager.data;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.anjocaido.groupmanager.GroupManager;
+import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
+import org.anjocaido.groupmanager.utils.StringPermissionComparator;
+
+/**
+ *
+ * @author gabrielcouto
+ */
+public abstract class DataUnit {
+
+ private WorldDataHolder dataSource;
+ private String name;
+ private boolean changed, sorted = false;
+ private ArrayList<String> permissions = new ArrayList<String>();
+
+ public DataUnit(WorldDataHolder dataSource, String name) {
+
+ this.dataSource = dataSource;
+ this.name = name;
+ }
+
+ public DataUnit(String name) {
+
+ this.name = name;
+ }
+
+ /**
+ * Every group is matched only by their names and DataSources names.
+ *
+ * @param o
+ * @return true if they are equal. false if not.
+ */
+ @Override
+ public boolean equals(Object o) {
+
+ if (o instanceof DataUnit) {
+ DataUnit go = (DataUnit) o;
+ if (this.getName().equalsIgnoreCase(go.getName())) {
+ // Global Group match.
+ if (this.dataSource == null && go.getDataSource() == null)
+ return true;
+ // This is a global group, the object to test isn't.
+ if (this.dataSource == null && go.getDataSource() != null)
+ return false;
+ // This is not a global group, but the object to test is.
+ if (this.dataSource != null && go.getDataSource() == null)
+ return false;
+ // Match on group name and world name.
+ if (this.dataSource.getName().equalsIgnoreCase(go.getDataSource().getName()))
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+
+ int hash = 5;
+ hash = 71 * hash + (this.name != null ? this.name.toLowerCase().hashCode() : 0);
+ return hash;
+ }
+
+ /**
+ * Set the data source to point to a different worldDataHolder
+ *
+ * @param source
+ */
+ public void setDataSource(WorldDataHolder source) {
+
+ this.dataSource = source;
+ }
+
+ /**
+ * Get the current worldDataHolder this object is pointing to
+ *
+ * @return the dataSource
+ */
+ public WorldDataHolder getDataSource() {
+
+ return dataSource;
+ }
+
+ /**
+ * @return the name
+ */
+ public String getName() {
+
+ return name;
+ }
+
+ public void flagAsChanged() {
+
+ WorldDataHolder testSource = getDataSource();
+ String source = "";
+
+ if (testSource == null)
+ source = "GlobalGroups";
+ else
+ source = testSource.getName();
+
+ GroupManager.logger.finest("DataSource: " + source + " - DataUnit: " + getName() + " flagged as changed!");
+ // for(StackTraceElement st: Thread.currentThread().getStackTrace()){
+ // GroupManager.logger.finest(st.toString());
+ // }
+ sorted = false;
+ changed = true;
+ }
+
+ public boolean isChanged() {
+
+ return changed;
+ }
+
+ public void flagAsSaved() {
+
+ WorldDataHolder testSource = getDataSource();
+ String source = "";
+
+ if (testSource == null)
+ source = "GlobalGroups";
+ else
+ source = testSource.getName();
+
+ GroupManager.logger.finest("DataSource: " + source + " - DataUnit: " + getName() + " flagged as saved!");
+ changed = false;
+ }
+
+ public boolean hasSamePermissionNode(String permission) {
+
+ return permissions.contains(permission);
+ }
+
+ public void addPermission(String permission) {
+
+ if (!hasSamePermissionNode(permission)) {
+ permissions.add(permission);
+ }
+ flagAsChanged();
+ }
+
+ public boolean removePermission(String permission) {
+
+ flagAsChanged();
+ return permissions.remove(permission);
+ }
+
+ /**
+ * Use this only to list permissions.
+ * You can't edit the permissions using the returned ArrayList instance
+ *
+ * @return a copy of the permission list
+ */
+ public List<String> getPermissionList() {
+
+ return Collections.unmodifiableList(permissions);
+ }
+
+ public boolean isSorted() {
+
+ return this.sorted;
+ }
+
+ public void sortPermissions() {
+
+ if (!isSorted()) {
+ Collections.sort(permissions, StringPermissionComparator.getInstance());
+ sorted = true;
+ }
+ }
+} \ No newline at end of file
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Group.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Group.java
new file mode 100644
index 000000000..751dc8fd6
--- /dev/null
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Group.java
@@ -0,0 +1,187 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.anjocaido.groupmanager.data;
+
+import org.anjocaido.groupmanager.GroupManager;
+import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
+import org.anjocaido.groupmanager.events.GMGroupEvent.Action;
+import org.anjocaido.groupmanager.events.GroupManagerEventHandler;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *
+ * @author gabrielcouto/ElgarL
+ */
+public class Group extends DataUnit implements Cloneable {
+
+ /**
+ * The group it inherits DIRECTLY!
+ */
+ private ArrayList<String> inherits = new ArrayList<String>();
+ /**
+ * This one holds the fields in INFO node.
+ * like prefix = 'c'
+ * or build = false
+ */
+ private GroupVariables variables = new GroupVariables(this);
+
+ /**
+ * Constructor for individual World Groups.
+ *
+ * @param name
+ */
+ public Group(WorldDataHolder source, String name) {
+
+ super(source, name);
+ }
+
+ /**
+ * Constructor for Global Groups.
+ *
+ * @param name
+ */
+ public Group(String name) {
+
+ super(name);
+ }
+
+ /**
+ * Is this a GlobalGroup
+ *
+ * @return true if this is a global group
+ */
+ public boolean isGlobal() {
+
+ return (getDataSource() == null);
+ }
+
+ /**
+ * Clone this group
+ *
+ * @return a clone of this group
+ */
+ @Override
+ public Group clone() {
+
+ Group clone;
+
+ if (isGlobal()) {
+ clone = new Group(this.getName());
+ } else {
+ clone = new Group(getDataSource(), this.getName());
+ clone.inherits = new ArrayList<String>(this.getInherits());
+ }
+
+ for (String perm : this.getPermissionList()) {
+ clone.addPermission(perm);
+ }
+ clone.variables = ((GroupVariables) variables).clone(clone);
+ //clone.flagAsChanged();
+ return clone;
+ }
+
+ /**
+ * Use this to deliver a group from a different dataSource to another
+ *
+ * @param dataSource
+ * @return Null or Clone
+ */
+ public Group clone(WorldDataHolder dataSource) {
+
+ if (dataSource.groupExists(this.getName())) {
+ return null;
+ }
+
+ Group clone = dataSource.createGroup(this.getName());
+
+ // Don't add inheritance for GlobalGroups
+ if (!isGlobal()) {
+ clone.inherits = new ArrayList<String>(this.getInherits());
+ }
+ for (String perm : this.getPermissionList()) {
+ clone.addPermission(perm);
+ }
+ clone.variables = variables.clone(clone);
+ clone.flagAsChanged(); //use this to make the new dataSource save the new group
+ return clone;
+ }
+
+ /**
+ * an unmodifiable list of inherits list
+ * You can't manage the list by here
+ * Lol... version 0.6 had a problem because this.
+ *
+ * @return the inherits
+ */
+ public List<String> getInherits() {
+
+ return Collections.unmodifiableList(inherits);
+ }
+
+ /**
+ * @param inherit the inherits to set
+ */
+ public void addInherits(Group inherit) {
+
+ if (!isGlobal()) {
+ if (!this.getDataSource().groupExists(inherit.getName())) {
+ getDataSource().addGroup(inherit);
+ }
+ if (!inherits.contains(inherit.getName().toLowerCase())) {
+ inherits.add(inherit.getName().toLowerCase());
+ }
+ flagAsChanged();
+ if (GroupManager.isLoaded()) {
+ GroupManager.BukkitPermissions.updateAllPlayers();
+ GroupManagerEventHandler.callEvent(this, Action.GROUP_INHERITANCE_CHANGED);
+ }
+ }
+ }
+
+ public boolean removeInherits(String inherit) {
+
+ if (!isGlobal()) {
+ if (this.inherits.contains(inherit.toLowerCase())) {
+ this.inherits.remove(inherit.toLowerCase());
+ flagAsChanged();
+ GroupManagerEventHandler.callEvent(this, Action.GROUP_INHERITANCE_CHANGED);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * @return the variables
+ */
+ public GroupVariables getVariables() {
+
+ return variables;
+ }
+
+ /**
+ *
+ * @param varList
+ */
+ public void setVariables(Map<String, Object> varList) {
+
+ if (!isGlobal()) {
+ GroupVariables temp = new GroupVariables(this, varList);
+ variables.clearVars();
+ for (String key : temp.getVarKeyList()) {
+ variables.addVar(key, temp.getVarObject(key));
+ }
+ flagAsChanged();
+ if (GroupManager.isLoaded()) {
+ GroupManager.BukkitPermissions.updateAllPlayers();
+ GroupManagerEventHandler.callEvent(this, Action.GROUP_INFO_CHANGED);
+ }
+ }
+ }
+}
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/GroupVariables.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/GroupVariables.java
new file mode 100644
index 000000000..e08d1db7d
--- /dev/null
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/GroupVariables.java
@@ -0,0 +1,94 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.anjocaido.groupmanager.data;
+
+import java.util.Map;
+
+/**
+ *
+ * @author gabrielcouto
+ */
+public class GroupVariables extends Variables implements Cloneable {
+
+ private Group owner;
+
+ public GroupVariables(Group owner) {
+
+ super(owner);
+ this.owner = owner;
+ addVar("prefix", "");
+ addVar("suffix", "");
+ addVar("build", false);
+ }
+
+ public GroupVariables(Group owner, Map<String, Object> varList) {
+
+ super(owner);
+ variables = varList;
+ if (variables.get("prefix") == null) {
+ variables.put("prefix", "");
+ owner.flagAsChanged();
+ }
+ //thisGrp.prefix = infoNode.get("prefix").toString();
+
+ if (variables.get("suffix") == null) {
+ variables.put("suffix", "");
+ owner.flagAsChanged();
+ }
+ //thisGrp.suffix = infoNode.get("suffix").toString();
+
+ if (variables.get("build") == null) {
+ variables.put("build", false);
+ owner.flagAsChanged();
+ }
+ this.owner = owner;
+ }
+
+ /**
+ * A clone of all vars here.
+ *
+ * @return GroupVariables clone
+ */
+ protected GroupVariables clone(Group newOwner) {
+
+ GroupVariables clone = new GroupVariables(newOwner);
+ for (String key : variables.keySet()) {
+ clone.variables.put(key, variables.get(key));
+ }
+ newOwner.flagAsChanged();
+ return clone;
+ }
+
+ /**
+ * Remove a var from the list
+ *
+ * @param name
+ */
+ @Override
+ public void removeVar(String name) {
+
+ try {
+ this.variables.remove(name);
+ } catch (Exception e) {
+ }
+ if (name.equals("prefix")) {
+ addVar("prefix", "");
+ } else if (name.equals("suffix")) {
+ addVar("suffix", "");
+ } else if (name.equals("build")) {
+ addVar("build", false);
+ }
+ owner.flagAsChanged();
+ }
+
+ /**
+ * @return the owner
+ */
+ @Override
+ public Group getOwner() {
+
+ return owner;
+ }
+}
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/User.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/User.java
new file mode 100644
index 000000000..6c74c2e50
--- /dev/null
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/User.java
@@ -0,0 +1,273 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.anjocaido.groupmanager.data;
+
+//import com.sun.org.apache.bcel.internal.generic.AALOAD;
+import java.util.ArrayList;
+
+import org.anjocaido.groupmanager.GroupManager;
+import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
+import org.anjocaido.groupmanager.events.GMUserEvent.Action;
+import org.anjocaido.groupmanager.events.GroupManagerEventHandler;
+
+import java.util.Map;
+import org.bukkit.Bukkit;
+import org.bukkit.entity.Player;
+
+/**
+ *
+ * @author gabrielcouto/ElgarL
+ */
+public class User extends DataUnit implements Cloneable {
+
+ /**
+ *
+ */
+ private String group = null;
+ private ArrayList<String> subGroups = new ArrayList<String>();
+ /**
+ * This one holds the fields in INFO node. like prefix = 'c' or build =
+ * false
+ */
+ private UserVariables variables = new UserVariables(this);
+ private transient Player bukkitPlayer = null;
+
+ /**
+ *
+ * @param name
+ */
+ public User(WorldDataHolder source, String name) {
+
+ super(source, name);
+ this.group = source.getDefaultGroup().getName();
+ }
+
+ /**
+ *
+ * @return User clone
+ */
+ @Override
+ public User clone() {
+
+ User clone = new User(getDataSource(), this.getName());
+ clone.group = this.group;
+ for (String perm : this.getPermissionList()) {
+ clone.addPermission(perm);
+ }
+ // clone.variables = this.variables.clone();
+ // clone.flagAsChanged();
+ return clone;
+ }
+
+ /**
+ * Use this to deliver a user from one WorldDataHolder to another
+ *
+ * @param dataSource
+ * @return null if given dataSource already contains the same user
+ */
+ public User clone(WorldDataHolder dataSource) {
+
+ if (dataSource.isUserDeclared(this.getName())) {
+ return null;
+ }
+ User clone = dataSource.createUser(this.getName());
+ if (dataSource.getGroup(group) == null) {
+ clone.setGroup(dataSource.getDefaultGroup());
+ } else {
+ clone.setGroup(dataSource.getGroup(this.getGroupName()));
+ }
+ for (String perm : this.getPermissionList()) {
+ clone.addPermission(perm);
+ }
+ clone.variables = this.variables.clone(this);
+ clone.flagAsChanged();
+ return clone;
+ }
+
+ public Group getGroup() {
+
+ Group result = getDataSource().getGroup(group);
+ if (result == null) {
+ this.setGroup(getDataSource().getDefaultGroup());
+ result = getDataSource().getDefaultGroup();
+ }
+ return result;
+ }
+
+ /**
+ * @return the group
+ */
+ public String getGroupName() {
+
+ Group result = getDataSource().getGroup(group);
+ if (result == null) {
+ group = getDataSource().getDefaultGroup().getName();
+ }
+ return group;
+ }
+
+
+ /**
+ * @param group
+ * the group to set
+ */
+ public void setGroup(Group group) {
+
+ setGroup(group, true);
+ }
+
+ /**
+ * @param group the group to set
+ * @param updatePerms if we are to trigger a superperms update.
+ *
+ */
+ public void setGroup(Group group, Boolean updatePerms) {
+
+ if (!this.getDataSource().groupExists(group.getName())) {
+ getDataSource().addGroup(group);
+ }
+ group = getDataSource().getGroup(group.getName());
+ String oldGroup = this.group;
+ this.group = group.getName();
+ flagAsChanged();
+ if (GroupManager.isLoaded()) {
+ if (!GroupManager.BukkitPermissions.isPlayer_join() && (updatePerms))
+ GroupManager.BukkitPermissions.updatePlayer(getBukkitPlayer());
+
+ // Do we notify of the group change?
+ String defaultGroupName = getDataSource().getDefaultGroup().getName();
+ // if we were not in the default group
+ // or we were in the default group and the move is to a different
+ // group.
+ boolean notify = (!oldGroup.equalsIgnoreCase(defaultGroupName)) || ((oldGroup.equalsIgnoreCase(defaultGroupName)) && (!this.group.equalsIgnoreCase(defaultGroupName)));
+
+ if (notify)
+ GroupManager.notify(this.getName(), String.format(" moved to the group %s.", group.getName()));
+
+ GroupManagerEventHandler.callEvent(this, Action.USER_GROUP_CHANGED);
+ }
+ }
+
+ public boolean addSubGroup(Group subGroup) {
+
+ // Don't allow adding a subgroup if it's already set as the primary.
+ if (this.group.equalsIgnoreCase(subGroup.getName())) {
+ return false;
+ }
+ // User already has this subgroup
+ if (containsSubGroup(subGroup))
+ return false;
+
+ // If the group doesn't exists add it
+ if (!this.getDataSource().groupExists(subGroup.getName())) {
+ getDataSource().addGroup(subGroup);
+ }
+
+ subGroups.add(subGroup.getName());
+ flagAsChanged();
+ if (GroupManager.isLoaded()) {
+ if (!GroupManager.BukkitPermissions.isPlayer_join())
+ GroupManager.BukkitPermissions.updatePlayer(getBukkitPlayer());
+ GroupManagerEventHandler.callEvent(this, Action.USER_SUBGROUP_CHANGED);
+ }
+ return true;
+
+ //subGroup = getDataSource().getGroup(subGroup.getName());
+ //removeSubGroup(subGroup);
+ //subGroups.add(subGroup.getName());
+ }
+
+ public int subGroupsSize() {
+
+ return subGroups.size();
+ }
+
+ public boolean isSubGroupsEmpty() {
+
+ return subGroups.isEmpty();
+ }
+
+ public boolean containsSubGroup(Group subGroup) {
+
+ return subGroups.contains(subGroup.getName());
+ }
+
+ public boolean removeSubGroup(Group subGroup) {
+
+ try {
+ if (subGroups.remove(subGroup.getName())) {
+ flagAsChanged();
+ if (GroupManager.isLoaded())
+ if (!GroupManager.BukkitPermissions.isPlayer_join())
+ GroupManager.BukkitPermissions.updatePlayer(getBukkitPlayer());
+ GroupManagerEventHandler.callEvent(this, Action.USER_SUBGROUP_CHANGED);
+ return true;
+ }
+ } catch (Exception e) {
+ }
+ return false;
+ }
+
+ public ArrayList<Group> subGroupListCopy() {
+
+ ArrayList<Group> val = new ArrayList<Group>();
+ for (String gstr : subGroups) {
+ Group g = getDataSource().getGroup(gstr);
+ if (g == null) {
+ removeSubGroup(g);
+ continue;
+ }
+ val.add(g);
+ }
+ return val;
+ }
+
+ public ArrayList<String> subGroupListStringCopy() {
+
+ return new ArrayList<String>(subGroups);
+ }
+
+ /**
+ * @return the variables
+ */
+ public UserVariables getVariables() {
+
+ return variables;
+ }
+
+ /**
+ *
+ * @param varList
+ */
+ public void setVariables(Map<String, Object> varList) {
+
+ //UserVariables temp = new UserVariables(this, varList);
+ variables.clearVars();
+ for (String key : varList.keySet()) {
+ variables.addVar(key, varList.get(key));
+ }
+ flagAsChanged();
+ if (GroupManager.isLoaded()) {
+ //if (!GroupManager.BukkitPermissions.isPlayer_join())
+ // GroupManager.BukkitPermissions.updatePlayer(this.getName());
+ GroupManagerEventHandler.callEvent(this, Action.USER_INFO_CHANGED);
+ }
+ }
+
+
+ public User updatePlayer(Player player) {
+
+ bukkitPlayer = player;
+ return this;
+ }
+
+ public Player getBukkitPlayer() {
+
+ if (bukkitPlayer == null) {
+ bukkitPlayer = Bukkit.getPlayer(this.getName());
+ }
+ return bukkitPlayer;
+ }
+}
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/UserVariables.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/UserVariables.java
new file mode 100644
index 000000000..f994595c1
--- /dev/null
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/UserVariables.java
@@ -0,0 +1,53 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.anjocaido.groupmanager.data;
+
+import java.util.Map;
+
+/**
+ *
+ * @author gabrielcouto
+ */
+public class UserVariables extends Variables {
+
+ private User owner;
+
+ public UserVariables(User owner) {
+
+ super(owner);
+ this.owner = owner;
+ }
+
+ public UserVariables(User owner, Map<String, Object> varList) {
+
+ super(owner);
+ this.variables = varList;
+ this.owner = owner;
+ }
+
+ /**
+ * A clone of all vars here.
+ *
+ * @return UserVariables clone
+ */
+ protected UserVariables clone(User newOwner) {
+
+ UserVariables clone = new UserVariables(newOwner);
+ for (String key : variables.keySet()) {
+ clone.variables.put(key, variables.get(key));
+ }
+ newOwner.flagAsChanged();
+ return clone;
+ }
+
+ /**
+ * @return the owner
+ */
+ @Override
+ public User getOwner() {
+
+ return owner;
+ }
+}
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Variables.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Variables.java
new file mode 100644
index 000000000..42ceba7e4
--- /dev/null
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Variables.java
@@ -0,0 +1,208 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.anjocaido.groupmanager.data;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * 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 DataUnit owner;
+ protected Map<String, Object> variables = new HashMap<String, Object>();
+
+ public Variables(DataUnit owner) {
+
+ this.owner = owner;
+ }
+
+ /**
+ * 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(String name, Object o) {
+
+ if (o == null) {
+ return;
+ }
+ if (variables.containsKey(name)) {
+ variables.remove(name);
+ }
+ variables.put(name, o);
+ owner.flagAsChanged();
+ }
+
+ /**
+ * Returns the object inside the var
+ *
+ * @param name
+ * @return a Object if exists. null if doesn't exists
+ */
+ public Object getVarObject(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(String name) {
+
+ Object o = variables.get(name);
+ try {
+ return o == null ? "" : o.toString();
+ } catch (Exception e) {
+ return "";
+ }
+ }
+
+ /**
+ *
+ * @param name
+ * @return false if null. or a Boolean.parseBoolean of the string
+ */
+ public Boolean getVarBoolean(String name) {
+
+ Object o = variables.get(name);
+ try {
+ return o == null ? false : Boolean.parseBoolean(o.toString());
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ *
+ * @param name
+ * @return -1 if null. or a parseInt of the string
+ */
+ public Integer getVarInteger(String name) {
+
+ Object o = variables.get(name);
+ try {
+ return o == null ? -1 : Integer.parseInt(o.toString());
+ } catch (Exception e) {
+ return -1;
+ }
+ }
+
+ /**
+ *
+ * @param name
+ * @return -1 if null. or a parseDouble of the string
+ */
+ public Double getVarDouble(String name) {
+
+ Object o = variables.get(name);
+ try {
+ return o == null ? -1.0D : Double.parseDouble(o.toString());
+ } catch (Exception e) {
+ return -1.0D;
+ }
+ }
+
+ /**
+ * All variable keys this is holding
+ *
+ * @return Set of all variable names.
+ */
+ public Set<String> getVarKeyList() {
+
+ return variables.keySet();
+ }
+
+ /**
+ * verify is a var exists
+ *
+ * @param name the key name of the var
+ * @return true if that var exists
+ */
+ public boolean hasVar(String name) {
+
+ return variables.containsKey(name);
+ }
+
+ /**
+ * Returns the quantity of vars this is holding
+ *
+ * @return the number of vars
+ */
+ public int getSize() {
+
+ return variables.size();
+ }
+
+ /**
+ * Remove a var from the list
+ *
+ * @param name
+ */
+ public void removeVar(String name) {
+
+ try {
+ variables.remove(name);
+ } catch (Exception e) {
+ }
+ owner.flagAsChanged();
+ }
+
+ public static Object parseVariableValue(String value) {
+
+ try {
+ Integer i = Integer.parseInt(value);
+ return i;
+ } catch (NumberFormatException e) {
+ }
+ try {
+ Double d = Double.parseDouble(value);
+ return d;
+ } catch (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;
+
+ }
+
+ public void clearVars() {
+
+ variables.clear();
+ owner.flagAsChanged();
+ }
+
+ /**
+ * @return the owner
+ */
+ public DataUnit getOwner() {
+
+ return owner;
+ }
+
+ public boolean isEmpty() {
+
+ return variables.isEmpty();
+ }
+}