|
@@ -1,75 +1,74 @@ |
1 |
1 |
|
package com.temp.shared.utils; |
2 |
2 |
|
|
3 |
|
- |
|
4 |
3 |
|
/** |
5 |
4 |
|
* Convert Integer value to/from a very tight string representation |
6 |
5 |
|
*/ |
7 |
6 |
|
public class IntegerCodec { |
8 |
|
- |
public static String encodePositive(int pValue) { |
9 |
|
- |
if (pValue < 0) { |
10 |
|
- |
throw new IllegalArgumentException("Negative values not supported: " + pValue); |
|
7 |
+ |
public static String encodePositive( int pValue ) { |
|
8 |
+ |
if ( pValue < 0 ) { |
|
9 |
+ |
throw new IllegalArgumentException( "Negative values not supported: " + pValue ); |
11 |
10 |
|
} |
12 |
|
- |
if (pValue < 32) { |
13 |
|
- |
return Character.toString(SixBitCodec.encode(pValue)); |
|
11 |
+ |
if ( pValue < 32 ) { |
|
12 |
+ |
return Character.toString( SixBitCodec.encode( pValue ) ); |
14 |
13 |
|
} |
15 |
|
- |
return new Encoder(pValue).encode(0, 32); |
|
14 |
+ |
return new Encoder( pValue ).encode( 0, 32 ); |
16 |
15 |
|
} |
17 |
16 |
|
|
18 |
|
- |
public static int decodePositive(String pValue) { |
19 |
|
- |
return decodePositive(new CharSource(pValue)); |
|
17 |
+ |
public static int decodePositive( String pValue ) { |
|
18 |
+ |
return decodePositive( new CharSource( pValue ) ); |
20 |
19 |
|
} |
21 |
20 |
|
|
22 |
|
- |
public static int decodePositive(CharSource pValue) { |
23 |
|
- |
return toInt(new Decoder(pValue).decode(0, 32)); |
|
21 |
+ |
public static int decodePositive( CharSource pValue ) { |
|
22 |
+ |
return toInt( new Decoder( pValue ).decode( 0, 32 ) ); |
24 |
23 |
|
} |
25 |
24 |
|
|
26 |
|
- |
public static String encodeSigned(int pValue) { |
27 |
|
- |
if (0 <= pValue) { |
28 |
|
- |
if (pValue < 16) { |
29 |
|
- |
return Character.toString(SixBitCodec.encode(pValue)); |
|
25 |
+ |
public static String encodeSigned( int pValue ) { |
|
26 |
+ |
if ( 0 <= pValue ) { |
|
27 |
+ |
if ( pValue < 16 ) { |
|
28 |
+ |
return Character.toString( SixBitCodec.encode( pValue ) ); |
30 |
29 |
|
} |
31 |
|
- |
return new Encoder(pValue).encode(0, 16); |
|
30 |
+ |
return new Encoder( pValue ).encode( 0, 16 ); |
32 |
31 |
|
} |
33 |
32 |
|
|
34 |
|
- |
return new Encoder(Math.abs((long) pValue)).encode(16, 16); |
|
33 |
+ |
return new Encoder( Math.abs( (long) pValue ) ).encode( 16, 16 ); |
35 |
34 |
|
} |
36 |
35 |
|
|
37 |
|
- |
public static int decodeSigned(String pValue) { |
38 |
|
- |
return decodeSigned(new CharSource(pValue)); |
|
36 |
+ |
public static int decodeSigned( String pValue ) { |
|
37 |
+ |
return decodeSigned( new CharSource( pValue ) ); |
39 |
38 |
|
} |
40 |
39 |
|
|
41 |
|
- |
public static int decodeSigned(CharSource pValue) { |
42 |
|
- |
return toInt(new Decoder(pValue).decode(16, 16)); |
|
40 |
+ |
public static int decodeSigned( CharSource pValue ) { |
|
41 |
+ |
return toInt( new Decoder( pValue ).decode( 16, 16 ) ); |
43 |
42 |
|
} |
44 |
43 |
|
|
45 |
|
- |
private static int toInt(long pValue) { |
46 |
|
- |
if ((Integer.MIN_VALUE <= pValue) && (pValue <= Integer.MAX_VALUE)) { |
|
44 |
+ |
private static int toInt( long pValue ) { |
|
45 |
+ |
if ( (Integer.MIN_VALUE <= pValue) && (pValue <= Integer.MAX_VALUE) ) { |
47 |
46 |
|
return (int) pValue; |
48 |
47 |
|
} |
49 |
|
- |
throw new IllegalArgumentException("Resulting value (" + pValue + ") is outside the acceptable Integer Range"); |
|
48 |
+ |
throw new IllegalArgumentException( "Resulting value (" + pValue + ") is outside the acceptable Integer Range" ); |
50 |
49 |
|
} |
51 |
50 |
|
|
52 |
51 |
|
private static class Encoder { |
53 |
52 |
|
private long mValue; |
54 |
53 |
|
|
55 |
|
- |
Encoder(long pValue) { |
|
54 |
+ |
Encoder( long pValue ) { |
56 |
55 |
|
mValue = pValue; |
57 |
56 |
|
} |
58 |
57 |
|
|
59 |
|
- |
public String encode(int pNegativeBit, int pFirstCharLimit) { |
|
58 |
+ |
public String encode( int pNegativeBit, int pFirstCharLimit ) { |
60 |
59 |
|
StringBuilder sb = new StringBuilder(); |
61 |
|
- |
int bits = extractBits(pFirstCharLimit - 1, pFirstCharLimit); |
62 |
|
- |
sb.append(SixBitCodec.encode(bits | pNegativeBit)); |
63 |
|
- |
while ((bits & 32) != 0) { |
64 |
|
- |
bits = extractBits(31, 32); |
65 |
|
- |
sb.append(SixBitCodec.encode(bits)); |
|
60 |
+ |
int bits = extractBits( pFirstCharLimit - 1, pFirstCharLimit ); |
|
61 |
+ |
sb.append( SixBitCodec.encode( bits | pNegativeBit ) ); |
|
62 |
+ |
while ( (bits & 32) != 0 ) { |
|
63 |
+ |
bits = extractBits( 31, 32 ); |
|
64 |
+ |
sb.append( SixBitCodec.encode( bits ) ); |
66 |
65 |
|
} |
67 |
66 |
|
return sb.toString(); |
68 |
67 |
|
} |
69 |
68 |
|
|
70 |
|
- |
private int extractBits(int pBits, int pDivisor) { |
|
69 |
+ |
private int extractBits( int pBits, int pDivisor ) { |
71 |
70 |
|
int rv = (int) (mValue & pBits); |
72 |
|
- |
if ((mValue /= pDivisor) > 0) { |
|
71 |
+ |
if ( (mValue /= pDivisor) > 0 ) { |
73 |
72 |
|
rv |= 32; |
74 |
73 |
|
} |
75 |
74 |
|
return rv; |
|
@@ -79,19 +78,19 @@ |
79 |
78 |
|
private static class Decoder { |
80 |
79 |
|
private CharSource mValue; |
81 |
80 |
|
|
82 |
|
- |
Decoder(CharSource pValue) { |
|
81 |
+ |
Decoder( CharSource pValue ) { |
83 |
82 |
|
mValue = pValue; |
84 |
83 |
|
} |
85 |
84 |
|
|
86 |
|
- |
public long decode(int pNegativeBit, long pMultiplier) { |
87 |
|
- |
int bits = SixBitCodec.decode(mValue.getRequired()); |
|
85 |
+ |
public long decode( int pNegativeBit, long pMultiplier ) { |
|
86 |
+ |
int bits = SixBitCodec.decode( mValue.getRequired() ); |
88 |
87 |
|
boolean negative = ((bits & pNegativeBit) != 0); |
89 |
|
- |
if (negative) { |
|
88 |
+ |
if ( negative ) { |
90 |
89 |
|
bits -= pNegativeBit; |
91 |
90 |
|
} |
92 |
91 |
|
long result = (bits & 31); |
93 |
|
- |
while ((bits & 32) != 0) { |
94 |
|
- |
bits = SixBitCodec.decode(mValue.getRequired()); |
|
92 |
+ |
while ( (bits & 32) != 0 ) { |
|
93 |
+ |
bits = SixBitCodec.decode( mValue.getRequired() ); |
95 |
94 |
|
result += (bits & 31) * pMultiplier; |
96 |
95 |
|
pMultiplier *= 32; |
97 |
96 |
|
} |