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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package org.litesoft.core.simpletypes.temporal.nonpublic;

import org.litesoft.core.simpletypes.temporal.*;
import org.litesoft.core.util.*;

public interface TemporalParsingSupport
{
    public static final int BUCKET_FACTOR = 256;

    public static final int OPT_YEAR = 1;
    public static final int OPT_MONTH = 2;
    public static final int OPT_DAY = 4;
    public static final int IS_MONTH = 8;

    public static final int B0_OPT_YEAR = OPT_YEAR;
    public static final int B0_OPT_YEARorDAY = OPT_YEAR + OPT_DAY;
    public static final int B0_OPT_YEARorMONTHorDAY = OPT_YEAR + OPT_MONTH + OPT_DAY;
    public static final int B0_IS_MONTH = IS_MONTH;

    public static final int B1_OPT_YEAR = B0_OPT_YEAR << 8;
    public static final int B1_OPT_YEARorDAY = B0_OPT_YEARorDAY << 8;
    public static final int B1_OPT_YEARorMONTHorDAY = B0_OPT_YEARorMONTHorDAY << 8;
    public static final int B1_IS_MONTH = B0_IS_MONTH << 8;

    public static final int B2_OPT_YEAR = B1_OPT_YEAR << 8;
    public static final int B2_OPT_YEARorDAY = B1_OPT_YEARorDAY << 8;
    public static final int B2_OPT_YEARorMONTHorDAY = B1_OPT_YEARorMONTHorDAY << 8;
    public static final int B2_IS_MONTH = B1_IS_MONTH << 8;

    public interface PartCollector
    {
        public boolean accept( char pChar );

        public boolean isReal();

        public void validate( String pFromText );
    }

    public static class SeparatorPC implements PartCollector
    {
        public static final PartCollector INSTANCE = new SeparatorPC();

        public boolean accept( char pChar )
        {
            return !UtilsCommon.isAlphaNumeric( pChar );
        }

        public boolean isReal()
        {
            return false;
        }

        public void validate( String pFromText )
        {
        }

        public String toString()
        {
            return "'sep'";
        }
    }

    public static abstract class AbstractRealPC implements PartCollector
    {
        protected StringBuilder mSB = new StringBuilder();

        protected AbstractRealPC( char pChar )
        {
            mSB.append( pChar );
        }

        protected AbstractRealPC( String pText )
        {
            mSB.append( pText );
        }

        public boolean accept( char pChar )
        {
            boolean rv = isAcceptable( pChar );
            if ( rv )
            {
                mSB.append( pChar );
            }
            return rv;
        }

        public boolean isReal()
        {
            return true;
        }

        abstract protected boolean isAcceptable( char pChar );

        abstract public boolean isMonth();

        abstract public boolean isCouldBeDay();

        abstract public boolean isCouldBeMonth();

        abstract public int getDay();

        abstract public int getMonth();

        abstract public int getYear();

        abstract public int getNumericValue();

        abstract public int getFlags();

        abstract public void validate( String pFromText, String pWhat, int pMaxValue );

        public String toString()
        {
            return mSB.toString();
        }
    }

    public static class StringPC extends AbstractRealPC
    {
        public StringPC( String pText )
        {
            super( pText );
        }

        public StringPC( char pChar )
        {
            super( pChar );
        }

        protected boolean isAcceptable( char pChar )
        {
            return UtilsCommon.isAlpha( pChar );
        }

        public void validate( String pFromText )
        {
        }

        public void validate( String pFromText, String pWhat, int pMaxValue )
        {
            throw new IllegalArgumentException( pWhat + " from '" + pFromText + "' not numeric" );
        }

        public boolean isMonth()
        {
            return false;
        }

        public boolean isCouldBeDay()
        {
            return false;
        }

        public boolean isCouldBeMonth()
        {
            return false;
        }

        public int getDay()
        {
            return 0;
        }

        public int getMonth()
        {
            return 0;
        }

        public int getYear()
        {
            return 0;
        }

        public int getNumericValue()
        {
            return 0;
        }

        public int getFlags()
        {
            return 0;
        }

    }

    public static class MonthAsTextPC extends StringPC
    {
        protected int mValue;

        public MonthAsTextPC( String pText )
        {
            super( pText );
        }

        public MonthAsTextPC( char pChar )
        {
            super( pChar );
        }

        public void validate( String pFromText )
        {
            mValue = CalendarSupport.parseMonth( mSB.toString(), pFromText );
        }

        public int getNumericValue()
        {
            return mValue;
        }

        public int getMonth()
        {
            return mValue;
        }

        public boolean isMonth()
        {
            return true;
        }

        public boolean isCouldBeMonth()
        {
            return true;
        }

        public int getFlags()
        {
            return IS_MONTH;
        }
    }

    public static class NumericPC extends AbstractRealPC
    {
        protected boolean mCouldBeDay, mCouldBeMonth;
        protected int mDay, mMonth, mYear, mValue;

        public NumericPC( String pText )
        {
            super( pText );
        }

        public NumericPC( char pChar )
        {
            super( pChar );
        }

        protected boolean isAcceptable( char pChar )
        {
            return UtilsCommon.isNumeric( pChar );
        }

        public void validate( String pFromText )
        {
            String s = mSB.toString();
            int i = mYear = mValue = Integer.parseInt( s );
            if ( s.length() <= 2 )
            {
                window2digitYear();
                if ( i > 0 )
                {
                    if ( i <= 31 )
                    {
                        mDay = i;
                        mCouldBeDay = true;
                        if ( i <= 12 )
                        {
                            mMonth = i;
                            mCouldBeMonth = true;
                        }
                    }
                }
            }
        }

        private void window2digitYear()
        {
            int thisYear = new UtilDateAdaptor().getYear();
            mYear += (thisYear / 100) * 100;
            if ( mYear > (thisYear + 10) )
            {
                mYear -= 100;
            }
            else if ( mYear < (thisYear - 90) )
            {
                mYear += 100;
            }
        }

        public void validate( String pFromText, String pWhat, int pMaxValue )
        {
            if ( mValue > pMaxValue )
            {
                throw new IllegalArgumentException(
                        pWhat + "(" + mValue + ") from '" + pFromText + "' greater than " + pMaxValue );
            }
        }

        public boolean isMonth()
        {
            return false;
        }

        public boolean isCouldBeDay()
        {
            return mCouldBeDay;
        }

        public boolean isCouldBeMonth()
        {
            return mCouldBeMonth;
        }

        public int getDay()
        {
            return mDay;
        }

        public int getMonth()
        {
            return mMonth;
        }

        public int getYear()
        {
            return mYear;
        }

        public int getNumericValue()
        {
            return mValue;
        }

        public int getFlags()
        {
            int rv = OPT_YEAR;
            if ( isCouldBeMonth() )
            {
                rv += OPT_MONTH;
            }
            if ( isCouldBeDay() )
            {
                rv += OPT_DAY;
            }
            return rv;
        }
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/simpletypes/temporal/nonpublic/TemporalParsingSupport.java

Diff revisions: vs.
Revision Author Commited Message
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000