|
@@ -2,180 +2,32 @@ |
2 |
2 |
|
package org.litesoft.codec; |
3 |
3 |
|
|
4 |
4 |
|
import org.litesoft.charstreams.*; |
5 |
|
- |
import org.litesoft.core.annotations.*; |
6 |
5 |
|
|
7 |
6 |
|
/** |
8 |
7 |
|
* Convert Integer value to/from a very tight string representation |
9 |
8 |
|
*/ |
10 |
|
- |
public class IntegerTypedTerminatingCodec |
|
9 |
+ |
public class IntegerTypedTerminatingCodec extends AbstractTypedTerminatingCodec<Integer> |
11 |
10 |
|
{ |
12 |
|
- |
public interface Primitive |
13 |
|
- |
{ |
14 |
|
- |
abstract public void encode( @NotNull CharSink pCharSink, int pValue ); |
|
11 |
+ |
public static final IntegerTypedTerminatingCodec SIGNED = new IntegerTypedTerminatingCodec( PrimitiveIntegerCodec.SIGNED ); |
|
12 |
+ |
public static final IntegerTypedTerminatingCodec NON_NEGATIVE = new IntegerTypedTerminatingCodec( PrimitiveIntegerCodec.NON_NEGATIVE ); |
15 |
13 |
|
|
16 |
|
- |
abstract public int decode( @NotNull CharSource pCharSource ); |
17 |
|
- |
} |
|
14 |
+ |
private final PrimitiveIntegerCodec mPrimitiveCodec; |
18 |
15 |
|
|
19 |
|
- |
public static class SignedPrimitive implements Primitive |
|
16 |
+ |
private IntegerTypedTerminatingCodec( PrimitiveIntegerCodec pPrimitiveCodec ) |
20 |
17 |
|
{ |
21 |
|
- |
public static final SignedPrimitive INSTANCE = new SignedPrimitive(); |
22 |
|
- |
|
23 |
|
- |
@Override |
24 |
|
- |
public void encode( @NotNull CharSink pCharSink, int pValue ) |
25 |
|
- |
{ |
26 |
|
- |
if ( pValue < 0 ) |
27 |
|
- |
{ |
28 |
|
- |
new Encoder( pCharSink, Math.abs( (long) pValue ) ).encode( 16, 16 ); |
29 |
|
- |
} |
30 |
|
- |
else |
31 |
|
- |
{ |
32 |
|
- |
if ( pValue < 16 ) |
33 |
|
- |
{ |
34 |
|
- |
pCharSink.add( SixBitCodec.encode( pValue ) ); |
35 |
|
- |
} |
36 |
|
- |
else |
37 |
|
- |
{ |
38 |
|
- |
new Encoder( pCharSink, pValue ).encode( 0, 16 ); |
39 |
|
- |
} |
40 |
|
- |
} |
41 |
|
- |
} |
42 |
|
- |
|
43 |
|
- |
@Override |
44 |
|
- |
public int decode( @NotNull CharSource pCharSource ) |
45 |
|
- |
{ |
46 |
|
- |
return toInt( new Decoder( pCharSource ).decode( 16, 16 ) ); |
47 |
|
- |
} |
|
18 |
+ |
mPrimitiveCodec = pPrimitiveCodec; |
48 |
19 |
|
} |
49 |
20 |
|
|
50 |
|
- |
public static class Signed extends AbstractTypedTerminatingCodec<Integer> |
|
21 |
+ |
@Override |
|
22 |
+ |
protected void encodeNonNull( CharSink pCharSink, Integer pValue ) |
51 |
23 |
|
{ |
52 |
|
- |
public static final Signed INSTANCE = new Signed(); |
53 |
|
- |
|
54 |
|
- |
@Override |
55 |
|
- |
protected void encodeNonNull( CharSink pCharSink, Integer pValue ) |
56 |
|
- |
{ |
57 |
|
- |
SignedPrimitive.INSTANCE.encode( pCharSink, pValue ); |
58 |
|
- |
} |
59 |
|
- |
|
60 |
|
- |
@Override |
61 |
|
- |
protected Integer decodeNonNull( CharSource pCharSource ) |
62 |
|
- |
{ |
63 |
|
- |
return SignedPrimitive.INSTANCE.decode( pCharSource ); |
64 |
|
- |
} |
|
24 |
+ |
mPrimitiveCodec.encode( pCharSink, pValue ); |
65 |
25 |
|
} |
66 |
26 |
|
|
67 |
|
- |
public static class NonNegativePrimitive implements Primitive |
|
27 |
+ |
@Override |
|
28 |
+ |
protected Integer decodeNonNull( CharSource pCharSource ) |
68 |
29 |
|
{ |
69 |
|
- |
public static final NonNegativePrimitive INSTANCE = new NonNegativePrimitive(); |
70 |
|
- |
|
71 |
|
- |
@Override |
72 |
|
- |
public void encode( @NotNull CharSink pCharSink, int pValue ) |
73 |
|
- |
{ |
74 |
|
- |
if ( pValue < 0 ) |
75 |
|
- |
{ |
76 |
|
- |
throw new IllegalArgumentException( "Negative values not supported: " + pValue ); |
77 |
|
- |
} |
78 |
|
- |
if ( pValue < 32 ) |
79 |
|
- |
{ |
80 |
|
- |
pCharSink.add( SixBitCodec.encode( pValue ) ); |
81 |
|
- |
} |
82 |
|
- |
else |
83 |
|
- |
{ |
84 |
|
- |
new Encoder( pCharSink, pValue ).encode( 0, 32 ); |
85 |
|
- |
} |
86 |
|
- |
} |
87 |
|
- |
|
88 |
|
- |
@Override |
89 |
|
- |
public int decode( @NotNull CharSource pCharSource ) |
90 |
|
- |
{ |
91 |
|
- |
return toInt( new Decoder( pCharSource ).decode( 0, 32 ) ); |
92 |
|
- |
} |
93 |
|
- |
} |
94 |
|
- |
|
95 |
|
- |
public static class NonNegative extends AbstractTypedTerminatingCodec<Integer> |
96 |
|
- |
{ |
97 |
|
- |
public static final NonNegative INSTANCE = new NonNegative(); |
98 |
|
- |
|
99 |
|
- |
@Override |
100 |
|
- |
protected void encodeNonNull( CharSink pCharSink, Integer pValue ) |
101 |
|
- |
{ |
102 |
|
- |
NonNegativePrimitive.INSTANCE.encode( pCharSink, pValue ); |
103 |
|
- |
} |
104 |
|
- |
|
105 |
|
- |
@Override |
106 |
|
- |
protected Integer decodeNonNull( CharSource pCharSource ) |
107 |
|
- |
{ |
108 |
|
- |
return NonNegativePrimitive.INSTANCE.decode( pCharSource ); |
109 |
|
- |
} |
110 |
|
- |
} |
111 |
|
- |
|
112 |
|
- |
private static int toInt( long pValue ) |
113 |
|
- |
{ |
114 |
|
- |
if ( (Integer.MIN_VALUE <= pValue) && (pValue <= Integer.MAX_VALUE) ) |
115 |
|
- |
{ |
116 |
|
- |
return (int) pValue; |
117 |
|
- |
} |
118 |
|
- |
throw new IllegalArgumentException( "Resulting value (" + pValue + ") is outside the acceptable Integer Range" ); |
119 |
|
- |
} |
120 |
|
- |
|
121 |
|
- |
private static class Encoder |
122 |
|
- |
{ |
123 |
|
- |
private CharSink mCharSink; |
124 |
|
- |
private long mValue; |
125 |
|
- |
|
126 |
|
- |
Encoder( CharSink pCharSink, long pValue ) |
127 |
|
- |
{ |
128 |
|
- |
mCharSink = pCharSink; |
129 |
|
- |
mValue = pValue; |
130 |
|
- |
} |
131 |
|
- |
|
132 |
|
- |
public void encode( int pNegativeBit, int pFirstCharLimit ) |
133 |
|
- |
{ |
134 |
|
- |
int bits = extractBits( pFirstCharLimit - 1, pFirstCharLimit ); |
135 |
|
- |
mCharSink.add( SixBitCodec.encode( bits | pNegativeBit ) ); |
136 |
|
- |
while ( (bits & 32) != 0 ) |
137 |
|
- |
{ |
138 |
|
- |
bits = extractBits( 31, 32 ); |
139 |
|
- |
mCharSink.add( SixBitCodec.encode( bits ) ); |
140 |
|
- |
} |
141 |
|
- |
} |
142 |
|
- |
|
143 |
|
- |
private int extractBits( int pBits, int pDivisor ) |
144 |
|
- |
{ |
145 |
|
- |
int rv = (int) (mValue & pBits); |
146 |
|
- |
if ( (mValue /= pDivisor) > 0 ) |
147 |
|
- |
{ |
148 |
|
- |
rv |= 32; |
149 |
|
- |
} |
150 |
|
- |
return rv; |
151 |
|
- |
} |
152 |
|
- |
} |
153 |
|
- |
|
154 |
|
- |
private static class Decoder |
155 |
|
- |
{ |
156 |
|
- |
private CharSource mValue; |
157 |
|
- |
|
158 |
|
- |
Decoder( CharSource pValue ) |
159 |
|
- |
{ |
160 |
|
- |
mValue = pValue; |
161 |
|
- |
} |
162 |
|
- |
|
163 |
|
- |
public long decode( int pNegativeBit, long pMultiplier ) |
164 |
|
- |
{ |
165 |
|
- |
int bits = SixBitCodec.decode( mValue.getRequired() ); |
166 |
|
- |
boolean negative = ((bits & pNegativeBit) != 0); |
167 |
|
- |
if ( negative ) |
168 |
|
- |
{ |
169 |
|
- |
bits -= pNegativeBit; |
170 |
|
- |
} |
171 |
|
- |
long result = (bits & 31); |
172 |
|
- |
while ( (bits & 32) != 0 ) |
173 |
|
- |
{ |
174 |
|
- |
bits = SixBitCodec.decode( mValue.getRequired() ); |
175 |
|
- |
result += (bits & 31) * pMultiplier; |
176 |
|
- |
pMultiplier *= 32; |
177 |
|
- |
} |
178 |
|
- |
return negative ? -result : result; |
179 |
|
- |
} |
|
30 |
+ |
return mPrimitiveCodec.decode( pCharSource ); |
180 |
31 |
|
} |
181 |
32 |
|
} |
|
33 |
+ |
|