Skip to content

Commit cbc7205

Browse files
committed
更新 true false ,减少类对象
1 parent 2a01ec5 commit cbc7205

File tree

2 files changed

+155
-87
lines changed

2 files changed

+155
-87
lines changed

csharp/ToolGood.Algorithm2/Operand.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public static Operand Create(string obj)
169169
}
170170
return new OperandString(obj);
171171
}
172-
172+
173173
/// <summary>
174174
/// 创建操作数
175175
/// </summary>
@@ -328,13 +328,13 @@ public Operand ToBoolean(string errorMessage = null)
328328
{
329329
if (Type == OperandType.BOOLEAN) { return this; }
330330
if (IsError) { return this; }
331-
if (Type == OperandType.NUMBER) { return Create(NumberValue != 0); }
332-
if (Type == OperandType.DATE) { return Create(((double)DateValue) != 0); }
331+
if (Type == OperandType.NUMBER) { return NumberValue != 0 ? True : False; }
332+
if (Type == OperandType.DATE) { return ((double)DateValue) != 0 ? True : False; }
333333
if (Type == OperandType.STRING) {
334-
if (TextValue.Equals("true", StringComparison.OrdinalIgnoreCase)) { return Create(true); }
335-
if (TextValue.Equals("false", StringComparison.OrdinalIgnoreCase)) { return Create(false); }
336-
if (TextValue.Equals("1", StringComparison.OrdinalIgnoreCase)) { return Create(true); }
337-
if (TextValue.Equals("0", StringComparison.OrdinalIgnoreCase)) { return Create(false); }
334+
if (TextValue.Equals("true", StringComparison.OrdinalIgnoreCase)) { return True; }
335+
if (TextValue.Equals("false", StringComparison.OrdinalIgnoreCase)) { return False; }
336+
if (TextValue.Equals("1", StringComparison.OrdinalIgnoreCase)) { return True; }
337+
if (TextValue.Equals("0", StringComparison.OrdinalIgnoreCase)) { return False; }
338338
}
339339
return Error(errorMessage);
340340
}

java/toolgood.algorithm/src/main/java/toolgood/algorithm/Operand.java

Lines changed: 148 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -15,103 +15,153 @@ public abstract class Operand {
1515
public final static Operand True = Create(true);
1616
public final static Operand False = Create(false);
1717

18-
public boolean IsNull() {return false;}
19-
public boolean IsError() {return false;}
20-
21-
public String ErrorMsg() {return null;}
22-
public OperandType Type() {return OperandType.ERROR;}
23-
public double NumberValue() {return 0.0;}
24-
public int IntValue(){return 0;}
25-
public String TextValue() {return null;}
26-
public boolean BooleanValue() {return false;}
27-
public List<Operand> ArrayValue() {return null;}
28-
public JsonData JsonValue() {return null;}
29-
public MyDate DateValue() {return null;}
18+
public boolean IsNull() {
19+
return false;
20+
}
21+
22+
public boolean IsError() {
23+
return false;
24+
}
25+
26+
public String ErrorMsg() {
27+
return null;
28+
}
29+
30+
public OperandType Type() {
31+
return OperandType.ERROR;
32+
}
33+
34+
public double NumberValue() {
35+
return 0.0;
36+
}
37+
38+
public int IntValue() {
39+
return 0;
40+
}
41+
42+
public String TextValue() {
43+
return null;
44+
}
45+
46+
public boolean BooleanValue() {
47+
return false;
48+
}
49+
50+
public List<Operand> ArrayValue() {
51+
return null;
52+
}
53+
54+
public JsonData JsonValue() {
55+
return null;
56+
}
57+
58+
public MyDate DateValue() {
59+
return null;
60+
}
3061

3162
public static Operand Create(final boolean obj) {
3263
return new OperandBoolean(obj);
3364
}
65+
3466
public static Operand Create(final short obj) {
35-
return new OperandNumber( (double)obj);
67+
return new OperandNumber((double) obj);
3668
}
69+
3770
public static Operand Create(final int obj) {
38-
return new OperandNumber( (double)obj);
71+
return new OperandNumber((double) obj);
3972
}
73+
4074
public static Operand Create(final long obj) {
4175
return new OperandNumber((double) obj);
4276
}
77+
4378
public static Operand Create(final float obj) {
4479
return new OperandNumber((double) obj);
4580
}
81+
4682
public static Operand Create(final double obj) {
4783
return new OperandNumber(obj);
4884
}
85+
4986
public static Operand Create(final BigDecimal obj) {
5087
return new OperandNumber(obj.doubleValue());
5188
}
89+
5290
public static Operand Create(final String obj) {
5391
if (obj.equals(null)) {
5492
return CreateNull();
5593
}
5694
return new OperandString(obj);
5795
}
58-
public static Operand CreateJson(final String txt)
59-
{
60-
if ((txt.startsWith("{") && txt.endsWith("}")) || (txt.startsWith("[") && txt.endsWith("]")))
61-
{
62-
try
63-
{
64-
JsonData json = (JsonData)JsonMapper.ToObject(txt);
96+
97+
public static Operand CreateJson(final String txt) {
98+
if ((txt.startsWith("{") && txt.endsWith("}")) || (txt.startsWith("[") && txt.endsWith("]"))) {
99+
try {
100+
JsonData json = (JsonData) JsonMapper.ToObject(txt);
65101
return Create(json);
102+
} catch (final Exception e) {
66103
}
67-
catch (final Exception e) { }
68104
}
69105
return Error("string to json is error!");
70106
}
107+
71108
public static Operand Create(final MyDate obj) {
72109
return new OperandDate(obj);
73110
}
111+
74112
public static Operand Create(final DateTime obj) {
75113
return new OperandDate(new MyDate(obj));
76114
}
115+
77116
public static Operand Create(final Date obj) {
78117
return new OperandDate(new MyDate(obj));
79118
}
119+
80120
// public static Operand Create(final DateTime obj) {
81-
// return new OperandDate(new Date(obj));
121+
// return new OperandDate(new Date(obj));
82122
// }
83123
// public static Operand Create(final TimeSpan obj) {
84-
// return new OperandDate(new Date(obj));
124+
// return new OperandDate(new Date(obj));
85125
// }
86126
public static Operand Create(final JsonData obj) {
87127
return new OperandJson(obj);
88128
}
129+
89130
public static Operand Create(List<Operand> obj) {
90131
return new OperandArray(obj);
91132
}
92-
133+
93134
public static Operand Error(final String msg) {
94135
return new OperandError(msg);
95136
}
137+
96138
public static Operand CreateNull() {
97139
return new OperandNull();
98140
}
99-
public Operand ToNumber(final String errorMessage )
100-
{
101-
if (Type() == OperandType.NUMBER) { return this; }
102-
if (IsError()) { return this; }
103-
if (Type() == OperandType.BOOLEAN) { return Create(BooleanValue() ? 1.0 : 0.0); }
104-
if (Type() == OperandType.DATE) { return Create((double) DateValue().ToNumber()); }
105-
if (Type() == OperandType.STRING)
106-
{
141+
142+
public Operand ToNumber(final String errorMessage) {
143+
if (Type() == OperandType.NUMBER) {
144+
return this;
145+
}
146+
if (IsError()) {
147+
return this;
148+
}
149+
if (Type() == OperandType.BOOLEAN) {
150+
return Create(BooleanValue() ? 1.0 : 0.0);
151+
}
152+
if (Type() == OperandType.DATE) {
153+
return Create((double) DateValue().ToNumber());
154+
}
155+
if (Type() == OperandType.STRING) {
107156
try {
108-
Double d= Double.parseDouble(TextValue());
157+
Double d = Double.parseDouble(TextValue());
109158
return Create(d);
110159
} catch (Exception e) {
111160
}
112161
}
113162
return Error(errorMessage);
114163
}
164+
115165
public Operand ToBoolean(final String errorMessage) {
116166
if (Type() == OperandType.BOOLEAN) {
117167
return this;
@@ -120,86 +170,104 @@ public Operand ToBoolean(final String errorMessage) {
120170
return this;
121171
}
122172
if (Type() == OperandType.NUMBER) {
123-
return Create(NumberValue() != 0);
173+
return (NumberValue() != 0) ? True : False;
124174
}
125175
if (Type() == OperandType.DATE) {
126-
return Create(((double) DateValue().ToNumber()) != 0);
176+
return (((double) DateValue().ToNumber()) != 0) ? True : False;
127177
}
128178
if (Type() == OperandType.STRING) {
129179
if (TextValue().toLowerCase().equals("true")) {
130-
return Create(true);
180+
return True;
131181
}
132182
if (TextValue().toLowerCase().equals("false")) {
133-
return Create(false);
183+
return False;
134184
}
135185
if (TextValue().equals("1")) {
136-
return Create(true);
186+
return True;
137187
}
138188
if (TextValue().equals("0")) {
139-
return Create(false);
189+
return False;
140190
}
141191
}
142192
return Error(errorMessage);
143193
}
144-
public Operand ToText(final String errorMessage )
145-
{
146-
if (Type() == OperandType.STRING) { return this; }
147-
if (IsError()) { return this; }
194+
195+
public Operand ToText(final String errorMessage) {
196+
if (Type() == OperandType.STRING) {
197+
return this;
198+
}
199+
if (IsError()) {
200+
return this;
201+
}
148202
if (Type() == OperandType.NUMBER) {
149-
String str=((Double)NumberValue()).toString();
150-
if(str.contains(".")){
151-
str= Pattern.compile("(\\.)?0+$").matcher(str).replaceAll("");
203+
String str = ((Double) NumberValue()).toString();
204+
if (str.contains(".")) {
205+
str = Pattern.compile("(\\.)?0+$").matcher(str).replaceAll("");
152206
}
153-
return Create(str);
207+
return Create(str);
208+
}
209+
if (Type() == OperandType.BOOLEAN) {
210+
return Create(BooleanValue() ? "TRUE" : "FALSE");
211+
}
212+
if (Type() == OperandType.DATE) {
213+
return Create(DateValue().toString());
154214
}
155-
if (Type() == OperandType.BOOLEAN) { return Create(BooleanValue() ? "TRUE" : "FALSE"); }
156-
if (Type() == OperandType.DATE) { return Create(DateValue().toString()); }
157215

158216
return Error(errorMessage);
159217
}
160-
public Operand ToDate(final String errorMessage )
161-
{
162-
if (Type() == OperandType.DATE) { return this; }
163-
if (IsError()) { return this; }
164-
if (Type() == OperandType.NUMBER) { return Create(new MyDate(NumberValue())); }
165-
if (Type() == OperandType.STRING)
166-
{
167-
MyDate date=MyDate.parse(TextValue());
168-
if (date != null){
218+
219+
public Operand ToDate(final String errorMessage) {
220+
if (Type() == OperandType.DATE) {
221+
return this;
222+
}
223+
if (IsError()) {
224+
return this;
225+
}
226+
if (Type() == OperandType.NUMBER) {
227+
return Create(new MyDate(NumberValue()));
228+
}
229+
if (Type() == OperandType.STRING) {
230+
MyDate date = MyDate.parse(TextValue());
231+
if (date != null) {
169232
return Create(date);
170233
}
171-
// if (TimeSpan.TryParse(TextValue, cultureInfo, out TimeSpan t)) { return Create(new Date(t)); }
172-
// if (DateTime.TryParse(TextValue, cultureInfo, DateTimeStyles.None, out DateTime d)) { return Create(new Date(d)); }
234+
// if (TimeSpan.TryParse(TextValue, cultureInfo, out TimeSpan t)) { return
235+
// Create(new Date(t)); }
236+
// if (DateTime.TryParse(TextValue, cultureInfo, DateTimeStyles.None, out
237+
// DateTime d)) { return Create(new Date(d)); }
173238
}
174239
return Error(errorMessage);
175240
}
176-
public Operand ToJson(final String errorMessage )
177-
{
178-
if (Type() == OperandType.JSON) { return this; }
179-
if (IsError()) { return this; }
180-
if (Type() == OperandType.STRING)
181-
{
241+
242+
public Operand ToJson(final String errorMessage) {
243+
if (Type() == OperandType.JSON) {
244+
return this;
245+
}
246+
if (IsError()) {
247+
return this;
248+
}
249+
if (Type() == OperandType.STRING) {
182250
final String txt = TextValue();
183-
if ((txt.startsWith("{") && txt.endsWith("}")) || (txt.startsWith("[") && txt.endsWith("]")))
184-
{
185-
try
186-
{
251+
if ((txt.startsWith("{") && txt.endsWith("}")) || (txt.startsWith("[") && txt.endsWith("]"))) {
252+
try {
187253
final JsonData json = JsonMapper.ToObject(txt);
188254
return Operand.Create(json);
255+
} catch (final Exception e) {
189256
}
190-
catch (final Exception e) { }
191257
}
192258
}
193259
return Error(errorMessage);
194260
}
195-
public Operand ToArray(final String errorMessage )
196-
{
197-
if (Type() == OperandType.ARRARY) { return this; }
198-
if (IsError()) { return this; }
199-
if (Type() == OperandType.JSON)
200-
{
201-
if (JsonValue().IsArray())
202-
{
261+
262+
public Operand ToArray(final String errorMessage) {
263+
if (Type() == OperandType.ARRARY) {
264+
return this;
265+
}
266+
if (IsError()) {
267+
return this;
268+
}
269+
if (Type() == OperandType.JSON) {
270+
if (JsonValue().IsArray()) {
203271
final List<Operand> list = new ArrayList<Operand>();
204272
for (JsonData v : JsonValue().inst_array) {
205273
if (v.IsString())

0 commit comments

Comments
 (0)