Subversion Repository Public Repository

litesoft

Diff Revisions 861 vs 862 for /trunk/Java/core/Anywhere/src/org/litesoft/core/simpletypes/temporal/CalendarYMD.java

Diff revisions: vs.
  @@ -4,8 +4,6 @@
4 4 import java.util.*;
5 5
6 6 import org.litesoft.core.*;
7 - import org.litesoft.core.simpletypes.temporal.nonpublic.*;
8 - import org.litesoft.core.typeutils.*;
9 7 import org.litesoft.core.typeutils.gregorian.*;
10 8
11 9 /**
  @@ -27,12 +25,12 @@
27 25
28 26 public CalendarYMD( int pYear, int pMonth, int pDay )
29 27 {
30 - super(pYear, pMonth, pDay);
28 + super( pYear, pMonth, pDay );
31 29 }
32 30
33 31 public CalendarYMD( CalendarAccessorYMD pAccessor )
34 32 {
35 - this( pAccessor.getYear(),pAccessor.getMonth(), pAccessor.getDay() );
33 + this( pAccessor.getYear(), pAccessor.getMonth(), pAccessor.getDay() );
36 34 }
37 35
38 36 public CalendarYMD( Date pDate )
  @@ -41,6 +39,69 @@
41 39 this( new UtilDateAdaptor( pDate ) );
42 40 }
43 41
42 + @Override
43 + public TemporalResolution getResolution()
44 + {
45 + return TemporalResolution.ToDay;
46 + }
47 +
48 + @Override
49 + public String toString()
50 + {
51 + return YearMonthDayFormatAccessor.get().format( this );
52 + }
53 +
54 + public static String toString( CalendarYMD pDate )
55 + {
56 + return (pDate != null) ? pDate.toString() : null;
57 + }
58 +
59 + public static String toYMD( CalendarYMD pFromYMD )
60 + {
61 + return (pFromYMD != null) ? pFromYMD.toYMD() : null;
62 + }
63 +
64 + public String toYMD()
65 + {
66 + StringBuilder sb = new StringBuilder();
67 + yyyy_Chunk.appendTo( sb, this );
68 + sb.append( '/' );
69 + MM_Chunk.appendTo( sb, this );
70 + sb.append( '/' );
71 + dd_Chunk.appendTo( sb, this );
72 + return sb.toString();
73 + }
74 +
75 + public static CalendarYMD fromYMD( String pToYMD )
76 + throws IllegalArgumentException
77 + {
78 + return fromStringForm( pToYMD, "//" );
79 + }
80 +
81 + public static CalendarYMD fromSQLvalue( String pToSQLvalue )
82 + {
83 + return fromStringForm( pToSQLvalue, "--" );
84 + }
85 +
86 + private static CalendarYMD fromStringForm( String pStringForm, String pSequentialSeperators )
87 + {
88 + if ( pStringForm == null )
89 + {
90 + return null;
91 + }
92 + String[] parts = SimpleParser.parse( pStringForm, pSequentialSeperators, 3 );
93 + return new CalendarYMD( parseInt( parts[0], "Year", pStringForm ), //
94 + parseInt( parts[1], "Month", pStringForm ),
95 + parseInt( parts[2], "Day", pStringForm ) );
96 + }
97 +
98 + public UtilDateAdaptor toUtilDateAdaptor()
99 + {
100 + return new UtilDateAdaptor( getYear(), getMonth(), getDay() );
101 + }
102 +
103 + // vvvvvvvvvvvvvvvvvv Non-common Public methods vvvvvvvvvvvvvvvvv
104 +
44 105 public static CalendarYMD today()
45 106 {
46 107 return new CalendarYMD( new Date() );
  @@ -91,262 +152,55 @@
91 152 *
92 153 * @throws IllegalArgumentException if pThem is null or the resolution of either this' or pThem is not to the Day
93 154 */
94 - public int daysTill( CalendarYMD pThem )
155 + public int daysTill( CalendarAccessorYMD pThem )
95 156 throws IllegalArgumentException
96 157 {
97 - undefinedResultIfNull( "daysTill", pThem ).validateToDay();
98 - validateToDay();
99 - int zThisMinusThem = compareTo( pThem );
100 - if ( zThisMinusThem == 0 )
158 + undefinedResultIfNull( "daysTill", pThem );
159 + int result = compareEm( compare( this.getYear(), pThem.getYear() ),
160 + compare( this.getMonth(), pThem.getMonth() ),
161 + compare( this.getDay(), pThem.getDay() ) );
162 + if ( result == EQUAL )
101 163 {
102 164 return 0;
103 165 }
104 - if ( zThisMinusThem > 0 )
105 - {
106 - return -new DaysDiff( pThem ).till( this );
107 - }
108 - return new DaysDiff( this ).till( pThem );
166 + return ( result == LESSER ) ? new DaysDiff( this ).till( pThem ): -new DaysDiff( pThem ).till( this );
109 167 }
110 168
111 - XXXXX
112 -
113 - /**
114 - * True if this is 'before' pThem.
115 - * <p/>
116 - * Note: Uses the lesser resolution of pThem or this.
117 - *
118 - * @param pThem !null
119 - *
120 - * @throws IllegalArgumentException if pThem is null
121 - */
122 - public boolean before( CalendarYMD pThem )
123 - throws IllegalArgumentException
124 - {
125 - return LLbeforeOrAfter( pThem, "before", -1 );
126 - }
127 -
128 - /**
129 - * True if this is 'after' pThem.
130 - * <p/>
131 - * Note: Uses the lesser resolution of pThem or this.
132 - *
133 - * @param pThem !null
134 - *
135 - * @throws IllegalArgumentException if pThem is null
136 - */
137 - public boolean after( CalendarYMD pThem )
138 - throws IllegalArgumentException
139 - {
140 - return LLbeforeOrAfter( pThem, "after", 1 );
141 - }
169 + // vvvvvvvvvvvvvvvvvv Support methods vvvvvvvvvvvvvvvvv
142 170
143 171 /**
144 - * Number of days from this to pThem (could be negative).
145 - * <p/>
146 - * Note: Treats pThem as if its resolution is to the Day.
147 - *
148 - * @param pThem !null
149 - *
150 - * @throws IllegalArgumentException if pThem is null or this' resolution is not to the Day
172 + * @param pNewYear != current
151 173 */
152 - public int daysTill( Date pThem )
153 - throws IllegalArgumentException
174 + @Override
175 + protected CalendarYMD LLsetYear( int pNewYear )
154 176 {
155 - return daysTill( new CalendarYMD( mDateRes, new UtilDateAdaptor( undefinedResultIfNull( "daysTill", pThem ) ) ) );
177 + Mutable zMutable = new Mutable( pNewYear, getMonth(), getDay(), true );
178 + zMutable.normalize();
179 + return new CalendarYMD( zMutable.getYear(), zMutable.getMonth(), zMutable.getDay() );
156 180 }
157 181
158 182 /**
159 - * True if this is 'after' pThem.
160 - * <p/>
161 - * Note: Treats pThem as if its resolution is the same as this' resolution.
162 - *
163 - * @param pThem !null
164 - *
165 - * @throws IllegalArgumentException if pThem is null
183 + * @param pNewMonth != current
166 184 */
167 - public boolean after( Date pThem )
185 + @Override
186 + protected CalendarYMD LLsetMonth( int pNewMonth )
168 187 throws IllegalArgumentException
169 188 {
170 - return after( new CalendarYMD( mDateRes, new UtilDateAdaptor( undefinedResultIfNull( "after", pThem ) ) ) );
189 + Mutable zMutable = new Mutable( getYear(), pNewMonth, getDay(), true );
190 + zMutable.normalize();
191 + return new CalendarYMD( zMutable.getYear(), zMutable.getMonth(), zMutable.getDay() );
171 192 }
172 193
173 194 /**
174 - * True if this is 'before' pThem.
175 - * <p/>
176 - * Note: Treats pThem as if its resolution is the same as this' resolution.
177 - *
178 - * @param pThem !null
179 - *
180 - * @throws IllegalArgumentException if pThem is null
195 + * @param pNewDay != current
181 196 */
182 - public boolean before( Date pThem )
183 - throws IllegalArgumentException
184 - {
185 - return before( new CalendarYMD( mDateRes, new UtilDateAdaptor( undefinedResultIfNull( "before", pThem ) ) ) );
186 - }
187 -
188 - @Override
189 - public boolean equals( Object o )
190 - {
191 - return (this == o) || //
192 - ((o instanceof CalendarYMD) && equals( (CalendarYMD) o ));
193 - }
194 -
195 - public boolean equals( CalendarYMD them )
196 - {
197 - // DateRes does not need to be explicitly checked as the Month and Day are appropriately effected by DateRes!
198 - return (this == them) || //
199 - ((them != null) //
200 - && equal( this.mYear, them.mYear ) //
201 - && equal( this.mMonth, them.mMonth ) //
202 - && equal( this.mDay, them.mDay ) //
203 - );
204 - }
205 -
206 - @Override
207 - public int hashCode()
208 - {
209 - // DateRes does not need to be explicitly checked as the Month and Day are appropriately effected by DateRes!
210 - return hashCodeEm( calcHashCode( mYear ), //
211 - calcHashCode( mMonth ), //
212 - calcHashCode( mDay ) );
213 - }
214 -
215 - @Override
216 - public int compareTo( CalendarYMD them )
217 - {
218 - // DateRes does not need to be explicitly checked as the Month and Day are appropriately effected by DateRes!
219 - return compareEm( compare( this.mYear, them.mYear ), //
220 - compare( this.mMonth, them.mMonth ), //
221 - compare( this.mDay, them.mDay ) );
222 - }
223 -
224 - public UtilDateAdaptor toUtilDateAdaptor()
225 - {
226 - if ( isValidToDay() )
227 - {
228 - return new UtilDateAdaptor( mYear, mMonth, mDay );
229 - }
230 - if ( isValidToMonth() )
231 - {
232 - return new UtilDateAdaptor( mYear, mMonth, 1 );
233 - }
234 - return new UtilDateAdaptor( mYear, 1, 1 );
235 - }
236 -
237 - public Date toUtilDate()
238 - {
239 - return toUtilDateAdaptor().getWallDate();
240 - }
241 -
242 197 @Override
243 - public String toString()
244 - {
245 - return mDateRes.getDateFormat().format( this );
246 - }
247 -
248 - public static String toString( CalendarYMD pDate )
249 - {
250 - return (pDate != null) ? pDate.toString() : null;
251 - }
252 -
253 - public static String toYMD( CalendarYMD pFromYMD )
254 - {
255 - return (pFromYMD != null) ? pFromYMD.toYMD() : null;
256 - }
257 -
258 - public static CalendarYMD fromYMD( String pToYMD )
198 + protected CalendarYMD LLsetDay( int pNewDay )
259 199 throws IllegalArgumentException
260 200 {
261 - if ( pToYMD == null )
262 - {
263 - return null;
264 - }
265 - String[] parts = SimpleParser.parse( pToYMD, "//", 1 );
266 - switch ( parts.length )
267 - {
268 - case 1:
269 - return new CalendarYMD( parseInt( parts[0], "Year", pToYMD ) );
270 - case 2:
271 - return new CalendarYMD( parseInt( parts[0], "Year", pToYMD ), //
272 - parseInt( parts[1], "Month", pToYMD ) );
273 - case 3:
274 - return new CalendarYMD( parseInt( parts[0], "Year", pToYMD ), //
275 - parseInt( parts[1], "Month", pToYMD ),
276 - parseInt( parts[2], "Day", pToYMD ) );
277 - default:
278 - throw new IllegalStateException( "parts.length = " + parts.length );
279 - }
280 - }
281 -
282 - public String toYMD()
283 - {
284 - StringBuilder sb = new StringBuilder();
285 - yyyy_Chunk.appendTo( sb, this );
286 - if ( isValidToMonth() )
287 - {
288 - sb.append( '/' );
289 - MM_Chunk.appendTo( sb, this );
290 - if ( isValidToDay() )
291 - {
292 - sb.append( '/' );
293 - dd_Chunk.appendTo( sb, this );
294 - }
295 - }
296 - return sb.toString();
297 - }
298 -
299 - @Override
300 - public String toSQLvalue()
301 - {
302 - return toYMD();
303 - }
304 -
305 - public static CalendarYMD fromSQLvalue( String pToSQLvalue )
306 - throws IllegalArgumentException
307 - {
308 - return fromYMD( pToSQLvalue );
309 - }
310 -
311 - // vvvvvvvvvvvvvvvvvv Support methods vvvvvvvvvvvvvvvvv
312 -
313 - private void validateToDay()
314 - {
315 - mDateRes.validateToDay( this );
316 - }
317 -
318 - private CalendarYMD LLsetYear( int pNewYear, boolean pAdjustForward )
319 - {
320 - return (mYear == pNewYear) ? this : new Mutable( this ).setYear( pNewYear, pAdjustForward ).toSimpleDate();
321 - }
322 -
323 - private CalendarYMD LLsetMonth( int pNewMonth, boolean pAdjustForward )
324 - throws IllegalArgumentException
325 - {
326 - mDateRes.validateToMonth( this );
327 - return (mMonth == pNewMonth) ? this : new Mutable( this ).setMonth( pNewMonth, pAdjustForward ).toSimpleDate();
328 - }
329 -
330 - private CalendarYMD LLsetDay( int pDay )
331 - throws IllegalArgumentException
332 - {
333 - mDateRes.validateToDay( this );
334 - return (mDay == pDay) ? this : new Mutable( this ).setDay( pDay ).toSimpleDate();
335 - }
336 -
337 - private boolean LLbeforeOrAfter( CalendarYMD pThem, String pWhat, int pCompareRetValue )
338 - throws IllegalArgumentException
339 - {
340 - undefinedResultIfNull( pWhat, pThem );
341 - if ( (this.mYear == pThem.mYear) && this.isValidToMonth() && pThem.isValidToMonth() )
342 - {
343 - if ( (this.mMonth == pThem.mMonth) && this.isValidToDay() && pThem.isValidToDay() )
344 - {
345 - return pCompareRetValue == compare( this.mDay, pThem.mDay );
346 - }
347 - return pCompareRetValue == compare( this.mMonth, pThem.mMonth );
348 - }
349 - return pCompareRetValue == compare( this.mYear, pThem.mYear );
201 + Mutable zMutable = new Mutable( getYear(), getMonth(), pNewDay, false );
202 + zMutable.normalize();
203 + return new CalendarYMD( zMutable.getYear(), zMutable.getMonth(), zMutable.getDay() );
350 204 }
351 205
352 206 private static class DaysDiff
  @@ -395,173 +249,4 @@
395 249 }
396 250 }
397 251 }
398 -
399 - private static class Mutable implements CalendarAccessorYMD
400 - {
401 - private final DateRes mDateRes;
402 - private int mYear, mMonth, mDay;
403 -
404 - private Mutable( DateRes pDateRes, int pYear, int pMonth, int pDay )
405 - {
406 - mDateRes = pDateRes;
407 - mYear = pYear;
408 - mMonth = pMonth;
409 - mDay = pDay;
410 - normalizeMonthOfYear();
411 - normalizeDayOfMonth();
412 - }
413 -
414 - private Mutable( CalendarYMD pDate )
415 - {
416 - this( pDate.mDateRes, pDate.getYear(), pDate.getMonth(), pDate.getDay() );
417 - }
418 -
419 - @Override
420 - public int getYear()
421 - {
422 - return mYear;
423 - }
424 -
425 - public Mutable setYear( int pYear )
426 - {
427 - return setYear( pYear, (pYear > mYear) );
428 - }
429 -
430 - public Mutable setYear( int pYear, boolean pAdjustForward )
431 - {
432 - mYear = pYear;
433 - return adjustDayOfMonthForMonthOrYearChange( pAdjustForward );
434 - }
435 -
436 - @Override
437 - public int getMonth()
438 - {
439 - return mMonth;
440 - }
441 -
442 - public Mutable setMonth( int pMonth )
443 - {
444 - return setMonth( pMonth, (pMonth > mMonth) );
445 - }
446 -
447 - public Mutable setMonth( int pMonth, boolean pAdjustForward )
448 - {
449 - mMonth = pMonth;
450 - return adjustDayOfMonthForMonthOrYearChange( pAdjustForward );
451 - }
452 -
453 - @Override
454 - public int getDay()
455 - {
456 - return mDay;
457 - }
458 -
459 - public Mutable setDay( int pDay )
460 - {
461 - mDay = pDay;
462 - return normalizeDayOfMonth();
463 - }
464 -
465 - private Mutable normalizeMonthOfYear()
466 - {
467 - if ( mDateRes.isValidToMonth() )
468 - {
469 - while ( mMonth > 12 )
470 - {
471 - mYear++;
472 - mMonth -= 12;
473 - }
474 - while ( mMonth < 1 )
475 - {
476 - mYear--;
477 - mMonth += 12;
478 - }
479 - }
480 - return this;
481 - }
482 -
483 - private Mutable normalizeDayOfMonth()
484 - {
485 - if ( mDateRes.isValidToDay() )
486 - {
487 - while ( mDay < 1 )
488 - {
489 - if ( --mMonth < 1 )
490 - {
491 - mMonth = 12;
492 - mYear--;
493 - }
494 - mDay += Month.daysIn( mYear, mMonth );
495 - }
496 - while ( 28 < mDay )
497 - {
498 - int zDaysInMonth = Month.daysIn( mYear, mMonth );
499 - if ( mDay <= zDaysInMonth )
500 - {
501 - break;
502 - }
503 - mDay -= zDaysInMonth;
504 - if ( 12 < ++mMonth )
505 - {
506 - mMonth = 1;
507 - mYear++;
508 - }
509 - }
510 - }
511 - return this;
512 - }
513 -
514 - private Mutable adjustDayOfMonthForMonthOrYearChange( boolean pAdjustForward )
515 - {
516 - normalizeMonthOfYear();
517 - if ( mDateRes.isValidToDay() ) // if Not, then any year works with any month
518 - {
519 - int maxDaysInMonth = Month.daysIn( mYear, mMonth );
520 - if ( maxDaysInMonth < mDay )
521 - {
522 - mDay = maxDaysInMonth;
523 - if ( pAdjustForward )
524 - {
525 - return addDays( 1 );
526 - }
527 - }
528 - }
529 - return this;
530 - }
531 -
532 - public Mutable addDays( int pDays )
533 - {
534 - mDay += pDays;
535 - for ( int maxDaysInMonth; (maxDaysInMonth = Month.daysIn( mYear, mMonth )) < mDay; )
536 - {
537 - mDay -= maxDaysInMonth;
538 - if ( ++mMonth > 12 )
539 - {
540 - mMonth = 1;
541 - mYear++;
542 - }
543 - }
544 - return this;
545 - }
546 -
547 - public Mutable minusDays( int pDays )
548 - {
549 - mDay -= pDays;
550 - while ( mDay < 1 )
551 - {
552 - if ( --mMonth < 1 )
553 - {
554 - mMonth = 12;
555 - mYear--;
556 - }
557 - mDay += Month.daysIn( mYear, mMonth );
558 - }
559 - return this;
560 - }
561 -
562 - public CalendarYMD toSimpleDate()
563 - {
564 - return new CalendarYMD( mDateRes, this );
565 - }
566 - }
567 252 }