hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package frame.variant;
 
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
 
import org.apache.log4j.Logger;
 
import frame.util.Util;
import frame.variant.translator.Translator;
 
public class Value {
 
    private Object data;
    private ValueType type;
    private static Logger logger;
    
    
    static {
        logger = Logger.getLogger(Value.class);
    }
    
    public Value() {
        data = null;
    }
    
    public Value(Boolean value) {
        setData(value);
    }
    
    public Value(Integer value) {
        setData(value);
    }
    
    public Value(Long value) {
        setData(value);
    }
    
    public Value(Double value) {
        setData(value);
    }
    
    public Value(BigDecimal value) {
        setData(value);
    }
    
    public Value(Date value) {
        setData(value);
    }
    
    public Value(String value) {
        setData(value);
    }
 
    public void setData(Boolean value) {
        this.data = value;
        type = ValueType.Int;        
    }
    
    public void setData(Integer value) {
        this.data = value;
        type = ValueType.Int;
    }
    
    public void setData(Long value) {
        this.data = value;
        type = ValueType.Long;
    }
 
    public void setData(Double value) {
        this.data = value;
        type = ValueType.Double;
    }
    
    public void setData(BigDecimal value) {
        this.data = value;
        type = ValueType.Decimal;
    }    
 
    public void setData(Date value) {
        this.data = value;
        type = ValueType.Date;
    }
 
    public void setData(String value) {
        if ("null".equalsIgnoreCase(value)) {
            this.data = null;
        }
        else {
            this.data = value;                
        }
    
        type = ValueType.String;        
    }
 
    public void setData(Object value, ValueType type) {
        this.data = value;
        this.type = type;        
    }
    
    public ValueType getType() {
        return type;
    }
 
    public Integer getInt() {
        Integer result = null;
        
        if (data != null) {
            Class<?> clazz = data.getClass();
            
            if (clazz == Integer.class) {
                result = (Integer)data;
            }
            else if (clazz == BigDecimal.class) {
                result = ((BigDecimal)data).intValue();                
            }
            else if (clazz == String.class) {
                result = Integer.parseInt((String)data);                    
            }
            else {
                String value_str = String.valueOf(data);
                result = Integer.parseInt(value_str);
            }
        }
        else {
            result = 0;
        }
        
        return result;
    }
    
    public int getInt(int defaultValue) {
        if (data == null) {
            return defaultValue;
        }
        
        Integer result = defaultValue;
        
        try {
            result = getInt();
            
            if (result == null) {
                return defaultValue;
            }
        }
        catch(Exception e) {
            return defaultValue;
        }
        
        return result;
    }    
    
    public Double getDouble() {
        if (type == ValueType.Int) {
            return Double.valueOf((Integer)data);
        }
        else {
            try {
                return (Double)data;                
            }
            catch(Exception e) {
                return Double.valueOf(String.valueOf(data));                
            }
        }
    }
    
    public BigDecimal getBigDecimal() {
        if (type == ValueType.Int) {
            return BigDecimal.valueOf((Integer)data);
        }
        else {
            try {
                return BigDecimal.valueOf((Double)data);                
            }
            catch(Exception e) {
                return BigDecimal.valueOf(Double.valueOf(String.valueOf(data)));            
            }
        }
    }
    
    public Long getLong() {
        return (Long) data;
    }
 
    public Date getDate() throws ParseException {
        Date result = null;
        
        if (ValueType.Date == type) {
            result = (Date)data;
        }
        else if (ValueType.String == type) {
            result = Util.StringToDate(data.toString());
        }
        else if (ValueType.Int == type) {
            logger.error("can not parse int to date");
        }
        else if (ValueType.Double == type) {
            logger.error("can not parse double to date");            
        }
        
        return result;
    }
    
    public java.sql.Timestamp getSqlDate() {
        return new java.sql.Timestamp(((Date)data).getTime());
    }    
 
    public String getString() {
        return Translator.toString(data, null);
    }
    
    public String getString(String defaultValue) {
        if (data != null) {
            if (ValueType.Date == type) {
                String result = data.toString();
                if (result.length() > 15) {
                    return result.substring(0, 16);
                }
            }
 
            return data.toString();
        }
        else {
            return defaultValue;
        }
    }
    
    public boolean getBoolean() {
        String str = getString();
        
        boolean result = "T".equalsIgnoreCase(str);
        result = result || "True".equalsIgnoreCase(str);
        result = result || "Y".equalsIgnoreCase(str);
        result = result || "Yes".equalsIgnoreCase(str);        
        
        return result;
    }
    
    public boolean getBoolean(boolean defaultValue) {
        String str = getString();
        
        if (Util.isEmptyStr(str)) {
            return defaultValue;
        }
        
        boolean result = "T".equalsIgnoreCase(str);
        result = result || "True".equalsIgnoreCase(str);
        result = result || "Y".equalsIgnoreCase(str);
        result = result || "Yes".equalsIgnoreCase(str);        
        
        return result;
    }
    
    public Object getData() {
        return data;
    }
 
    public boolean isNull() {
        return data == null;
    }
    
    public boolean isEmpty() {
        String str = getString();
        return Util.isEmptyStr(str);
    }
    
    public void setNull() {
        data = null;
    }    
    
    public void clear() {
        data = null;
    }
 
    public String toString() {
        return getString();
    }
    
}