@@ -10,15 +10,41 @@ public class BasicDuration extends AbstractScalar implements Comparable<BasicDur
1010 private static final String [] unitSyms = {"ns" , "us" , "ms" , "s" , "m" , "H" , "d" , "w" , "M" , "y" , "B" };
1111 private int value ;
1212 private DURATION unit ;
13+ private int exchange_ ;
1314
1415 public BasicDuration (DURATION unit , int value ){
1516 this .value = value ;
1617 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_ ;
1738 }
1839
1940 public BasicDuration (ExtendedDataInput in ) throws IOException {
2041 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 ;
2248 }
2349
2450 public int getDuration () {
@@ -29,6 +55,22 @@ public DURATION getUnit() {
2955 return unit ;
3056 }
3157
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+
3274 @ Override
3375 public boolean isNull () {
3476 return value == Integer .MIN_VALUE ;
@@ -75,19 +117,19 @@ public String getString() {
75117 if (value == Integer .MIN_VALUE )
76118 return "" ;
77119 else
78- return String . valueOf ( value ) + unitSyms [ unit . ordinal ()] ;
120+ return value + getExchangeName () ;
79121 }
80122
81123 @ Override
82124 protected void writeScalarToOutputStream (ExtendedDataOutput out ) throws IOException {
83125 out .writeInt (value );
84- out .writeInt (unit . ordinal () );
126+ out .writeInt (exchange_ );
85127 }
86128
87129 @ Override
88130 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 );
91133 else
92134 return -1 ;
93135 }
@@ -97,12 +139,51 @@ public boolean equals(Object o){
97139 if (! (o instanceof BasicDuration ) || o == null )
98140 return false ;
99141 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_ ;
101143 }
102144
103145 @ JsonIgnore
104146 @ Override
105147 public int getScale (){
106148 return super .getScale ();
107149 }
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+ }
108189}
0 commit comments