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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.temp.common.shared.utils;

/**
 * Convert Integer value to/from a very tight string representation
 */
public class IntegerCodec {
    public static String encodePositive( int pValue ) {
        if ( pValue < 0 ) {
            throw new IllegalArgumentException( "Negative values not supported: " + pValue );
        }
        if ( pValue < 32 ) {
            return Character.toString( SixBitCodec.encode( pValue ) );
        }
        return new Encoder( pValue ).encode( 0, 32 );
    }

    public static int decodePositive( String pValue ) {
        return decodePositive( new CharSource( pValue ) );
    }

    public static int decodePositive( CharSource pValue ) {
        return toInt( new Decoder( pValue ).decode( 0, 32 ) );
    }

    public static String encodeSigned( int pValue ) {
        if ( 0 <= pValue ) {
            if ( pValue < 16 ) {
                return Character.toString( SixBitCodec.encode( pValue ) );
            }
            return new Encoder( pValue ).encode( 0, 16 );
        }

        return new Encoder( Math.abs( (long) pValue ) ).encode( 16, 16 );
    }

    public static int decodeSigned( String pValue ) {
        return decodeSigned( new CharSource( pValue ) );
    }

    public static int decodeSigned( CharSource pValue ) {
        return toInt( new Decoder( pValue ).decode( 16, 16 ) );
    }

    private static int toInt( long pValue ) {
        if ( (Integer.MIN_VALUE <= pValue) && (pValue <= Integer.MAX_VALUE) ) {
            return (int) pValue;
        }
        throw new IllegalArgumentException( "Resulting value (" + pValue + ") is outside the acceptable Integer Range" );
    }

    private static class Encoder {
        private long mValue;

        Encoder( long pValue ) {
            mValue = pValue;
        }

        public String encode( int pNegativeBit, int pFirstCharLimit ) {
            StringBuilder sb = new StringBuilder();
            int bits = extractBits( pFirstCharLimit - 1, pFirstCharLimit );
            sb.append( SixBitCodec.encode( bits | pNegativeBit ) );
            while ( (bits & 32) != 0 ) {
                bits = extractBits( 31, 32 );
                sb.append( SixBitCodec.encode( bits ) );
            }
            return sb.toString();
        }

        private int extractBits( int pBits, int pDivisor ) {
            int rv = (int) (mValue & pBits);
            if ( (mValue /= pDivisor) > 0 ) {
                rv |= 32;
            }
            return rv;
        }
    }

    private static class Decoder {
        private CharSource mValue;

        Decoder( CharSource pValue ) {
            mValue = pValue;
        }

        public long decode( int pNegativeBit, long pMultiplier ) {
            int bits = SixBitCodec.decode( mValue.getRequired() );
            boolean negative = ((bits & pNegativeBit) != 0);
            if ( negative ) {
                bits -= pNegativeBit;
            }
            long result = (bits & 31);
            while ( (bits & 32) != 0 ) {
                bits = SixBitCodec.decode( mValue.getRequired() );
                result += (bits & 31) * pMultiplier;
                pMultiplier *= 32;
            }
            return negative ? -result : result;
        }
    }
}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/common/shared/utils/IntegerCodec.java

Diff revisions: vs.
Revision Author Commited Message
964 GeorgeS picture GeorgeS Fri 01 Aug, 2014 03:18:23 +0000

!