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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package org.litesoft.core.simpletypes.temporal.nonpublic;

import java.io.*;

import org.litesoft.core.simpletypes.nonpublic.*;

public abstract class CalendarSupport<T extends CalendarSupport> extends CompareSupport<T> implements Serializable
{
    public static final String JAN = "January";
    public static final String FEB = "Febuary";
    public static final String MAR = "March";
    public static final String APR = "April";
    public static final String MAY = "May";
    public static final String JUN = "June";
    public static final String JUL = "July";
    public static final String AUG = "August";
    public static final String SEP = "September";
    public static final String OCT = "October";
    public static final String NOV = "November";
    public static final String Dec = "December";

    protected static final String[] MONTH_NAMES = new String[] //
            { //
              "---", // 0 - Text required for Chunking (see below)
              JAN, //  1
              FEB, //  2
              MAR, //  3
              APR, //  4
              MAY, //  5
              JUN, //  6
              JUL, //  7
              AUG, //  8
              SEP, //  9
              OCT, // 10
              NOV, // 11
              Dec, // 12
            };

    protected static final int[] MONTH_DAYS = new int[] //
            { //
              0, //   0
              31, //  1 - Jan
              0, //   2 - Feb: 28/29
              31, //  3 - Mar
              30, //  4 - Apr
              31, //  5 - May
              30, //  6 - Jun
              31, //  7 - Jul
              31, //  8 - Aug
              30, //  9 - Sep
              31, // 10 - Oct
              30, // 11 - Nov
              31, // 12 - Dec
            };

    protected static final String[] MONTH_NAMES_ALL_CAPS = new String[] //
            { //
              "---", //              0 - Text required for Chunking (see below)
              JAN.toUpperCase(), //  1
              FEB.toUpperCase(), //  2
              MAR.toUpperCase(), //  3
              APR.toUpperCase(), //  4
              MAY.toUpperCase(), //  5
              JUN.toUpperCase(), //  6
              JUL.toUpperCase(), //  7
              AUG.toUpperCase(), //  8
              SEP.toUpperCase(), //  9
              OCT.toUpperCase(), // 10
              NOV.toUpperCase(), // 11
              Dec.toUpperCase(), // 12
            };

    public CalendarSupport()
    {
    }

    /**
     * True if pYear is thought to be a Leap Year (based on the rules that apply from 1800 thru 2100 in the "Gregorian" calendar).
     * <p/>
     * Note: if pYear is before 1800 or after 2010 or are not using a "Gregorian" calendar, then you may get incorrect results!
     * <p/>
     * Rules for 1800 - 2010:
     * <ul>
     * 1) Every 4 years is a Leap Year, except
     * 2) Every 100 years is NOT a Leap Year, except
     * 3) Every 400 years is a Leap year.
     * <ul/>
     * Hence:
     * The following are Leap Years: 1800, 1804, 1896, 1900, 1904, 1996, 2004, 2096, 2100, 2104
     * The following are NOT Leap Years: 1801, 1903, 1998, 2000, 2002
     */
    public static boolean isLeapYear( int pYear )
    {
        if ( 0 != (pYear & 3) ) // Every 4
        {
            return false;
        }
        //noinspection SimplifiableIfStatement
        if ( 0 != (pYear % 100) ) // ! Every 100
        {
            return true;
        }
        return (0 == (pYear % 400)); // Every 400
    }

    /**
     * Days in specified Year & Month (or 0 if the month is invalid).
     * <p/>
     * Only guaranteed to produce valid results in the "Gregorian" calendar from 1800 thru 2010.
     * <p/>
     * Note: if pYear is before 1800 or after 2010, then you may get incorrect results!
     */
    public static int daysInMonth( int pYear, int pMonth )
    {
        if ( (pMonth < 1) || (12 < pMonth) )
        {
            return 0;
        }
        return (pMonth != 2) ? MONTH_DAYS[pMonth] : isLeapYear( pYear ) ? 29 : 28;
    }

    /**
     * Returns null if a valid date, or error text if not!
     */
    public static String validDate( int pYear, int pMonth, int pDay )
    {
        int maxDaysInMonth = daysInMonth( pYear, pMonth );
        if ( maxDaysInMonth == 0 )
        {
            return "Invalid month: " + pMonth;
        }
        if ( pDay < 1 )
        {
            return "Invalid day: " + pDay;
        }
        if ( maxDaysInMonth < pDay )
        {
            return MONTH_NAMES[pMonth] + " " + pYear + " only has " + maxDaysInMonth + ", attempted to set days to: " + pDay;
        }
        return null;
    }

    public static int parseInt( String pToParse, String pWhat, String pFrom )
            throws IllegalArgumentException
    {
        pToParse = pToParse.trim();
        if ( pToParse.length() == 0 )
        {
            throw new IllegalArgumentException( "No " + pWhat + " in '" + pFrom + "'" );
        }
        try
        {
            return Integer.parseInt( pToParse );
        }
        catch ( NumberFormatException e )
        {
            throw new IllegalArgumentException( "Invalid " + pWhat + " '" + pToParse + "' in '" + pFrom + "': " + e.getMessage() );
        }
    }

    public static int parseMonth( String pToParse, String pFrom )
            throws IllegalArgumentException
    {
        pToParse = pToParse.trim().toUpperCase();
        if ( pToParse.length() == 0 )
        {
            throw new IllegalArgumentException( "No Month in '" + pFrom + "'" );
        }
        int zLastMatch = 0;
        int zMatchedCount = 0;
        String zMatches = "";
        for ( int i = 1; i < MONTH_NAMES_ALL_CAPS.length; i++ )
        {
            String monthName = MONTH_NAMES_ALL_CAPS[i];
            if ( monthName.startsWith( pToParse ) )
            {
                zMatches += ", " + MONTH_NAMES[zLastMatch = i];
                zMatchedCount++;
            }
        }
        String zMessage;
        switch ( zMatchedCount )
        {
            case 1:
                return zLastMatch;
            case 0:
                zMessage = "";
                break;
            default:
                zMessage = ": Matched-" + zMatches.substring( 2 );
                break;
        }
        throw new IllegalArgumentException( "Invalid Month '" + pToParse + "' in '" + pFrom + "'" + zMessage );
    }

    protected static void undefinedResultIfNull( String pWhat, Object pToCheck )
            throws IllegalArgumentException
    {
        if ( pToCheck == null )
        {
            throw new IllegalArgumentException( "Result is undefined for: " + pWhat + "( null )" );
        }
    }

    protected static void LLcheckMonth( int pMonth )
    {
        if ( (pMonth < 1) || (12 < pMonth) )
        {
            throw new IllegalArgumentException( "Invalid Month provided: " + pMonth );
        }
    }

    protected static void LLcheckDay( int pDay )
    {
        if ( (pDay < 1) || (31 < pDay) )
        {
            throw new IllegalArgumentException( "Invalid Day provided: " + pDay );
        }
    }

    protected int validate( String pWhat, int pNewValue, int pMaxValue )
            throws IllegalArgumentException
    {
        if ( (pNewValue < 0) || (pMaxValue < pNewValue) )
        {
            throw new IllegalArgumentException( "The Time '" + pWhat + "' field only accepts values between 0 and " + pMaxValue + " inclusive, attemted to set it to '" + pNewValue + "', current value is: " + this );
        }
        return pNewValue;
    }

    protected static String minLength( int pMinLength, String pValue )
    {
        while ( pValue.length() < pMinLength )
        {
            pValue = "0" + pValue;
        }
        return pValue;
    }

    public interface Chunk // Marker
    {
    }

    protected static class ProxyLeadZeroMinLengthChunk implements Chunk
    {
        private Chunk mChunk;
        private int mMinLength;

        public ProxyLeadZeroMinLengthChunk( Chunk pChunk, int pMinLength )
        {
            mChunk = pChunk;
            mMinLength = pMinLength;
        }

        public String toString()
        {
            return minLength( mMinLength, mChunk.toString() );
        }
    }

    protected static class LiteralChunk implements Chunk
    {
        private String mLiteral;

        public LiteralChunk( String pLiteral )
        {
            mLiteral = pLiteral;
        }

        public String toString()
        {
            return mLiteral;
        }
    }

    protected static abstract class YMDaccessorChunk implements Chunk
    {
        protected YMDaccessor mYMDaccessor;

        protected YMDaccessorChunk( YMDaccessor pYMDaccessor )
        {
            mYMDaccessor = pYMDaccessor;
        }
    }

    protected static class M_Chunk extends YMDaccessorChunk
    {
        public M_Chunk( YMDaccessor pYMDaccessor )
        {
            super( pYMDaccessor );
        }

        public String toString()
        {
            return Integer.toString( mYMDaccessor.getMonth() );
        }
    }

    protected static class MM_Chunk extends ProxyLeadZeroMinLengthChunk
    {
        public MM_Chunk( YMDaccessor pYMDaccessor )
        {
            super( new M_Chunk( pYMDaccessor ), 2 );
        }
    }

    protected static class MMMM_Chunk extends YMDaccessorChunk
    {
        public MMMM_Chunk( YMDaccessor pYMDaccessor )
        {
            super( pYMDaccessor );
        }

        public String toString()
        {
            return MONTH_NAMES[mYMDaccessor.getMonth()];
        }
    }

    protected static class MMM_Chunk extends MMMM_Chunk
    {
        public MMM_Chunk( YMDaccessor pYMDaccessor )
        {
            super( pYMDaccessor );
        }

        public String toString()
        {
            return super.toString().substring( 0, 3 );
        }
    }

    protected static class y_Chunk extends YMDaccessorChunk
    {
        public y_Chunk( YMDaccessor pYMDaccessor )
        {
            super( pYMDaccessor );
        }

        public String toString()
        {
            return Integer.toString( mYMDaccessor.getYear() );
        }
    }

    protected static class yy_Chunk extends y_Chunk
    {
        public yy_Chunk( YMDaccessor pYMDaccessor )
        {
            super( pYMDaccessor );
        }

        public String toString()
        {
            String s = super.toString();
            return (s.length() == 1) ? "0" + s : s.substring( s.length() - 2 );
        }
    }

    protected static class yyy_Chunk extends y_Chunk
    {
        private int mMinLength;

        public yyy_Chunk( YMDaccessor pYMDaccessor, int pMinLength )
        {
            super( pYMDaccessor );
            mMinLength = pMinLength;
        }

        public String toString()
        {
            return minLength( mMinLength, super.toString() );
        }
    }

    protected static class d_Chunk extends YMDaccessorChunk
    {
        public d_Chunk( YMDaccessor pYMDaccessor )
        {
            super( pYMDaccessor );
        }

        public String toString()
        {
            return Integer.toString( mYMDaccessor.getDay() );
        }
    }

    protected static class dd_Chunk extends ProxyLeadZeroMinLengthChunk
    {
        public dd_Chunk( YMDaccessor pYMDaccessor )
        {
            super( new d_Chunk( pYMDaccessor ), 2 );
        }
    }

    protected static LiteralChunk SLASH = new LiteralChunk( "/" );
    protected static LiteralChunk SPACE = new LiteralChunk( " " );
    protected static LiteralChunk COLON = new LiteralChunk( ":" );
    protected static LiteralChunk COMMA = new LiteralChunk( "," );
    protected static LiteralChunk DASH = new LiteralChunk( "-" );
}

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

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