-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathOperand.cs
More file actions
681 lines (605 loc) · 16.4 KB
/
Operand.cs
File metadata and controls
681 lines (605 loc) · 16.4 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
using System;
using System.Collections.Generic;
using ToolGood.Algorithm.Enums;
using ToolGood.Algorithm.LitJson;
using ToolGood.Algorithm.Operands;
namespace ToolGood.Algorithm
{
/// <summary>
/// 操作数
/// </summary>
public abstract class Operand
{
/// <summary>
/// 版本号
/// </summary>
public static readonly Operand Version = new OperandString("ToolGood.Algorithm 6.2");
/// <summary>
/// True
/// </summary>
public static readonly Operand True = new OperandBoolean(true);
/// <summary>
/// False
/// </summary>
public static readonly Operand False = new OperandBoolean(false);
/// <summary>
/// One
/// </summary>
public static readonly Operand One;
/// <summary>
/// Zero
/// </summary>
public static readonly Operand Zero;
/// <summary>
/// Null
/// </summary>
public static readonly Operand Null = new OperandNull();
/// <summary>
/// NONE
/// </summary>
public static readonly Operand None = new OperandNone();
// 整数缓存范围: -1000 ~ 1000,共2001个值
private const int IntCacheOffset = 1000;
private const int IntCacheSize = 2001;
private static readonly Operand[] IntCache = new Operand[IntCacheSize];
static Operand()
{
for (int i = 0; i < IntCacheSize; i++)
IntCache[i] = new OperandInt(i - IntCacheOffset);
One = Operand.Create(1);
Zero = Operand.Create(0);
}
#region IsNull IsNumber IsText IsBoolean IsArray IsDate IsJson IsArrayJson IsError ErrorMsg
internal virtual bool IsErrorOrNone => false;
/// <summary>
/// 是否未指定值
/// </summary>
public virtual bool IsNone => false;
/// <summary>
/// 是否为空值
/// </summary>
public virtual bool IsNull => false;
/// <summary>
/// 是否数字
/// </summary>
public virtual bool IsNumber => false;
/// <summary>
/// 是否字符串
/// </summary>
public virtual bool IsText => false;
/// <summary>
/// 是否布尔值
/// </summary>
public virtual bool IsBoolean => false;
/// <summary>
/// 是否数组
/// </summary>
public virtual bool IsArray => false;
/// <summary>
/// 是否日期
/// </summary>
public virtual bool IsDate => false;
/// <summary>
/// 是否Json对象
/// </summary>
public virtual bool IsJson => false;
/// <summary>
/// 是否Json数组
/// </summary>
public virtual bool IsArrayJson => false;
/// <summary>
/// 是否出错
/// </summary>
public virtual bool IsError => false;
/// <summary>
/// 错误信息
/// </summary>
public virtual string ErrorMsg => null;
#endregion
/// <summary>
/// 操作数类型
/// </summary>
public abstract OperandType Type { get; }
#region Value
/// <summary>
/// 数字值
/// </summary>
public virtual decimal NumberValue => throw new NotImplementedException();
/// <summary>
/// double值
/// </summary>
public virtual double DoubleValue => throw new NotImplementedException();
/// <summary>
/// int值
/// </summary>
public virtual int IntValue => throw new NotImplementedException();
/// <summary>
/// long值
/// </summary>
public virtual long LongValue => throw new NotImplementedException();
/// <summary>
/// 字符串值
/// </summary>
public virtual string TextValue => throw new NotImplementedException();
/// <summary>
/// 布尔值
/// </summary>
public virtual bool BooleanValue => throw new NotImplementedException();
/// <summary>
/// 数组值
/// </summary>
public virtual List<Operand> ArrayValue => throw new NotImplementedException();
internal virtual JsonData JsonValue => throw new NotImplementedException();
/// <summary>
/// 时间值
/// </summary>
public virtual MyDate DateValue => throw new NotImplementedException();
#endregion
#region Create
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(bool obj)
{
return obj ? True : False;
}
#region number
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(short obj)
{
return IntCache[obj + IntCacheOffset];
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(int obj)
{
if (obj >= -IntCacheOffset && obj <= IntCacheOffset)
return IntCache[obj + IntCacheOffset];
return new OperandInt(obj);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(long obj)
{
if (obj >= -IntCacheOffset && obj <= IntCacheOffset)
return IntCache[(int)obj + IntCacheOffset];
return new OperandDecimal((decimal)obj);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(ushort obj)
{
if (obj <= IntCacheOffset)
return IntCache[obj + IntCacheOffset];
return new OperandDecimal((decimal)obj);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(uint obj)
{
if (obj <= IntCacheOffset)
return IntCache[(int)obj + IntCacheOffset];
return new OperandDecimal((decimal)obj);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(ulong obj)
{
if (obj <= (ulong)IntCacheOffset)
return IntCache[(int)obj + IntCacheOffset];
return new OperandDecimal((decimal)obj);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(float obj)
{
if(obj == MathF.Truncate(obj) && obj >= -IntCacheOffset && obj <= IntCacheOffset) {
return IntCache[(int)obj + IntCacheOffset];
}
return new OperandDecimal((decimal)obj);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(double obj)
{
if(obj == Math.Truncate(obj) && obj >= -IntCacheOffset && obj <= IntCacheOffset) {
return IntCache[(int)obj + IntCacheOffset];
}
return new OperandDecimal((decimal)obj);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(decimal obj)
{
if(obj == decimal.Truncate(obj) && obj >= -IntCacheOffset && obj <= IntCacheOffset) {
return IntCache[(int)obj + IntCacheOffset];
}
return new OperandDecimal(obj);
}
#endregion number
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(string obj)
{
return obj is null ? Null : new OperandString(obj);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="txt"></param>
/// <returns></returns>
public static Operand CreateJson(string txt)
{
if((txt.StartsWith('{') && txt.EndsWith('}')) || (txt.StartsWith('[') && txt.EndsWith(']'))) {
try {
var json = JsonMapper.ToObject(txt);
return Operand.Create(json);
} catch(Exception) { }
}
return Operand.Error("Convert to json error!");
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(MyDate obj)
{
return new OperandMyDate(obj);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(DateTime obj)
{
return new OperandMyDate(new MyDate(obj));
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(TimeSpan obj)
{
return new OperandMyDate(new MyDate(obj));
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
internal static Operand Create(JsonData obj)
{
return new OperandJson(obj);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(List<Operand> obj)
{
return new OperandArray(obj);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(ICollection<string> obj)
{
var array = new List<Operand>(obj.Count);
foreach(var item in obj) {
array.Add(Create(item));
}
return new OperandArray(array);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(ICollection<double> obj)
{
var array = new List<Operand>(obj.Count);
foreach(var item in obj) {
array.Add(Create(item));
}
return new OperandArray(array);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(ICollection<int> obj)
{
var array = new List<Operand>(obj.Count);
foreach(var item in obj) {
array.Add(Create(item));
}
return new OperandArray(array);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Operand Create(ICollection<bool> obj)
{
var array = new List<Operand>(obj.Count);
foreach(var item in obj) {
array.Add(Create(item));
}
return new OperandArray(array);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public static Operand Error(string msg)
{
return new OperandError(msg);
}
/// <summary>
/// 创建操作数
/// </summary>
/// <param name="msg"></param>
/// <param name="args"></param>
/// <returns></returns>
public static Operand Error(string msg, params object[] args)
{
return new OperandError(msg, args);
}
#endregion Create
#region 转化类型
/// <summary>
/// 转数值类型
/// </summary>
/// <param name="errorMessage"></param>
/// <returns></returns>
public virtual Operand ToNumber(string errorMessage = null) { return Error(errorMessage ?? "Convert to number error!"); }
/// <summary>
/// 转数值类型
/// </summary>
/// <param name="errorMessage"></param>
/// <param name="args"></param>
/// <returns></returns>
public virtual Operand ToNumber(string errorMessage, params object[] args) { return new OperandError(errorMessage, args); }
/// <summary>
/// 转bool类型
/// </summary>
/// <param name="errorMessage"></param>
/// <returns></returns>
public virtual Operand ToBoolean(string errorMessage = null) { return Error(errorMessage ?? "Convert to bool error!"); }
/// <summary>
/// 转bool类型
/// </summary>
/// <param name="errorMessage"></param>
/// <param name="args"></param>
/// <returns></returns>
public virtual Operand ToBoolean(string errorMessage, params object[] args) { return new OperandError(errorMessage, args); }
/// <summary>
/// 转string类型
/// </summary>
/// <param name="errorMessage"></param>
/// <returns></returns>
public virtual Operand ToText(string errorMessage = null) { return Error(errorMessage ?? "Convert to string error!"); }
/// <summary>
/// 转string类型
/// </summary>
/// <param name="errorMessage"></param>
/// <param name="args"></param>
/// <returns></returns>
public virtual Operand ToText(string errorMessage, params object[] args) { return new OperandError(errorMessage, args); }
/// <summary>
/// 转MyDate类型
/// </summary>
/// <param name="errorMessage"></param>
/// <returns></returns>
public virtual Operand ToMyDate(string errorMessage = null) { return Error(errorMessage ?? "Convert to date error!"); }
/// <summary>
/// 转MyDate类型
/// </summary>
/// <param name="errorMessage"></param>
/// <param name="args"></param>
/// <returns></returns>
public virtual Operand ToMyDate(string errorMessage, params object[] args) { return new OperandError(errorMessage, args); }
/// <summary>
/// 转Array类型
/// </summary>
/// <param name="errorMessage"></param>
/// <returns></returns>
public virtual Operand ToArray(string errorMessage = null) { return Error(errorMessage ?? "Convert to array error!"); }
/// <summary>
/// 转Array类型
/// </summary>
/// <param name="errorMessage"></param>
/// <param name="args"></param>
/// <returns></returns>
public virtual Operand ToArray(string errorMessage, params object[] args) { return new OperandError(errorMessage, args); }
/// <summary>
/// 转Json类型
/// </summary>
/// <param name="errorMessage"></param>
/// <returns></returns>
public virtual Operand ToJson(string errorMessage = null) { return Error(errorMessage ?? "Convert to json error!"); }
#endregion
#region Operand
#region number
/// <summary>
/// 从 Int16 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(Int16 obj)
{
return Operand.Create((int)obj);
}
/// <summary>
/// 从 Int32 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(Int32 obj)
{
return Operand.Create(obj);
}
/// <summary>
/// 从 Int64 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(Int64 obj)
{
return Operand.Create((decimal)obj);
}
/// <summary>
/// 从 UInt16 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(UInt16 obj)
{
return Operand.Create((decimal)obj);
}
/// <summary>
/// 从 UInt32 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(UInt32 obj)
{
return Operand.Create((decimal)obj);
}
/// <summary>
/// 从 UInt64 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(UInt64 obj)
{
return Operand.Create((decimal)obj);
}
/// <summary>
/// 从 float 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(float obj)
{
return Operand.Create((decimal)obj);
}
/// <summary>
/// 从 double 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(double obj)
{
return Operand.Create(obj);
}
/// <summary>
/// 从 decimal 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(decimal obj)
{
return Operand.Create(obj);
}
#endregion number
/// <summary>
/// 从 bool 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(bool obj)
{
return Operand.Create(obj);
}
/// <summary>
/// 从 string 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(string obj)
{
return Operand.Create(obj);
}
/// <summary>
/// 从 DateTime 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(DateTime obj)
{
return Operand.Create(obj);
}
/// <summary>
/// 从 TimeSpan 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(TimeSpan obj)
{
return Operand.Create(obj);
}
/// <summary>
/// 从 List<string> 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(List<string> obj)
{
return Operand.Create(obj);
}
/// <summary>
/// 从 List<bool> 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(List<bool> obj)
{
return Operand.Create(obj);
}
/// <summary>
/// 从 List<int> 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(List<int> obj)
{
return Operand.Create(obj);
}
/// <summary>
/// 从 List<double> 隐式转换为 Operand
/// </summary>
/// <param name="obj"></param>
public static implicit operator Operand(List<double> obj)
{
return Operand.Create(obj);
}
#endregion Operand
}
}