shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
package com.walker.infrastructure.arguments.support;
 
import com.walker.infrastructure.arguments.ArgumentsException;
import com.walker.infrastructure.arguments.Variable;
import com.walker.infrastructure.arguments.VariableType;
import com.walker.infrastructure.utils.StringUtils;
 
public class DefaultVariable implements Variable {
 
    private String id;
    private String description = StringUtils.EMPTY_STRING;
    private VariableType type;
    
    private String stringValue;
    private int integerValue = 0;
    private boolean booleanValue = false;
    private float floatValue = 0F;
    private long longValue = 0;
    private double doubleValue = 0.0;
    
    private Object defaultValue = null;
    
    public DefaultVariable(){
        
    }
    
    public DefaultVariable(VariableType type, Object value, Object defaultValue){
        assert (type != null);
        assert (value != null);
        this.type = type;
        this.defaultValue = defaultValue;
        setValue(value);
    }
    
    public DefaultVariable setType(VariableType type) {
        this.type = type;
        return this;
    }
 
    public DefaultVariable setDefaultValue(Object defaultValue) {
        this.defaultValue = defaultValue;
        return this;
    }
 
    public DefaultVariable setValue(Object value){
        assert (type != null);
        if(type == VariableType.Integer){
            integerValue = Integer.parseInt(value.toString());
        } else if(type == VariableType.Boolean){
            booleanValue = Boolean.valueOf(value.toString());
        } else if(type == VariableType.Float){
            floatValue = Float.parseFloat(value.toString());
        } else if(type == VariableType.String){
            stringValue = value.toString();
        } else if(type == VariableType.Long){
            longValue = Long.parseLong(value.toString());
        } else if(type == VariableType.Double){
            doubleValue = Double.parseDouble(value.toString());
        } else
            throw new IllegalArgumentException("unsupported primitive type in value: " + value);
        // 默认情况:默认值与设置值一样
//        this.defaultValue = value;
        return this;
        
//        Class<?> dataClazz = value.getClass();
//        if(dataClazz.isPrimitive()){
//        } else {
//            if(dataClazz == String.class){
//                stringValue = value.toString();
//            } else if(dataClazz == Integer.class){
//                integerValue = (Integer)value;
//            } else if(dataClazz == Boolean.class){
//                booleanValue = (Boolean)value;
//            } else if(dataClazz == Float.class){
//                floatValue = (Float)value;
//            } else
//                throw new IllegalArgumentException("unsupported primitive type: " + dataClazz);
//        }
    }
    
    public DefaultVariable setId(String id) {
        assert (StringUtils.isNotEmpty(id));
        this.id = id;
        return this;
    }
 
    public DefaultVariable setDescription(String description) {
        this.description = description;
        return this;
    }
 
    @Override
    public String getId() {
        return id;
    }
 
    @Override
    public String getDescription() {
        return this.description;
    }
 
    @Override
    public VariableType getType() {
        return type;
    }
 
    @Override
    public String getStringValue() {
        if(type != VariableType.String){
            throw new ArgumentsException("error invoke in getStringValue(), type: " + type);
        }
        return this.stringValue;
    }
 
    @Override
    public int getIntegerValue() {
        if(type != VariableType.Integer){
            throw new ArgumentsException("error invoke in getStringValue(), type: " + type);
        }
        return this.integerValue;
    }
 
    @Override
    public boolean getBooleanValue() {
        if(type != VariableType.Boolean){
            throw new ArgumentsException("error invoke in getStringValue(), type: " + type);
        }
        return this.booleanValue;
    }
 
    @Override
    public float getFloatValue() {
        if(type != VariableType.Float){
            throw new ArgumentsException("error invoke in getStringValue(), type: " + type);
        }
        return this.floatValue;
    }
 
    @Override
    public Object getDefaultValue() {
        if(defaultValue == null)
            return null;
        if(type == VariableType.String){
            return this.defaultValue.toString();
        } else if(type == VariableType.Integer){
            return Integer.parseInt(defaultValue.toString());
        } else if(type == VariableType.Boolean){
            return Boolean.valueOf(defaultValue.toString());
        } else if(type == VariableType.Float){
            return Float.parseFloat(defaultValue.toString());
        } else if(type == VariableType.Long){
            return Long.parseLong(defaultValue.toString());
        } else
            throw new IllegalArgumentException("unsupported defaultValue type: " + defaultValue.getClass());
    }
 
    public String toString(){
        return new StringBuilder().append("{")
                .append("id = ").append(id)
                .append(", desc = ").append(description)
                .append(", type = ").append(type)
                .append("}").toString();
    }
    
    public boolean equals(Object o){
        if(o == null) return false;
        if(o == this) return true;
        if(o instanceof DefaultVariable){
            DefaultVariable dv = (DefaultVariable)o;
            if(dv.id.equals(this.id) && dv.description.equals(this.description) 
                    && dv.type == this.type){
                return true;
            }
        }
        return false;
    }
    
    public int hashCode(){
        return 31 + 31*this.id.hashCode() + this.description.hashCode() + type.hashCode();
    }
 
    @Override
    public long getLongValue() {
        return this.longValue;
    }
 
    @Override
    public double getDoubleValue() {
        return this.doubleValue;
    }
}