1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Diagnostics . CodeAnalysis ;
4
+ using System . Text ;
5
+
6
+ namespace Finance
7
+ {
8
+ public readonly struct Account : IEquatable < Account > , IComparable < Account >
9
+ {
10
+ private readonly string [ ] _branch = Array . Empty < string > ( ) ;
11
+
12
+ public Account ( params string [ ] branch )
13
+ {
14
+ if ( branch == null )
15
+ {
16
+ throw new ArgumentException ( ) ;
17
+ }
18
+ _branch = branch ;
19
+ }
20
+
21
+ public string [ ] Branch => _branch ;
22
+
23
+ public override string ToString ( )
24
+ {
25
+ return string . Join ( ":" , _branch ) ;
26
+ }
27
+
28
+ public bool IsUnder ( Account parent )
29
+ {
30
+ if ( parent . Branch . Length >= this . Branch . Length )
31
+ {
32
+ return false ;
33
+ }
34
+
35
+ for ( var i = 0 ; i < parent . Branch . Length ; i ++ )
36
+ {
37
+ if ( parent . Branch [ i ] != Branch [ i ] )
38
+ {
39
+ return false ;
40
+ }
41
+ }
42
+
43
+ return true ;
44
+ }
45
+
46
+ public static bool operator == ( Account first , Account other )
47
+ {
48
+ return first . Equals ( other ) ;
49
+ }
50
+
51
+ public static bool operator != ( Account first , Account other )
52
+ {
53
+ return ! first . Equals ( other ) ;
54
+ }
55
+
56
+ public bool Equals ( Account other )
57
+ {
58
+ if ( other . _branch . Length != this . _branch . Length )
59
+ {
60
+ return false ;
61
+ }
62
+
63
+ for ( var i = 0 ; i < _branch . Length ; i ++ )
64
+ {
65
+ if ( other . Branch [ i ] != Branch [ i ] )
66
+ {
67
+ return false ;
68
+ }
69
+ }
70
+
71
+ return true ;
72
+ }
73
+
74
+ public override int GetHashCode ( )
75
+ {
76
+ var hashCode = 0 ;
77
+ foreach ( var part in _branch )
78
+ {
79
+ hashCode ^= part . GetHashCode ( ) ;
80
+ }
81
+
82
+ return hashCode ;
83
+ }
84
+
85
+ public int CompareTo ( Account other )
86
+ {
87
+ for ( var i = 0 ; ; i ++ )
88
+ {
89
+ if ( i < this . Branch . Length && i < other . Branch . Length )
90
+ {
91
+ var order = string . Compare ( this . Branch [ i ] , other . Branch [ i ] , StringComparison . Ordinal ) ;
92
+ if ( order != 0 )
93
+ {
94
+ return order ;
95
+ }
96
+ }
97
+ else if ( i < this . Branch . Length )
98
+ {
99
+ return 1 ;
100
+ }
101
+ else if ( i < other . Branch . Length )
102
+ {
103
+ return - 1 ;
104
+ }
105
+ else
106
+ {
107
+ return 0 ;
108
+ }
109
+ }
110
+ }
111
+ }
112
+ }
0 commit comments