summaryrefslogtreecommitdiffstats
path: root/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Variables.java
blob: 547e6266d36d82d92ecd4ee194f79b6475bb788f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * 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
     */
    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();
    }
}