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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.core.util;

import org.litesoft.core.typeutils.Objects;

import java.util.*;

/**
 * SimpleMessage is similar to MessageFormat in that it utilizes {} to identify the substitution positions
 * and index of the parameters.  It differs in that nothing but simple indexes are supported and if no substitutions
 * are indicated, then "{0} " is added to the front, and all other parameters are added to the end.<p>
 * <p/>
 * The following example creates a <code>MessageFormat</code> instance that
 * can be used repeatedly:
 * <blockquote><pre>
 * int fileCount = 1273;
 * String diskName = "MyDisk";
 * Object[] testArgs = {new Long(fileCount), diskName};
 * <p/>
 * MessageFormat form = new MessageFormat(
 *     "The disk \"{1}\" contains {0} file(s).");
 */
public class SimpleMessage
{
    public static final char START_SUB = '{';
    public static final char END_SUB = '}';

    public static String format( String pPattern, Object... pParameters )
    {
        if ( pPattern == null )
        {
            pPattern = "?";
        }
        AbstractFormatter zFormatter;
        synchronized ( sCached )
        {
            if ( null == (zFormatter = sCached.get( pPattern )) )
            {
                sCached.put( pPattern, zFormatter = createFormatter( pPattern ) );
            }
        }
        return zFormatter.format( pParameters );
    }

    private static final Map<String, AbstractFormatter> sCached = new HashMap<String, AbstractFormatter>();

    private static String getParam( Object[] pParameters, int pIndex )
    {
        return (pParameters != null) && (0 <= pIndex) && (pIndex < pParameters.length) ? Objects.toString( pParameters[pIndex] ) : "";
    }

    private static interface AbstractFormatter
    {
        abstract public String format( Object[] pParameters );
    }

    private static class NoSubsFormatter implements AbstractFormatter
    {
        private String mPattern;

        protected NoSubsFormatter( String pPattern )
        {
            mPattern = pPattern;
        }

        @Override
        public String format( Object[] pParameters )
        {
            if ( Objects.isNullOrEmpty( pParameters ) )
            {
                return mPattern;
            }
            StringBuilder sb = new StringBuilder();
            sb.append( getParam( pParameters, 0 ) ).append( ' ' ).append( mPattern );
            for ( int i = 1; i < pParameters.length; i++ )
            {
                sb.append( ' ' ).append( getParam( pParameters, i ) );
            }
            return sb.toString();
        }
    }

    private static class PartsFormatter implements AbstractFormatter
    {
        private Part[] mParts;

        protected PartsFormatter( Part[] pParts )
        {
            mParts = pParts;
        }

        @Override
        public String format( Object[] pParameters )
        {
            StringBuilder sb = new StringBuilder();
            for ( Part part : mParts )
            {
                sb.append( part.get( pParameters ) );
            }
            return sb.toString();
        }
    }

    private interface Part
    {
        String get( Object[] pParameters );
    }

    private static class StringPart implements Part
    {
        private String mString;

        public StringPart( String pString )
        {
            mString = pString;
        }

        @Override
        public String get( Object[] pParameters )
        {
            return mString;
        }
    }

    private static class ParamPart implements Part
    {
        private int mIndex;

        public ParamPart( int pIndex )
        {
            mIndex = pIndex;
        }

        @Override
        public String get( Object[] pParameters )
        {
            return getParam( pParameters, mIndex );
        }
    }

    private static final Map<String, StringPart> sStringParts = new HashMap<String, StringPart>();
    private static final Map<Integer, ParamPart> sParamParts = new HashMap<Integer, ParamPart>();

    private static StringPart getStringPart( String pString )
    {
        StringPart rv = sStringParts.get( pString );
        if ( rv == null )
        {
            sStringParts.put( pString, rv = new StringPart( pString ) );
        }
        return rv;
    }

    private static ParamPart parse( String pIndex )
    {
        try
        {
            Integer index = Integer.valueOf( pIndex );
            ParamPart rv = sParamParts.get( index );
            if ( rv == null )
            {
                sParamParts.put( index, rv = new ParamPart( index ) );
            }
            return rv;
        }
        catch ( NumberFormatException e )
        {
            return null;
        }
    }

    private static AbstractFormatter createFormatter( String pPattern )
    {
        int sAT = pPattern.indexOf( START_SUB );
        int eAT = pPattern.indexOf( END_SUB, sAT + 1 );
        if ( (sAT == -1) || (eAT == -1) )
        {
            return new NoSubsFormatter( pPattern );
        }

        List<Part> zParts = new ArrayList<Part>();

        StringBuilder sb = new StringBuilder();
        do
        {
            sb.append( pPattern.substring( 0, sAT ) );
            String index = pPattern.substring( sAT + 1, eAT );
            ParamPart part = parse( index );
            if ( part == null )
            {
                sb.append( START_SUB ).append( index ).append( END_SUB );
            }
            else
            {
                zParts.add( getStringPart( sb.toString() ) );
                sb = new StringBuilder();
                zParts.add( part );
            }
            pPattern = pPattern.substring( eAT + 1 );
            sAT = pPattern.indexOf( START_SUB );
            eAT = pPattern.indexOf( END_SUB, sAT + 1 );
        }
        while ( (sAT != -1) && (eAT != -1) );

        String restOfString = sb.append( pPattern ).toString();

        if ( zParts.isEmpty() )
        {
            return new NoSubsFormatter( restOfString );
        }

        zParts.add( getStringPart( restOfString ) );

        return new PartsFormatter( zParts.toArray( new Part[zParts.size()] ) );
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/util/SimpleMessage.java

Diff revisions: vs.
Revision Author Commited Message
917 Diff Diff GeorgeS picture GeorgeS Sun 08 Dec, 2013 20:49:56 +0000

1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model!

822 Diff Diff GeorgeS picture GeorgeS Sun 19 Aug, 2012 01:03:51 +0000
809 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:10:46 +0000
50 Diff Diff GeorgeS picture GeorgeS Tue 13 Apr, 2010 11:51:38 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000