@@ -10,15 +10,41 @@ public class BasicDuration extends AbstractScalar implements Comparable<BasicDur
10
10
private static final String [] unitSyms = {"ns" , "us" , "ms" , "s" , "m" , "H" , "d" , "w" , "M" , "y" , "B" };
11
11
private int value ;
12
12
private DURATION unit ;
13
+ private int exchange_ ;
13
14
14
15
public BasicDuration (DURATION unit , int value ){
15
16
this .value = value ;
16
17
this .unit = unit ;
18
+ this .exchange_ = unit .ordinal ();
19
+ }
20
+
21
+ public BasicDuration (String unit , int value ) {
22
+ this .value = value ;
23
+ int codeNumber = getCodeNumber (unit );
24
+ if (codeNumber >= 0 && codeNumber <= 10 )
25
+ this .unit = DURATION .values ()[codeNumber ];
26
+ else
27
+ this .unit = DURATION .TDAY ;
28
+ this .exchange_ = codeNumber ;
29
+ }
30
+
31
+ protected BasicDuration (int exchange_ , int value ) {
32
+ this .value = value ;
33
+ if (exchange_ >= 0 && exchange_ <= 10 )
34
+ this .unit = DURATION .values ()[exchange_ ];
35
+ else
36
+ this .unit = DURATION .TDAY ;
37
+ this .exchange_ = exchange_ ;
17
38
}
18
39
19
40
public BasicDuration (ExtendedDataInput in ) throws IOException {
20
41
value = in .readInt ();
21
- unit = DURATION .values ()[in .readInt ()];
42
+ int codeNumber = in .readInt ();
43
+ if (codeNumber >= 0 && codeNumber <= 10 )
44
+ unit = DURATION .values ()[codeNumber ];
45
+ else
46
+ unit = DURATION .TDAY ;
47
+ exchange_ = codeNumber ;
22
48
}
23
49
24
50
public int getDuration () {
@@ -29,6 +55,22 @@ public DURATION getUnit() {
29
55
return unit ;
30
56
}
31
57
58
+ public int getExchangeInt () {
59
+ return exchange_ ;
60
+ }
61
+
62
+ public String getExchangeName () {
63
+ if (exchange_ >= 0 && exchange_ <= 10 ) {
64
+ return unitSyms [exchange_ ];
65
+ }
66
+ char [] codes = new char [4 ];
67
+ codes [0 ] = (char ) ((exchange_ >> 24 ) & 255 );
68
+ codes [1 ] = (char ) ((exchange_ >> 16 ) & 255 );
69
+ codes [2 ] = (char ) ((exchange_ >> 8 ) & 255 );
70
+ codes [3 ] = (char ) (exchange_ & 255 );
71
+ return String .valueOf (codes );
72
+ }
73
+
32
74
@ Override
33
75
public boolean isNull () {
34
76
return value == Integer .MIN_VALUE ;
@@ -75,19 +117,19 @@ public String getString() {
75
117
if (value == Integer .MIN_VALUE )
76
118
return "" ;
77
119
else
78
- return String . valueOf ( value ) + unitSyms [ unit . ordinal ()] ;
120
+ return value + getExchangeName () ;
79
121
}
80
122
81
123
@ Override
82
124
protected void writeScalarToOutputStream (ExtendedDataOutput out ) throws IOException {
83
125
out .writeInt (value );
84
- out .writeInt (unit . ordinal () );
126
+ out .writeInt (exchange_ );
85
127
}
86
128
87
129
@ Override
88
130
public int compareTo (BasicDuration o ) {
89
- if (unit == o .unit )
90
- return Integer .compare ((( BasicDuration ) o ) .value ,value );
131
+ if (unit == o .unit && exchange_ == o . exchange_ )
132
+ return Integer .compare (o .value , value );
91
133
else
92
134
return -1 ;
93
135
}
@@ -97,12 +139,51 @@ public boolean equals(Object o){
97
139
if (! (o instanceof BasicDuration ) || o == null )
98
140
return false ;
99
141
else
100
- return value == ((BasicDuration )o ).value && unit == ((BasicDuration )o ).unit ;
142
+ return value == ((BasicDuration ) o ).value && unit == ((BasicDuration ) o ).unit && exchange_ == (( BasicDuration ) o ). exchange_ ;
101
143
}
102
144
103
145
@ JsonIgnore
104
146
@ Override
105
147
public int getScale (){
106
148
return super .getScale ();
107
149
}
150
+
151
+ private int getCodeNumber (String unit ) {
152
+ if (unit .length () == 1 || unit .length () == 2 ) {
153
+ switch (unit ) {
154
+ case "ns" :
155
+ return 0 ;
156
+ case "us" :
157
+ return 1 ;
158
+ case "ms" :
159
+ return 2 ;
160
+ case "s" :
161
+ return 3 ;
162
+ case "m" :
163
+ return 4 ;
164
+ case "H" :
165
+ return 5 ;
166
+ case "d" :
167
+ return 6 ;
168
+ case "w" :
169
+ return 7 ;
170
+ case "M" :
171
+ return 8 ;
172
+ case "y" :
173
+ return 9 ;
174
+ case "B" :
175
+ return 10 ;
176
+ default :
177
+ throw new RuntimeException ("error unit: " + unit );
178
+ }
179
+ } else if (unit .length () == 4 ) {
180
+ int [] codes = new int [4 ];
181
+ for (int i = 0 ; i < 4 ; i ++) {
182
+ codes [i ] = unit .charAt (i );
183
+ }
184
+ return (codes [0 ] << 24 ) + (codes [1 ] << 16 ) + (codes [2 ] << 8 ) + codes [3 ];
185
+ } else {
186
+ throw new RuntimeException ("error length of unit, " + unit .length ());
187
+ }
188
+ }
108
189
}
0 commit comments