刘光辉
119 分钟以前 06f314545ac3514d7ce28939920866ae8188d31c
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
package jnpf.elnFormula;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
 
public final class FormulaValue {
 
    private static final FormulaValue NULL = new FormulaValue(FormulaType.NULL, null, null);
 
    private final FormulaType type;
    private final Object value;
    private final String missingSlotCode;
 
    private FormulaValue(FormulaType type, Object value) {
        this(type, value, null);
    }
 
    private FormulaValue(FormulaType type, Object value, String missingSlotCode) {
        this.type = type;
        this.value = value;
        this.missingSlotCode = missingSlotCode;
    }
 
    public static FormulaValue decimal(String value) {
        if (value == null) {
            throw new IllegalArgumentException("decimal value must not be null");
        }
        return decimal(new BigDecimal(value));
    }
 
    public static FormulaValue decimal(BigDecimal value) {
        if (value == null) {
            throw new IllegalArgumentException("decimal value must not be null");
        }
        return new FormulaValue(FormulaType.DECIMAL, value);
    }
 
    public static FormulaValue decimalList(List<FormulaValue> values) {
        if (values == null) {
            throw new IllegalArgumentException("decimal list must not be null");
        }
        List<FormulaValue> copy = new ArrayList<>(values.size());
        for (FormulaValue value : values) {
            if (value == null || value.type != FormulaType.DECIMAL) {
                throw new IllegalArgumentException("decimal list accepts DECIMAL values only");
            }
            copy.add(value);
        }
        return new FormulaValue(FormulaType.DECIMAL_LIST,
                Collections.unmodifiableList(copy));
    }
 
    public static FormulaValue decimalList(FormulaValue... values) {
        if (values == null) {
            throw new IllegalArgumentException("decimal list must not be null");
        }
        List<FormulaValue> items = new ArrayList<>(values.length);
        Collections.addAll(items, values);
        return decimalList(items);
    }
 
    public static FormulaValue text(String value) {
        if (value == null) {
            throw new IllegalArgumentException("text value must not be null");
        }
        return new FormulaValue(FormulaType.TEXT, value);
    }
 
    public static FormulaValue bool(boolean value) {
        return new FormulaValue(FormulaType.BOOLEAN, value);
    }
 
    public static FormulaValue nullValue() {
        return NULL;
    }
 
    public static FormulaValue nullValue(String missingSlotCode) {
        if (missingSlotCode == null || missingSlotCode.isEmpty()) {
            throw new IllegalArgumentException("missing slot code must not be empty");
        }
        return new FormulaValue(FormulaType.NULL, null, missingSlotCode);
    }
 
    public FormulaType getType() {
        return type;
    }
 
    public String getMissingSlotCode() {
        return missingSlotCode;
    }
 
    public BigDecimal getDecimal() {
        require(FormulaType.DECIMAL);
        return (BigDecimal) value;
    }
 
    @SuppressWarnings("unchecked")
    public List<FormulaValue> getDecimalList() {
        require(FormulaType.DECIMAL_LIST);
        return (List<FormulaValue>) value;
    }
 
    public String getText() {
        require(FormulaType.TEXT);
        return (String) value;
    }
 
    public boolean getBoolean() {
        require(FormulaType.BOOLEAN);
        return (Boolean) value;
    }
 
    public boolean isNull() {
        return type == FormulaType.NULL;
    }
 
    public String toPlainString() {
        if (type == FormulaType.NULL) {
            return "null";
        }
        if (type == FormulaType.TEXT) {
            return getText();
        }
        if (type == FormulaType.BOOLEAN) {
            return Boolean.toString(getBoolean());
        }
        if (type == FormulaType.DECIMAL) {
            BigDecimal decimal = getDecimal();
            return decimal.signum() == 0 ? "0" : decimal.stripTrailingZeros().toPlainString();
        }
        StringBuilder result = new StringBuilder("[");
        List<FormulaValue> items = getDecimalList();
        for (int i = 0; i < items.size(); i++) {
            if (i > 0) {
                result.append(',');
            }
            result.append(items.get(i).toPlainString());
        }
        return result.append(']').toString();
    }
 
    private void require(FormulaType expected) {
        if (type != expected) {
            throw new IllegalStateException("expected " + expected + " but was " + type);
        }
    }
 
    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof FormulaValue)) {
            return false;
        }
        FormulaValue that = (FormulaValue) other;
        if (type != that.type) {
            return false;
        }
        if (type == FormulaType.DECIMAL) {
            return getDecimal().compareTo(that.getDecimal()) == 0;
        }
        return Objects.equals(value, that.value);
    }
 
    @Override
    public int hashCode() {
        Object hashValue = type == FormulaType.DECIMAL
                ? getDecimal().stripTrailingZeros() : value;
        return Objects.hash(type, hashValue);
    }
 
    @Override
    public String toString() {
        return toPlainString();
    }
}