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
101
102
103
104
105
package com.temp.shared;

import com.temp.shared.utils.*;

import java.util.*;

/**
 * Externalization Data to be used with an Externally Sourced String keyed template system that supports
 * substitutions within any specific template by "named" values.
 */
public class E13nData {

    /**
     * Substitution Value w/ a flag indicating if it is User Data, or another Template Id Code to be used as a Sub-Template.
     */
    public static class SubstitutionValue {
        private final boolean userData;
        private final String value;

        public SubstitutionValue(boolean userData, String value) {
            this.userData = userData;
            this.value = value;
        }

        /**
         * Flag indicating if the "vale" is User Data, or another Template Id Code to be used as a Sub-Template.
         */
        public boolean isUserData() {
            return userData;
        }

        /**
         * User Data, or another Template Id Code to be used as a Sub-Template.
         */
        public String getValue() {
            return value;
        }

        @Override
        public String toString() {
            return userData ? value : "{" + value + "}";
        }
    }

    private final String templateIdCode;
    private Map<String, SubstitutionValue> templateSubstitutionNamedValues;

    /**
     * @param templateIdCode Template Identifying Code
     */
    public E13nData(String templateIdCode) {
        this.templateIdCode = Validate.noEmpty("templateIdCode", templateIdCode);
    }

    private synchronized E13nData addPair(boolean userData, String name, String value) {
        name = Validate.noEmpty("name", name);
        if (templateSubstitutionNamedValues == null) {
            templateSubstitutionNamedValues = new HashMap<String, SubstitutionValue>();
        }
        templateSubstitutionNamedValues.put(name, new SubstitutionValue(userData, value));
        return this;
    }

    /**
     * Add a User Substitution Named Value.
     *
     * @param name     Not allowed to be empty
     * @param userData null converted to ""
     */
    public E13nData addSubstitutionNamedUserData(String name, String userData) {
        return addPair(true, name, StringUtils.deNull(userData));
    }

    /**
     * Add a User Substitution Named Value.
     *
     * @param name              Not allowed to be empty
     * @param subTemplateIdCode Not allowed to be empty
     */
    public E13nData addSubstitutionNamedSubTemplateIdCode(String name, String subTemplateIdCode) {
        return addPair(false, name, Validate.noEmpty("subTemplateIdCode", subTemplateIdCode));
    }

    /**
     * @return Not Empty TemplateIdCode
     */
    public String getTemplateIdCode() {
        return templateIdCode;
    }

    /**
     * @return Not null map of Template Substitution Named Values
     */
    public synchronized Map<String, SubstitutionValue> getTemplateSubstitutionNamedValues() {
        if (templateSubstitutionNamedValues == null) {
            return Collections.emptyMap();
        }
        return new HashMap<String, SubstitutionValue>(templateSubstitutionNamedValues); // defensive copy for Thread Safety
    }

    @Override
    public String toString() {
        return getTemplateIdCode() + getTemplateSubstitutionNamedValues();
    }
}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/shared/E13nData.java

Diff revisions: vs.
Revision Author Commited Message
595 Diff Diff GeorgeS picture GeorgeS Sat 21 Jan, 2012 16:54:12 +0000
594 GeorgeS picture GeorgeS Sat 21 Jan, 2012 00:40:54 +0000