Subversion Repository Public Repository

litesoft

Diff Revisions 852 vs 853 for /trunk/Java/core/Anywhere/src/org/litesoft/core/simpletypes/temporal/SimpleDate.java

Diff revisions: vs.
  @@ -10,18 +10,10 @@
10 10
11 11 /**
12 12 * A Simple date that acts as a proxy for a java.util.Date (burying the deprecated method use) providing
13 - * varying levels of resolution based on the Format String (which supports formatting similar to SimpledateFormat).
13 + * varying levels of resolution based on the DateRes.
14 14 * <p/>
15 - * <pre>
16 - * y Year - YY (1996 becomes 96),
17 - * otherwise it is the complete Year, eg: 1996, 2001, 10050.
18 - * M Month - M generates no leading 0 months as numbers,
19 - * MM produces 2 digit month numbers,
20 - * MMM produces 3 letter month abbreviated form,
21 - * MMMM (4 or more) produces the full form.
22 - * d Day - d generates no leading 0 day of month as numbers,
23 - * dd produces 2 digit day of month numbers.
24 - * </pre>
15 + * Note: This class supports Gregorian dates only and has no concept of time or TimeZones. As such when working with
16 + * Java Util Date(s) (or it's descendants) the local (or "Wall") date is all that is considered!
25 17 */
26 18 public final class SimpleDate extends CalendarSupport<SimpleDate> implements YearMonthDayAccessor,
27 19 TemporalSimpleType,
  @@ -30,13 +22,13 @@
30 22 {
31 23 private static final long serialVersionUID = 2L;
32 24
33 - private final DateFormat mFormat;
25 + private final DateRes mDateRes;
34 26 private final int mYear, mMonth, mDay;
35 27
36 28 @SuppressWarnings({"deprecation", "UnusedDeclaration"}) @Deprecated /** for Serialization */
37 29 protected SimpleDate()
38 30 {
39 - this( 0, 0, 0, null );
31 + this( null, 0, 0, 0 );
40 32 }
41 33
42 34 public static SimpleDate today()
  @@ -44,112 +36,93 @@
44 36 return new SimpleDate( new Date() );
45 37 }
46 38
47 - private SimpleDate( int pYear, int pMonth, int pDay, DateFormat pFormat )
39 + private SimpleDate( DateRes pDateRes, int pYear, int pMonth, int pDay )
48 40 {
49 - mFormat = pFormat;
41 + mDateRes = pDateRes;
50 42 mYear = pYear;
51 43 mMonth = pMonth;
52 44 mDay = pDay;
45 + if ( isValidToDay() || (pDay != 0) )
46 + {
47 + mDateRes.validateDay( this, mYear, mMonth, mDay );
48 + }
49 + else if ( isValidToMonth() || (pMonth != 0) )
50 + {
51 + mDateRes.validateMonth( this, mYear, mMonth );
52 + }
53 + // Any Year is OK
53 54 }
54 55
55 - private SimpleDate( YearMonthDayAccessor pDate, DateFormat pFormat )
56 - throws IllegalArgumentException
57 - {
58 - mFormat = pFormat;
59 - mYear = pDate.getYear();
60 - mMonth = isValidToMonth() ? mFormat.monthProvided( mYear, pDate.getMonth() ) : 0;
61 - mDay = isValidToDay() ? mFormat.dayProvided( mYear, mMonth, pDate.getDay() ) : 0;
62 - }
63 -
64 - public SimpleDate( DateFormat pFormat, int pYear )
65 - throws IllegalArgumentException
66 - {
67 - this( pYear, 0, 0, YearFormatAccessor.deNull( pFormat ) );
68 - mFormat.noMonthProvided();
69 - }
70 -
71 - public SimpleDate( int pYear )
72 - throws IllegalArgumentException
73 - {
74 - this( null, pYear );
75 - }
76 -
77 - public SimpleDate( DateFormat pFormat, int pYear, int pMonth )
56 + public SimpleDate( int pYear, int pMonth, int pDay )
78 57 throws IllegalArgumentException
79 58 {
80 - this( pYear, pMonth, 0, YearMonthFormatAccessor.deNull( pFormat ) );
81 - mFormat.monthProvided( pYear, pMonth );
82 - mFormat.noDayProvided();
59 + this( DateRes.ToDAY, pYear, pMonth, pDay );
83 60 }
84 61
85 62 public SimpleDate( int pYear, int pMonth )
86 63 throws IllegalArgumentException
87 64 {
88 - this( null, pYear, pMonth );
89 - }
90 -
91 - public SimpleDate( DateFormat pFormat, int pYear, int pMonth, int pDay )
92 - throws IllegalArgumentException
93 - {
94 - this( pYear, pMonth, pDay, YearMonthDayFormatAccessor.deNull( pFormat ) );
95 - mFormat.dayProvided( pYear, pMonth, pDay );
65 + this( DateRes.ToMONTH, pYear, pMonth, 0 );
96 66 }
97 67
98 - public SimpleDate( int pYear, int pMonth, int pDay )
68 + public SimpleDate( int pYear )
99 69 throws IllegalArgumentException
100 70 {
101 - this( null, pYear, pMonth, pDay );
71 + this( DateRes.ToYEAR, pYear, 0, 0 );
102 72 }
103 73
104 - public SimpleDate( DateFormat pFormat, UtilDateAdaptor pDate )
74 + public SimpleDate( DateRes pDateRes, YearMonthDayAccessor pAccessor )
105 75 throws IllegalArgumentException
106 76 {
107 - this( pDate, YearMonthDayFormatAccessor.deNull( pFormat ) );
77 + this( pDateRes,
78 + pAccessor.getYear(),
79 + pDateRes.isValidToMonth() ? pAccessor.getMonth() : 0,
80 + pDateRes.isValidToDay() ? pAccessor.getDay() : 0 );
108 81 }
109 82
110 - public SimpleDate( UtilDateAdaptor pDate )
83 + public SimpleDate( YearMonthDayAccessor pAccessor )
111 84 throws IllegalArgumentException
112 85 {
113 - this( null, pDate );
86 + this( DateRes.ToDAY, pAccessor );
114 87 }
115 88
116 - public SimpleDate( DateFormat pFormat, Date pDate )
89 + public SimpleDate( DateRes pDateRes, Date pDate )
117 90 throws IllegalArgumentException
118 91 {
119 - this( pFormat, new UtilDateAdaptor( pDate ) );
92 + this( pDateRes, new UtilDateAdaptor( pDate ) );
120 93 }
121 94
122 95 public SimpleDate( Date pDate )
123 96 throws IllegalArgumentException
124 97 {
125 - this( null, pDate );
98 + this( new UtilDateAdaptor( pDate ) );
126 99 }
127 100
128 - public DateFormat getFormat()
101 + public DateRes getDateRes()
129 102 {
130 - return mFormat;
103 + return mDateRes;
131 104 }
132 105
133 106 /**
134 - * Return a SimpleDate (based on this) with the updated format (Resolution reduction supported)
107 + * Return a SimpleDate (based on this) with the updated Resolution (reduction supported)
135 108 *
136 - * @param pFormat !null
109 + * @param pDateRes !null
137 110 *
138 - * @throws IllegalArgumentException if <code>pFormat</code> is null, or the Resolution is attempting to be increased
111 + * @throws IllegalArgumentException if <code>pDateRes</code> is null, or the Resolution is attempting to be increased
139 112 */
140 - public SimpleDate changeFormat( DateFormat pFormat )
113 + public SimpleDate changeResolution( DateRes pDateRes )
141 114 throws IllegalArgumentException
142 115 {
143 - Objects.assertNotNull( "DateFormat", pFormat );
144 - if ( mFormat.equals( pFormat ) )
116 + Objects.assertNotNull( "DateRes", pDateRes );
117 + if ( mDateRes.equals( pDateRes ) )
145 118 {
146 119 return this;
147 120 }
148 - if ( mFormat.getDateRes().getIndex() < pFormat.getDateRes().getIndex() )
121 + if ( mDateRes.getIndex() < pDateRes.getIndex() )
149 122 {
150 123 throw new IllegalArgumentException( "May not add Resolution" );
151 124 }
152 - return new SimpleDate( pFormat, toUtilDateAdaptor() );
125 + return new SimpleDate( pDateRes, toUtilDateAdaptor() );
153 126 }
154 127
155 128 /**
  @@ -219,7 +192,7 @@
219 192 public SimpleDate month( int pMonth )
220 193 throws IllegalArgumentException
221 194 {
222 - LLcheckMonth( mYear, pMonth );
195 + mDateRes.validateMonth( this, mYear, pMonth );
223 196 return (mMonth == pMonth) ? this : (pMonth > mMonth) ? addMonths( pMonth - mMonth ) : minusMonths( mMonth - pMonth );
224 197 }
225 198
  @@ -255,9 +228,7 @@
255 228 public SimpleDate day( int pDay )
256 229 throws IllegalArgumentException
257 230 {
258 - mFormat.validateDayAdjustable();
259 - LLcheckDay( mYear, mMonth, pDay );
260 - return new SimpleDate( mYear, mMonth, pDay, mFormat );
231 + return new SimpleDate( mDateRes, mYear, mMonth, mDateRes.validateDay( this, mYear, mMonth, pDay ) );
261 232 }
262 233
263 234 /**
  @@ -284,12 +255,12 @@
284 255
285 256 public boolean isValidToMonth()
286 257 {
287 - return mFormat.getDateRes().isValidToMonth();
258 + return mDateRes.isValidToMonth();
288 259 }
289 260
290 261 public boolean isValidToDay()
291 262 {
292 - return mFormat.getDateRes().isValidToDay();
263 + return mDateRes.isValidToDay();
293 264 }
294 265
295 266 @Override
  @@ -318,7 +289,7 @@
318 289
319 290 public WeekDay getWeekDay()
320 291 {
321 - mFormat.validateValidToDay( "getWeekDay" );
292 + validateToDay();
322 293 return WeekDay.valueOf( UtilDateAdaptor.dayOfWeek( toUtilDateAdaptor().getWallDate() ) );
323 294 }
324 295
  @@ -332,7 +303,8 @@
332 303 public int daysTill( SimpleDate pThem )
333 304 throws IllegalArgumentException
334 305 {
335 - validateValidToDay( "daysTill", pThem );
306 + undefinedResultIfNull( "daysTill", pThem ).validateToDay();
307 + validateToDay();
336 308 int zThisMinusThem = compareTo( pThem );
337 309 if ( zThisMinusThem == 0 )
338 310 {
  @@ -387,8 +359,7 @@
387 359 public int daysTill( Date pThem )
388 360 throws IllegalArgumentException
389 361 {
390 - undefinedResultIfNull( "daysTill", pThem );
391 - return daysTill( new SimpleDate( mFormat, pThem ) );
362 + return daysTill( new SimpleDate( mDateRes, new UtilDateAdaptor( undefinedResultIfNull( "daysTill", pThem ) ) ) );
392 363 }
393 364
394 365 /**
  @@ -403,8 +374,7 @@
403 374 public boolean after( Date pThem )
404 375 throws IllegalArgumentException
405 376 {
406 - undefinedResultIfNull( "after", pThem );
407 - return after( new SimpleDate( mFormat, pThem ) );
377 + return after( new SimpleDate( mDateRes, new UtilDateAdaptor( undefinedResultIfNull( "after", pThem ) ) ) );
408 378 }
409 379
410 380 /**
  @@ -419,24 +389,19 @@
419 389 public boolean before( Date pThem )
420 390 throws IllegalArgumentException
421 391 {
422 - undefinedResultIfNull( "before", pThem );
423 - return before( new SimpleDate( mFormat, pThem ) );
392 + return before( new SimpleDate( mDateRes, new UtilDateAdaptor( undefinedResultIfNull( "before", pThem ) ) ) );
424 393 }
425 394
426 395 @Override
427 396 public boolean equals( Object o )
428 397 {
429 398 return (this == o) || //
430 - ((o instanceof SimpleDate) && equals( (SimpleDate) o )) || ((o instanceof Date) && equals( (Date) o ));
431 - }
432 -
433 - public boolean equals( Date pDate )
434 - {
435 - return (pDate != null) && equals( new SimpleDate( mFormat, pDate ) );
399 + ((o instanceof SimpleDate) && equals( (SimpleDate) o ));
436 400 }
437 401
438 402 public boolean equals( SimpleDate them )
439 403 {
404 + // DateRes does not need to be explicitly checked as the Month and Day are appropriately effected by DateRes!
440 405 return (this == them) || //
441 406 ((them != null) //
442 407 && equal( this.mYear, them.mYear ) //
  @@ -448,6 +413,7 @@
448 413 @Override
449 414 public int hashCode()
450 415 {
416 + // DateRes does not need to be explicitly checked as the Month and Day are appropriately effected by DateRes!
451 417 return hashCodeEm( calcHashCode( mYear ), //
452 418 calcHashCode( mMonth ), //
453 419 calcHashCode( mDay ) );
  @@ -456,6 +422,7 @@
456 422 @Override
457 423 public int compareTo( SimpleDate them )
458 424 {
425 + // DateRes does not need to be explicitly checked as the Month and Day are appropriately effected by DateRes!
459 426 return compareEm( compare( this.mYear, them.mYear ), //
460 427 compare( this.mMonth, them.mMonth ), //
461 428 compare( this.mDay, them.mDay ) );
  @@ -482,7 +449,7 @@
482 449 @Override
483 450 public String toString()
484 451 {
485 - return mFormat.format( this );
452 + return mDateRes.getDateFormat().format( this );
486 453 }
487 454
488 455 public static String toString( SimpleDate pDate )
  @@ -490,29 +457,12 @@
490 457 return (pDate != null) ? pDate.toString() : null;
491 458 }
492 459
493 - public String toYMD()
494 - {
495 - StringBuilder sb = new StringBuilder();
496 - sb.append( getYear() );
497 - if ( isValidToMonth() )
498 - {
499 - sb.append( '/' );
500 - MM_Chunk.appendTo( sb, this );
501 - if ( isValidToDay() )
502 - {
503 - sb.append( '/' );
504 - dd_Chunk.appendTo( sb, this );
505 - }
506 - }
507 - return sb.toString();
508 - }
509 -
510 460 public static String toYMD( SimpleDate pFromYMD )
511 461 {
512 462 return (pFromYMD != null) ? pFromYMD.toYMD() : null;
513 463 }
514 464
515 - public static SimpleDate fromYMD( DateFormat pFormat, String pToYMD )
465 + public static SimpleDate fromYMD( String pToYMD )
516 466 throws IllegalArgumentException
517 467 {
518 468 if ( pToYMD == null )
  @@ -523,39 +473,53 @@
523 473 switch ( parts.length )
524 474 {
525 475 case 1:
526 - return new SimpleDate( pFormat, //
527 - parseInt( parts[0], "Year", pToYMD ) );
476 + return new SimpleDate( parseInt( parts[0], "Year", pToYMD ) );
528 477 case 2:
529 - return new SimpleDate( pFormat, //
530 - parseInt( parts[0], "Year", pToYMD ), //
478 + return new SimpleDate( parseInt( parts[0], "Year", pToYMD ), //
531 479 parseInt( parts[1], "Month", pToYMD ) );
532 480 case 3:
533 - return new SimpleDate( pFormat, //
534 - parseInt( parts[0], "Year", pToYMD ), //
535 - parseInt( parts[1], "Month", pToYMD ), //
481 + return new SimpleDate( parseInt( parts[0], "Year", pToYMD ), //
482 + parseInt( parts[1], "Month", pToYMD ),
536 483 parseInt( parts[2], "Day", pToYMD ) );
537 484 default:
538 485 throw new IllegalStateException( "parts.length = " + parts.length );
539 486 }
540 487 }
541 488
489 + public String toYMD()
490 + {
491 + StringBuilder sb = new StringBuilder();
492 + yyyy_Chunk.appendTo( sb, this );
493 + if ( isValidToMonth() )
494 + {
495 + sb.append( '/' );
496 + MM_Chunk.appendTo( sb, this );
497 + if ( isValidToDay() )
498 + {
499 + sb.append( '/' );
500 + dd_Chunk.appendTo( sb, this );
501 + }
502 + }
503 + return sb.toString();
504 + }
505 +
542 506 @Override
543 - public java.sql.Date toSQLvalue()
507 + public String toSQLvalue()
544 508 {
545 - return new java.sql.Date( toUtilDate().getTime() );
509 + return toYMD();
510 + }
511 +
512 + public static SimpleDate fromSQLvalue( String pToSQLvalue )
513 + throws IllegalArgumentException
514 + {
515 + return fromYMD( pToSQLvalue );
546 516 }
547 517
548 518 // vvvvvvvvvvvvvvvvvv Support methods vvvvvvvvvvvvvvvvv
549 519
550 - private void validateValidToDay( String pWhat, SimpleDate pThem )
551 - throws IllegalArgumentException
520 + private void validateToDay()
552 521 {
553 - mFormat.validateValidToDay( pWhat );
554 - undefinedResultIfNull( pWhat, pThem );
555 - if ( !pThem.isValidToDay() )
556 - {
557 - throw new IllegalArgumentException( "Parameter to " + pWhat + " does NOT have Day resolution" );
558 - }
522 + mDateRes.validateToDay( this );
559 523 }
560 524
561 525 private SimpleDate LLsetYear( int pNewYear, boolean pAdjustForward )
  @@ -566,14 +530,14 @@
566 530 private SimpleDate LLsetMonth( int pNewMonth, boolean pAdjustForward )
567 531 throws IllegalArgumentException
568 532 {
569 - mFormat.validateMonthAdjustable();
533 + mDateRes.validateToMonth( this );
570 534 return (mMonth == pNewMonth) ? this : new Mutable( this ).setMonth( pNewMonth, pAdjustForward ).toSimpleDate();
571 535 }
572 536
573 537 private SimpleDate LLsetDay( int pDay )
574 538 throws IllegalArgumentException
575 539 {
576 - mFormat.validateDayAdjustable();
540 + mDateRes.validateToDay( this );
577 541 return (mDay == pDay) ? this : new Mutable( this ).setDay( pDay ).toSimpleDate();
578 542 }
579 543
  @@ -641,12 +605,12 @@
641 605
642 606 private static class Mutable implements YearMonthDayAccessor
643 607 {
644 - private DateFormat mDateFormat;
608 + private final DateRes mDateRes;
645 609 private int mYear, mMonth, mDay;
646 610
647 - private Mutable( DateFormat pDateFormat, int pYear, int pMonth, int pDay )
611 + private Mutable( DateRes pDateRes, int pYear, int pMonth, int pDay )
648 612 {
649 - mDateFormat = pDateFormat;
613 + mDateRes = pDateRes;
650 614 mYear = pYear;
651 615 mMonth = pMonth;
652 616 mDay = pDay;
  @@ -656,7 +620,7 @@
656 620
657 621 private Mutable( SimpleDate pDate )
658 622 {
659 - this( pDate.mFormat, pDate.getYear(), pDate.getMonth(), pDate.getDay() );
623 + this( pDate.mDateRes, pDate.getYear(), pDate.getMonth(), pDate.getDay() );
660 624 }
661 625
662 626 @Override
  @@ -751,7 +715,7 @@
751 715 private Mutable adjustDayOfMonthForMonthOrYearChange( boolean pAdjustForward )
752 716 {
753 717 normalizeMonthOfYear();
754 - if ( mDateFormat.getDateRes().isValidToDay() ) // if Not, then any year works with any month
718 + if ( mDateRes.isValidToDay() ) // if Not, then any year works with any month
755 719 {
756 720 int maxDaysInMonth = Month.daysIn( mYear, mMonth );
757 721 if ( maxDaysInMonth < mDay )
  @@ -798,7 +762,7 @@
798 762
799 763 public SimpleDate toSimpleDate()
800 764 {
801 - return new SimpleDate( this, mDateFormat );
765 + return new SimpleDate( mDateRes, this );
802 766 }
803 767 }
804 768 }