Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.litesoft.codec;

import org.litesoft.charstreams.*;
import org.litesoft.commonfoundation.annotations.*;

public abstract class AbstractTypedTerminatingCodec<T> implements TypedTerminatingCodec<T>
{
    protected static final char NULL_AS_CHAR = SixBitCodec.NOT_USED_NOT;

    @Override
    public final @NotNull String encode( @Nullable T pValue )
    {
        CharSink zCharSink = new CharSinkToString();
        encode( zCharSink, pValue );
        return zCharSink.toString();
    }

    @Override
    public final void encode( @NotNull CharSink pCharSink, @Nullable T pValue )
    {
        if ( pValue == null )
        {
            pCharSink.add( NULL_AS_CHAR );
        }
        else
        {
            encodeNonNull( pCharSink, pValue );
        }
    }

    @Override
    public final @Nullable T decode( @NotNull CharSequence pValue )
    {
        CharSource zCharSource = new CharSourceFromSequence( pValue );
        return validateConsumed( pValue, zCharSource, decode( zCharSource ) );
    }

    protected T validateConsumed( CharSequence pValue, CharSource pCharSource, T pDecoded )
    {
        if ( pCharSource.anyRemaining() )
        {
            throw new IllegalArgumentException( "Extraneous character(s) starting at " + pCharSource.getNextOffset() + " in: " + pValue );
        }
        return pDecoded;
    }

    @Override
    public final @Nullable T decode( @NotNull CharSource pCharSource )
    {
        return isEscapeWithConsume( pCharSource ) ? null : decodeNonNull( pCharSource );
    }

    protected abstract void encodeNonNull( CharSink pCharSink, T pValue );

    protected abstract @NotNull T decodeNonNull( CharSource pCharSource );

    protected static boolean isEscapeWithConsume( CharSource pCharSource )
    {
        if ( pCharSource.peek() == NULL_AS_CHAR )
        {
            pCharSource.get(); // consume
            return true;
        }
        return !pCharSource.anyRemaining();
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/codec/AbstractTypedTerminatingCodec.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

834 Diff Diff GeorgeS picture GeorgeS Sun 02 Sep, 2012 14:00:11 +0000
779 Diff Diff GeorgeS picture GeorgeS Mon 16 Jul, 2012 04:34:33 +0000
777 Diff Diff GeorgeS picture GeorgeS Mon 16 Jul, 2012 00:48:01 +0000
773 Diff Diff GeorgeS picture GeorgeS Sun 15 Jul, 2012 22:39:12 +0000
772 Diff Diff GeorgeS picture GeorgeS Sun 15 Jul, 2012 16:55:51 +0000

!

770 Diff Diff GeorgeS picture GeorgeS Sat 14 Jul, 2012 23:20:33 +0000
769 Diff Diff GeorgeS picture GeorgeS Sat 14 Jul, 2012 23:18:20 +0000
768 Diff Diff GeorgeS picture GeorgeS Sat 14 Jul, 2012 22:48:34 +0000

!

767 GeorgeS picture GeorgeS Sat 14 Jul, 2012 18:19:39 +0000