litesoft
Diff Revisions
766
vs
767
for /trunk/Java/core/Anywhere/src/org/litesoft/codec/AbstractCodec.java
|
@@ -5,9 +5,44 @@ |
5 |
5 |
|
|
6 |
6 |
|
public abstract class AbstractCodec<T> implements Codec<T> |
7 |
7 |
|
{ |
|
8 |
+ |
protected static final char NULL_AS_CHAR = SixBitCodec.ESCAPE; |
|
9 |
+ |
protected static final String NULL_AS_STRING = Character.toString( NULL_AS_CHAR ); |
|
10 |
+ |
|
|
11 |
+ |
@Override |
|
12 |
+ |
public final @NotNull String encode( @Nullable T pValue ) |
|
13 |
+ |
{ |
|
14 |
+ |
return (pValue == null) ? NULL_AS_STRING : encodeNonNull( pValue ); |
|
15 |
+ |
} |
|
16 |
+ |
|
8 |
17 |
|
@Override |
9 |
18 |
|
public final @Nullable T decode( @NotNull String pValue ) |
10 |
19 |
|
{ |
11 |
|
- |
return decode( new CharSource( pValue ) ); |
|
20 |
+ |
CharSource zCharSource = new CharSource( pValue ); |
|
21 |
+ |
T zDecoded = decode( zCharSource ); |
|
22 |
+ |
if ( zCharSource.anyRemaining() ) |
|
23 |
+ |
{ |
|
24 |
+ |
throw new IllegalArgumentException( "Extraneous character(s) starting at " + zCharSource.getFrom() + " in: " + pValue ); |
|
25 |
+ |
} |
|
26 |
+ |
return zDecoded; |
|
27 |
+ |
} |
|
28 |
+ |
|
|
29 |
+ |
@Override |
|
30 |
+ |
public final @Nullable T decode( @NotNull CharSource pCharSource ) |
|
31 |
+ |
{ |
|
32 |
+ |
return isEscapeWithConsume( pCharSource ) ? null : decodeNonNull( pCharSource ); |
|
33 |
+ |
} |
|
34 |
+ |
|
|
35 |
+ |
protected abstract @NotNull String encodeNonNull( T pValue ); |
|
36 |
+ |
|
|
37 |
+ |
protected abstract @NotNull T decodeNonNull( CharSource pCharSource ); |
|
38 |
+ |
|
|
39 |
+ |
protected static boolean isEscapeWithConsume( CharSource pCharSource ) |
|
40 |
+ |
{ |
|
41 |
+ |
if ( pCharSource.peek() == NULL_AS_CHAR ) |
|
42 |
+ |
{ |
|
43 |
+ |
pCharSource.get(); // consume |
|
44 |
+ |
return true; |
|
45 |
+ |
} |
|
46 |
+ |
return false; |
12 |
47 |
|
} |
13 |
48 |
|
} |