Subversion Repository Public Repository

litesoft

Diff Revisions 50 vs 851 for /trunk/Java/core/Anywhere/src/org/litesoft/core/simpletypes/temporal/nonpublic/CalendarSupport.java

Diff revisions: vs.
  @@ -4,143 +4,12 @@
4 4 import java.io.*;
5 5
6 6 import org.litesoft.core.simpletypes.nonpublic.*;
7 + import org.litesoft.core.simpletypes.temporal.*;
8 + import org.litesoft.core.typeutils.*;
9 + import org.litesoft.core.typeutils.gregorian.*;
7 10
8 11 public abstract class CalendarSupport<T extends CalendarSupport> extends CompareSupport<T> implements Serializable
9 12 {
10 - public static final String JAN = "January";
11 - public static final String FEB = "Febuary";
12 - public static final String MAR = "March";
13 - public static final String APR = "April";
14 - public static final String MAY = "May";
15 - public static final String JUN = "June";
16 - public static final String JUL = "July";
17 - public static final String AUG = "August";
18 - public static final String SEP = "September";
19 - public static final String OCT = "October";
20 - public static final String NOV = "November";
21 - public static final String Dec = "December";
22 -
23 - protected static final String[] MONTH_NAMES = new String[] //
24 - { //
25 - "---", // 0 - Text required for Chunking (see below)
26 - JAN, // 1
27 - FEB, // 2
28 - MAR, // 3
29 - APR, // 4
30 - MAY, // 5
31 - JUN, // 6
32 - JUL, // 7
33 - AUG, // 8
34 - SEP, // 9
35 - OCT, // 10
36 - NOV, // 11
37 - Dec, // 12
38 - };
39 -
40 - protected static final int[] MONTH_DAYS = new int[] //
41 - { //
42 - 0, // 0
43 - 31, // 1 - Jan
44 - 0, // 2 - Feb: 28/29
45 - 31, // 3 - Mar
46 - 30, // 4 - Apr
47 - 31, // 5 - May
48 - 30, // 6 - Jun
49 - 31, // 7 - Jul
50 - 31, // 8 - Aug
51 - 30, // 9 - Sep
52 - 31, // 10 - Oct
53 - 30, // 11 - Nov
54 - 31, // 12 - Dec
55 - };
56 -
57 - protected static final String[] MONTH_NAMES_ALL_CAPS = new String[] //
58 - { //
59 - "---", // 0 - Text required for Chunking (see below)
60 - JAN.toUpperCase(), // 1
61 - FEB.toUpperCase(), // 2
62 - MAR.toUpperCase(), // 3
63 - APR.toUpperCase(), // 4
64 - MAY.toUpperCase(), // 5
65 - JUN.toUpperCase(), // 6
66 - JUL.toUpperCase(), // 7
67 - AUG.toUpperCase(), // 8
68 - SEP.toUpperCase(), // 9
69 - OCT.toUpperCase(), // 10
70 - NOV.toUpperCase(), // 11
71 - Dec.toUpperCase(), // 12
72 - };
73 -
74 - public CalendarSupport()
75 - {
76 - }
77 -
78 - /**
79 - * True if pYear is thought to be a Leap Year (based on the rules that apply from 1800 thru 2100 in the "Gregorian" calendar).
80 - * <p/>
81 - * Note: if pYear is before 1800 or after 2010 or are not using a "Gregorian" calendar, then you may get incorrect results!
82 - * <p/>
83 - * Rules for 1800 - 2010:
84 - * <ul>
85 - * 1) Every 4 years is a Leap Year, except
86 - * 2) Every 100 years is NOT a Leap Year, except
87 - * 3) Every 400 years is a Leap year.
88 - * <ul/>
89 - * Hence:
90 - * The following are Leap Years: 1800, 1804, 1896, 1900, 1904, 1996, 2004, 2096, 2100, 2104
91 - * The following are NOT Leap Years: 1801, 1903, 1998, 2000, 2002
92 - */
93 - public static boolean isLeapYear( int pYear )
94 - {
95 - if ( 0 != (pYear & 3) ) // Every 4
96 - {
97 - return false;
98 - }
99 - //noinspection SimplifiableIfStatement
100 - if ( 0 != (pYear % 100) ) // ! Every 100
101 - {
102 - return true;
103 - }
104 - return (0 == (pYear % 400)); // Every 400
105 - }
106 -
107 - /**
108 - * Days in specified Year & Month (or 0 if the month is invalid).
109 - * <p/>
110 - * Only guaranteed to produce valid results in the "Gregorian" calendar from 1800 thru 2010.
111 - * <p/>
112 - * Note: if pYear is before 1800 or after 2010, then you may get incorrect results!
113 - */
114 - public static int daysInMonth( int pYear, int pMonth )
115 - {
116 - if ( (pMonth < 1) || (12 < pMonth) )
117 - {
118 - return 0;
119 - }
120 - return (pMonth != 2) ? MONTH_DAYS[pMonth] : isLeapYear( pYear ) ? 29 : 28;
121 - }
122 -
123 - /**
124 - * Returns null if a valid date, or error text if not!
125 - */
126 - public static String validDate( int pYear, int pMonth, int pDay )
127 - {
128 - int maxDaysInMonth = daysInMonth( pYear, pMonth );
129 - if ( maxDaysInMonth == 0 )
130 - {
131 - return "Invalid month: " + pMonth;
132 - }
133 - if ( pDay < 1 )
134 - {
135 - return "Invalid day: " + pDay;
136 - }
137 - if ( maxDaysInMonth < pDay )
138 - {
139 - return MONTH_NAMES[pMonth] + " " + pYear + " only has " + maxDaysInMonth + ", attempted to set days to: " + pDay;
140 - }
141 - return null;
142 - }
143 -
144 13 public static int parseInt( String pToParse, String pWhat, String pFrom )
145 14 throws IllegalArgumentException
146 15 {
  @@ -159,41 +28,6 @@
159 28 }
160 29 }
161 30
162 - public static int parseMonth( String pToParse, String pFrom )
163 - throws IllegalArgumentException
164 - {
165 - pToParse = pToParse.trim().toUpperCase();
166 - if ( pToParse.length() == 0 )
167 - {
168 - throw new IllegalArgumentException( "No Month in '" + pFrom + "'" );
169 - }
170 - int zLastMatch = 0;
171 - int zMatchedCount = 0;
172 - String zMatches = "";
173 - for ( int i = 1; i < MONTH_NAMES_ALL_CAPS.length; i++ )
174 - {
175 - String monthName = MONTH_NAMES_ALL_CAPS[i];
176 - if ( monthName.startsWith( pToParse ) )
177 - {
178 - zMatches += ", " + MONTH_NAMES[zLastMatch = i];
179 - zMatchedCount++;
180 - }
181 - }
182 - String zMessage;
183 - switch ( zMatchedCount )
184 - {
185 - case 1:
186 - return zLastMatch;
187 - case 0:
188 - zMessage = "";
189 - break;
190 - default:
191 - zMessage = ": Matched-" + zMatches.substring( 2 );
192 - break;
193 - }
194 - throw new IllegalArgumentException( "Invalid Month '" + pToParse + "' in '" + pFrom + "'" + zMessage );
195 - }
196 -
197 31 protected static void undefinedResultIfNull( String pWhat, Object pToCheck )
198 32 throws IllegalArgumentException
199 33 {
  @@ -203,20 +37,22 @@
203 37 }
204 38 }
205 39
206 - protected static void LLcheckMonth( int pMonth )
40 + protected static int LLcheckMonth( int pYear, int pMonth )
207 41 {
208 42 if ( (pMonth < 1) || (12 < pMonth) )
209 43 {
210 - throw new IllegalArgumentException( "Invalid Month provided: " + pMonth );
44 + throw new IllegalArgumentException( "Invalid Month provided (Year: " + pYear + "): " + pMonth );
211 45 }
46 + return pMonth;
212 47 }
213 48
214 - protected static void LLcheckDay( int pDay )
49 + protected static int LLcheckDay( int pYear, int pMonth, int pDay )
215 50 {
216 - if ( (pDay < 1) || (31 < pDay) )
51 + if ( (pDay < 1) || (Month.daysIn( pYear, pMonth ) < pDay) )
217 52 {
218 - throw new IllegalArgumentException( "Invalid Day provided: " + pDay );
53 + throw new IllegalArgumentException( "Invalid Day provided (Year: " + pYear + ", Month: " + pMonth + "): " + pDay );
219 54 }
55 + return pDay;
220 56 }
221 57
222 58 protected int validate( String pWhat, int pNewValue, int pMaxValue )
  @@ -224,7 +60,9 @@
224 60 {
225 61 if ( (pNewValue < 0) || (pMaxValue < pNewValue) )
226 62 {
227 - 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 );
63 + throw new IllegalArgumentException(
64 + "The Time '" + pWhat + "' field only accepts values between 0 and " + pMaxValue + " inclusive, attemted to set it to '" + pNewValue +
65 + "', current value is: " + this );
228 66 }
229 67 return pNewValue;
230 68 }
  @@ -238,29 +76,12 @@
238 76 return pValue;
239 77 }
240 78
241 - public interface Chunk // Marker
79 + public interface Chunk
242 80 {
81 + void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource );
243 82 }
244 83
245 - protected static class ProxyLeadZeroMinLengthChunk implements Chunk
246 - {
247 - private Chunk mChunk;
248 - private int mMinLength;
249 -
250 - public ProxyLeadZeroMinLengthChunk( Chunk pChunk, int pMinLength )
251 - {
252 - mChunk = pChunk;
253 - mMinLength = pMinLength;
254 - }
255 -
256 - @Override
257 - public String toString()
258 - {
259 - return minLength( mMinLength, mChunk.toString() );
260 - }
261 - }
262 -
263 - protected static class LiteralChunk implements Chunk
84 + private static final class LiteralChunk implements Chunk
264 85 {
265 86 private String mLiteral;
266 87
  @@ -270,143 +91,115 @@
270 91 }
271 92
272 93 @Override
273 - public String toString()
94 + public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource )
274 95 {
275 - return mLiteral;
96 + pSB.append( mLiteral );
276 97 }
277 98 }
278 99
279 - protected static abstract class YMDaccessorChunk implements Chunk
280 - {
281 - protected YMDaccessor mYMDaccessor;
100 + protected static final Chunk SLASH = new LiteralChunk( "/" );
101 + protected static final Chunk SPACE = new LiteralChunk( " " );
102 + protected static final Chunk COLON = new LiteralChunk( ":" );
103 + protected static final Chunk COMMA = new LiteralChunk( "," );
104 + protected static final Chunk DASH = new LiteralChunk( "-" );
282 105
283 - protected YMDaccessorChunk( YMDaccessor pYMDaccessor )
284 - {
285 - mYMDaccessor = pYMDaccessor;
286 - }
287 - }
288 -
289 - protected static class M_Chunk extends YMDaccessorChunk
106 + protected static final Chunk d_Chunk = new Chunk()
290 107 {
291 - public M_Chunk( YMDaccessor pYMDaccessor )
292 - {
293 - super( pYMDaccessor );
294 - }
295 -
296 108 @Override
297 - public String toString()
109 + public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource )
298 110 {
299 - return Integer.toString( mYMDaccessor.getMonth() );
111 + pSB.append( pYMDsource.getDay() );
300 112 }
301 - }
113 + };
302 114
303 - protected static class MM_Chunk extends ProxyLeadZeroMinLengthChunk
115 + protected static final Chunk dd_Chunk = new Chunk()
304 116 {
305 - public MM_Chunk( YMDaccessor pYMDaccessor )
117 + @Override
118 + public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource )
306 119 {
307 - super( new M_Chunk( pYMDaccessor ), 2 );
120 + pSB.append( Integers.zeroPadIt( pYMDsource.getDay(), 2 ) );
308 121 }
309 - }
122 + };
310 123
311 - protected static class MMMM_Chunk extends YMDaccessorChunk
124 + protected static final Chunk M_Chunk = new Chunk()
312 125 {
313 - public MMMM_Chunk( YMDaccessor pYMDaccessor )
314 - {
315 - super( pYMDaccessor );
316 - }
317 -
318 126 @Override
319 - public String toString()
127 + public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource )
320 128 {
321 - return MONTH_NAMES[mYMDaccessor.getMonth()];
129 + pSB.append( pYMDsource.getMonth() );
322 130 }
323 - }
131 + };
324 132
325 - protected static class MMM_Chunk extends MMMM_Chunk
133 + protected static final Chunk MM_Chunk = new Chunk()
326 134 {
327 - public MMM_Chunk( YMDaccessor pYMDaccessor )
328 - {
329 - super( pYMDaccessor );
330 - }
331 -
332 135 @Override
333 - public String toString()
136 + public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource )
334 137 {
335 - return super.toString().substring( 0, 3 );
138 + pSB.append( Integers.zeroPadIt( pYMDsource.getMonth(), 2 ) );
336 139 }
337 - }
140 + };
338 141
339 - protected static class y_Chunk extends YMDaccessorChunk
142 + protected static final Chunk MMMM_Chunk = new Chunk()
340 143 {
341 - public y_Chunk( YMDaccessor pYMDaccessor )
342 - {
343 - super( pYMDaccessor );
344 - }
345 -
346 144 @Override
347 - public String toString()
145 + public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource )
348 146 {
349 - return Integer.toString( mYMDaccessor.getYear() );
147 + pSB.append( Month.nameFromMonthNumber( pYMDsource.getMonth() ) );
350 148 }
351 - }
149 + };
352 150
353 - protected static class yy_Chunk extends y_Chunk
151 + protected static final Chunk MMM_Chunk = new Chunk()
354 152 {
355 - public yy_Chunk( YMDaccessor pYMDaccessor )
356 - {
357 - super( pYMDaccessor );
358 - }
359 -
360 153 @Override
361 - public String toString()
154 + public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource )
362 155 {
363 - String s = super.toString();
364 - return (s.length() == 1) ? "0" + s : s.substring( s.length() - 2 );
156 + pSB.append( Month.shortNameFromMonthNumber( pYMDsource.getMonth() ) );
365 157 }
366 - }
158 + };
367 159
368 - protected static class yyy_Chunk extends y_Chunk
160 + protected static final Chunk y_Chunk = new Chunk()
369 161 {
370 - private int mMinLength;
371 -
372 - public yyy_Chunk( YMDaccessor pYMDaccessor, int pMinLength )
373 - {
374 - super( pYMDaccessor );
375 - mMinLength = pMinLength;
376 - }
377 -
378 162 @Override
379 - public String toString()
163 + public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource )
380 164 {
381 - return minLength( mMinLength, super.toString() );
165 + pSB.append( pYMDsource.getYear() );
382 166 }
383 - }
167 + };
384 168
385 - protected static class d_Chunk extends YMDaccessorChunk
169 + protected static final Chunk yy_Chunk = new Chunk()
386 170 {
387 - public d_Chunk( YMDaccessor pYMDaccessor )
171 + @Override
172 + public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource )
388 173 {
389 - super( pYMDaccessor );
174 + String zYear = Integers.zeroPadIt( pYMDsource.getYear(), 2 );
175 + pSB.append( (zYear.length() == 2) ? zYear : zYear.substring( zYear.length() - 2 ) );
390 176 }
177 + };
391 178
179 + protected static final Chunk yyyy_Chunk = new Chunk()
180 + {
392 181 @Override
393 - public String toString()
182 + public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource )
394 183 {
395 - return Integer.toString( mYMDaccessor.getDay() );
184 + String zYear = Integers.zeroPadIt( pYMDsource.getYear(), 4 );
185 + pSB.append( (zYear.length() == 4) ? zYear : zYear.substring( zYear.length() - 4 ) );
396 186 }
397 - }
187 + };
398 188
399 - protected static class dd_Chunk extends ProxyLeadZeroMinLengthChunk
189 + protected static Chunk yyy_Chunk( final int pMinLength )
400 190 {
401 - public dd_Chunk( YMDaccessor pYMDaccessor )
191 + return new Chunk()
402 192 {
403 - super( new d_Chunk( pYMDaccessor ), 2 );
404 - }
193 + @Override
194 + public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource )
195 + {
196 + pSB.append( Integers.zeroPadIt( pYMDsource.getYear(), pMinLength ) );
197 + }
198 + };
405 199 }
406 200
407 - protected static LiteralChunk SLASH = new LiteralChunk( "/" );
408 - protected static LiteralChunk SPACE = new LiteralChunk( " " );
409 - protected static LiteralChunk COLON = new LiteralChunk( ":" );
410 - protected static LiteralChunk COMMA = new LiteralChunk( "," );
411 - protected static LiteralChunk DASH = new LiteralChunk( "-" );
201 + protected static Chunk literalChunk( String pLiteral )
202 + {
203 + return new LiteralChunk( pLiteral );
204 + }
412 205 }