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
package com.temp.client.foundation.pavment;

import java.util.HashMap;
import java.util.Map;


/**
 * Registry of the Default PlaceTokenCodec for encoding, and all the
 * PlaceTokenCodec(s) available for decoding.
 *
 * @author georgs
 */
public class PlaceTokenCodecRegistry {
    /******************* See Bottom of Class for Registering PlaceTokenCodec(s) *******************/

    /**
     * Encode the String (nulls are returned as "")
     *
     * @return nulls are returned as "", otherwise the Encoded String.
     */
    public static String encode(String unencoded) {
        if ((unencoded == null) || (unencoded.length() == 0)) {
            return "";
        }
        return Character.toString(DEFAULT_CODEC_ID) + DEFAULT_CODEC.encode(unencoded);
    }

    /**
     * Decode the previously "encoded" data, null or bad encoding will return
     * null.
     *
     * @return null indicates that the data was either null or contained
     *         unacceptable characters for decoding.
     */
    public static String decode(String encoded) {
        if (encoded == null) {
            return null;
        }
        if (encoded.length() == 0) {
            return "";
        }
        PlaceTokenCodec codec = CODECS.get(encoded.charAt(0));
        return (codec != null) ? codec.decode(encoded.substring(1)) : null;
    }

    /******************************** Maintain the order of the following ********************************/
    private static final Map<Character, PlaceTokenCodec> CODECS = new HashMap<Character, PlaceTokenCodec>();
    private static char DEFAULT_CODEC_ID;
    private static PlaceTokenCodec DEFAULT_CODEC;

    private static void register(char chr, PlaceTokenCodec codec) {
        CODECS.put(DEFAULT_CODEC_ID = chr, DEFAULT_CODEC = codec);
    }

    static { // Last One registered is the Default!
        register('0', new PlaceTokenCodecCharacterDeltaWithRandomStart());
        register('1', new PlaceTokenCodecCharacterDeltaWithRandomStartWitAdjustment());
    }
}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/client/foundation/pavment/PlaceTokenCodecRegistry.java

Diff revisions: vs.
Revision Author Commited Message
626 GeorgeS picture GeorgeS Wed 11 Apr, 2012 19:39:41 +0000