litesoft
@ 942
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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.simpletypes.temporal; import org.litesoft.commonfoundation.typeutils.gregorian.*; import org.litesoft.core.*; import org.litesoft.core.simpletypes.nonpublic.*; import org.litesoft.core.simpletypes.temporal.nonpublic.*; import java.util.*; /** * A Simple Time object with variable resolution from the Hour thru the Millisecond. */ public final class SimpleTime extends AbstractWithTimeRes<SimpleTime> implements // TODO: ??? CalendarSupport.Chunk, TimeWithResolutionAccessor, TemporalSimpleType, SQLtimeable { private static final long serialVersionUID = 1L; private int mHour, mMin, mSec, mMilliSec; /** * @deprecated - for Serialization */ @SuppressWarnings("deprecation") protected SimpleTime() { } private SimpleTime( TimeRes pResolution ) { super( pResolution ); } public SimpleTime( int pHour ) throws IllegalArgumentException { this( TimeRes.ToHOUR ); setHour( pHour ); } public SimpleTime( int pHour, int pMin ) throws IllegalArgumentException { this( TimeRes.ToMIN ); setHour( pHour ); setMin( pMin ); } public SimpleTime( int pHour, int pMin, int pSec ) throws IllegalArgumentException { this( TimeRes.ToSEC ); setHour( pHour ); setMin( pMin ); setSec( pSec ); } public SimpleTime( int pHour, int pMin, int pSec, int pMilliSec ) throws IllegalArgumentException { this( TimeRes.ToMSEC ); setHour( pHour ); setMin( pMin ); setSec( pSec ); setMilliSec( pMilliSec ); } public SimpleTime( TimeWithResolutionAccessor pThem ) // Copy Constructor throws IllegalArgumentException { super( (pThem != null) ? pThem.getTimeRes() : null ); if ( pThem == null ) { throw new IllegalArgumentException( "Time, 'them', may not be null" ); } this.mHour = pThem.getHour(); this.mMin = pThem.getMin(); this.mSec = pThem.getSec(); this.mMilliSec = pThem.getMilliSec(); } public SimpleTime copy() { return new SimpleTime( this ); } public SimpleTime copyWithUpdatedResolution( TimeRes pResolution ) throws IllegalArgumentException { int zCompareResult = chkCopyWithUpdatedResolution( pResolution ); if ( zCompareResult == 0 ) { return copy(); } StandIn standIn = new StandIn( this ); if ( CompareSupport.lessThan( zCompareResult ) ) { standIn.greaterResolution( pResolution.getIndex() ); // We had less resolution - Suspicious results! } else { standIn.lesserResolution( pResolution.getIndex() ); // We had more resolution! } return standIn.toSimpleTime(); } /** * Delete this method... */ public SimpleTime copyWithExpandedResolution( TimeRes pResolution ) throws IllegalArgumentException { if ( chkCopyWithExpandedResolution( pResolution ) ) { return copy(); } StandIn standIn = new StandIn( this ); standIn.greaterResolution( pResolution.getIndex() ); return standIn.toSimpleTime(); } /** * Delete this method... */ public SimpleTime copyWithReducedResolution( TimeRes pResolution ) throws IllegalArgumentException { if ( chkCopyWithReducedResolution( pResolution ) ) { return copy(); } StandIn standIn = new StandIn( this ); standIn.lesserResolution( pResolution.getIndex() ); return standIn.toSimpleTime(); } private static SimpleTime timeComponents( int pResolutionIndex, Date pDate ) throws IllegalArgumentException { if ( pDate == null ) { throw new IllegalArgumentException( "Date is Required" ); } return new StandIn( pResolutionIndex, pDate ).toSimpleTime(); } public static SimpleTime timeComponents( TimeRes pResolution, Date pDate ) throws IllegalArgumentException { return timeComponents( (pResolution == null) ? TimeRes.MSECindex : pResolution.getIndex(), pDate ); } public static SimpleTime timeComponents( Date pDate ) throws IllegalArgumentException { return timeComponents( TimeRes.MSECindex, pDate ); } public static SimpleTime timeComponents( UtilDateAdaptor pDate ) throws IllegalArgumentException { return new StandIn( pDate ).toSimpleTime(); } @Override public int getHour() { return mHour; } /** * @return 0 if not ValidToMin */ @Override public int getMin() { return mMin; } /** * @return 0 if not ValidToSec */ @Override public int getSec() { return mSec; } /** * @return 0 if not ValidToMilliSec */ @Override public int getMilliSec() { return mMilliSec; } public UtilDateAdaptor getUtilDateAdaptor() { return UtilDateAdaptor.currentWall( 1970, 1, 1, getHour(), getMin(), getSec(), getMilliSec() ); } public void setTime( SimpleTime pTime ) throws IllegalArgumentException { if ( pTime == null ) { throw new IllegalArgumentException( "Time was null" ); } setHour( pTime.getHour() ); if ( this.isValidToMin() && pTime.isValidToMin() ) { setMin( pTime.getMin() ); if ( this.isValidToSec() && pTime.isValidToSec() ) { setSec( pTime.getSec() ); if ( this.isValidToMilliSec() && pTime.isValidToMilliSec() ) { setMilliSec( pTime.getMilliSec() ); } } } } public void setHour( int pHour ) throws IllegalArgumentException { mHour = validate( TimeRes.HOURindex, "Hour", pHour, 23 ); } public void setMin( int pMin ) throws IllegalArgumentException { mMin = validate( TimeRes.MINindex, "Min", pMin, 59 ); } public void setSec( int pSec ) throws IllegalArgumentException { mSec = validate( TimeRes.SECindex, "Sec", pSec, 59 ); } public void setMilliSec( int pMilliSec ) { mMilliSec = validate( TimeRes.MSECindex, "MilliSec", pMilliSec, 999 ); } /** * Adjust the Hours and return the number of days needing to be adjusted. */ public int addHours( int pHours ) { if ( pHours == 0 ) { return 0; } mHour += pHours; return normalize(); } /** * Adjust the Minutes and return the number of days needing to be adjusted. */ public int addMins( int pMins ) throws IllegalArgumentException { validateMinAdjustable(); if ( pMins == 0 ) { return 0; } mMin += pMins; return normalize(); } /** * Adjust the Seconds and return the number of days needing to be adjusted. */ public int addSecs( int pSecs ) throws IllegalArgumentException { validateSecAdjustable(); if ( pSecs == 0 ) { return 0; } mSec += pSecs; return normalize(); } /** * Adjust the MilliSeconds and return the number of days needing to be adjusted. */ public int addMilliSecs( int pMilliSecs ) throws IllegalArgumentException { validateMilliSecAdjustable(); if ( pMilliSecs == 0 ) { return 0; } mMilliSec += pMilliSecs; return normalize(); } private int normalize() { while ( mMilliSec >= 1000 ) { mSec++; mMilliSec -= 1000; } while ( mMilliSec < 0 ) { mSec--; mMilliSec += 1000; } while ( mSec >= 60 ) { mMin++; mSec -= 60; } while ( mSec < 0 ) { mMin--; mSec += 60; } while ( mMin >= 60 ) { mHour++; mMin -= 60; } while ( mMin < 0 ) { mHour--; mMin += 60; } int days = 0; while ( mHour >= 24 ) { days++; mHour -= 24; } while ( mHour < 0 ) { days--; mHour += 24; } return days; } /** * True if this is 'before' pThem. * <p/> * Note: Uses the lesser resolution of pThem or this. * * @param pThem !null * * @throws IllegalArgumentException if pThem is null */ public boolean before( SimpleTime pThem ) throws IllegalArgumentException { return LLbeforeOrAfter( pThem, "before", -1 ); } /** * True if this is 'after' pThem. * <p/> * Note: Uses the lesser resolution of pThem or this. * * @param pThem !null * * @throws IllegalArgumentException if pThem is null */ public boolean after( SimpleTime pThem ) throws IllegalArgumentException { return LLbeforeOrAfter( pThem, "after", 1 ); } private boolean LLbeforeOrAfter( SimpleTime pThem, String pWhat, int pCompareRetValue ) throws IllegalArgumentException { undefinedResultIfNull( pWhat, pThem ); if ( (this.mHour == pThem.mHour) && this.isValidToMin() && pThem.isValidToMin() ) { if ( (this.mMin == pThem.mMin) && this.isValidToSec() && pThem.isValidToSec() ) { if ( (this.mSec == pThem.mSec) && this.isValidToMilliSec() && pThem.isValidToMilliSec() ) { return pCompareRetValue == compare( this.mMilliSec, pThem.mMilliSec ); } return pCompareRetValue == compare( this.mSec, pThem.mSec ); } return pCompareRetValue == compare( this.mMin, pThem.mMin ); } return pCompareRetValue == compare( this.mHour, pThem.mHour ); } /** * Determine the Number of Hours, Mins, Secs, & MilliSecs between this and pThem. * <p/> * The Maximum resolution of the result is the minimum resolution of this and pThem. However, the actual * resolution is dependant on the result. * * @param pThem !null * * @throws IllegalArgumentException if pThem is null */ public SimpleTimespan difference( SimpleTime pThem ) throws IllegalArgumentException { return LLbeforeOrAfter( pThem, "difference", -1 ) ? // this < pThem ? new Difference( this ).till( pThem ) : // new Difference( pThem ).till( this ); } /** * Adjust by the Timespan and return the number of days needing to be adjusted. */ public int add( SimpleTimespan pTimespan ) { if ( pTimespan == null ) { return 0; } switch ( getResolutionIndex() ) { case TimeRes.MSECindex: mMilliSec += pTimespan.getMilliSecs(); // Fall Thru case TimeRes.SECindex: mSec += pTimespan.getSecs(); // Fall Thru case TimeRes.MINindex: mMin += pTimespan.getMins(); // Fall Thru case TimeRes.HOURindex: mHour += pTimespan.getHours(); // Fall Thru default: break; } return normalize() + pTimespan.getDays(); } /** * Adjust by the Timespan and return the number of days needing to be adjusted. */ public int subtract( SimpleTimespan pTimespan ) { if ( pTimespan == null ) { return 0; } switch ( getResolutionIndex() ) { case TimeRes.MSECindex: mMilliSec -= pTimespan.getMilliSecs(); // Fall Thru case TimeRes.SECindex: mSec -= pTimespan.getSecs(); // Fall Thru case TimeRes.MINindex: mMin -= pTimespan.getMins(); // Fall Thru case TimeRes.HOURindex: mHour -= pTimespan.getHours(); // Fall Thru default: break; } return normalize() - pTimespan.getDays(); } public long getSqlTimeOffset() { UtilDateAdaptor adaptor = UtilDateAdaptor.currentWall( 1970, 1, 1, getHour(), getMin(), getSec() ); return adaptor.getWallDate().getTime(); } @Override public boolean equals( Object o ) { return (this == o) || // ((o instanceof SimpleTime) && equals( (SimpleTime) o )); } public boolean equals( SimpleTime them ) { return (this == them) || // ((them != null) // && equal( this.mHour, them.mHour ) // && equal( this.mMin, them.mMin ) // && equal( this.mSec, them.mSec ) // && equal( this.mMilliSec, them.mMilliSec ) // ); } @Override public int hashCode() { return hashCodeEm( calcHashCode( mHour ), // calcHashCode( mMin ), // calcHashCode( mSec ), // calcHashCode( mMilliSec ) ); } @Override public int compareTo( SimpleTime them ) { return compareEm( compare( this.mHour, them.mHour ), // compare( this.mMin, them.mMin ), // compare( this.mSec, them.mSec ), // compare( this.mMilliSec, them.mMilliSec ) ); } private String minLength( int pMinLength, int pValue ) { return minLength( pMinLength, Integer.toString( pValue ) ); } @Override public java.sql.Time toSQLvalue() { return new java.sql.Time( getSqlTimeOffset() ); } public static String toString( SimpleTime pTime ) { return (pTime != null) ? pTime.toString() : null; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append( minLength( 2, mHour ) ); if ( isValidToMin() ) { sb.append( ':' ).append( minLength( 2, mMin ) ); if ( isValidToSec() ) { sb.append( ':' ).append( minLength( 2, mSec ) ); if ( isValidToMilliSec() ) { sb.append( '.' ).append( minLength( 3, mMilliSec ) ); } } } return sb.toString(); } public static SimpleTime fromString( String pToString ) throws IllegalArgumentException { if ( pToString == null ) { return null; } String[] parts = SimpleParser.parse( pToString, "::.", 1 ); switch ( parts.length ) { case 1: return new SimpleTime( parseInt( parts[0], "Hour", pToString ) ); case 2: return new SimpleTime( parseInt( parts[0], "Hour", pToString ), // parseInt( parts[1], "Min", pToString ) ); case 3: return new SimpleTime( parseInt( parts[0], "Hour", pToString ), // parseInt( parts[1], "Min", pToString ), // parseInt( parts[2], "Sec", pToString ) ); case 4: return new SimpleTime( parseInt( parts[0], "Hour", pToString ), // parseInt( parts[1], "Min", pToString ), // parseInt( parts[2], "Sec", pToString ), // parseInt( parts[3], "MilliSec", pToString ) ); default: throw new IllegalStateException( "parts.length = " + parts.length ); } } private static class StandIn { public StandIn() { } public int mHour, mMin, mSec, mMilliSec, mResolutionIndex = TimeRes.MSECindex; public StandIn( SimpleTime pTime ) { mHour = pTime.getHour(); mMin = pTime.getMin(); mSec = pTime.getSec(); mMilliSec = pTime.getMilliSec(); mResolutionIndex = pTime.getResolutionIndex(); } public StandIn( int pResolutionIndex, Date pDate ) { inject( pResolutionIndex, new UtilDateAdaptor( pDate, TemporalResolution.ToHour.plus( pResolutionIndex ) ) ); } public StandIn( UtilDateAdaptor pAdaptor ) { inject( pAdaptor.getTemporalResolution().getTimeFieldsCount() - 1, pAdaptor ); } public void inject( int pResolutionIndex, UtilDateAdaptor pAdaptor ) { if ( (mResolutionIndex = pResolutionIndex) < 0 ) { throw new IllegalArgumentException( "UtilDateAdaptor has No Time elements!" ); } mHour = pAdaptor.getHour(); mMin = pAdaptor.getMin(); mSec = pAdaptor.getSec(); mMilliSec = pAdaptor.getMilliSec(); } public void greaterResolution( int pResolutionIndex ) { if ( pResolutionIndex > mResolutionIndex ) { clearBeyondResolution( mResolutionIndex ); mResolutionIndex = pResolutionIndex; } } public void lesserResolution( int pResolutionIndex ) { if ( pResolutionIndex < mResolutionIndex ) { clearBeyondResolution( mResolutionIndex = pResolutionIndex ); } } private void clearBeyondResolution( int pResolutionIndex ) { switch ( pResolutionIndex ) { case TimeRes.HOURindex: mMin = 0; // Fall Thru case TimeRes.MINindex: mSec = 0; // Fall Thru case TimeRes.SECindex: mMilliSec = 0; // Fall Thru case TimeRes.MSECindex: // Fall Thru default: break; } } public void lesserResolution( StandIn pThem ) { this.lesserResolution( pThem.mResolutionIndex ); pThem.lesserResolution( this.mResolutionIndex ); } public SimpleTime toSimpleTime() { switch ( mResolutionIndex ) { default: case TimeRes.HOURindex: return new SimpleTime( mHour ); case TimeRes.MINindex: return new SimpleTime( mHour, mMin ); case TimeRes.SECindex: return new SimpleTime( mHour, mMin, mSec ); case TimeRes.MSECindex: return new SimpleTime( mHour, mMin, mSec, mMilliSec ); } } public SimpleTimespan toSimpleTimespan() { return new SimpleTimespan( 0, mHour, mMin, mSec, mMilliSec ); } public void incHour() { if ( ++mHour >= 24 ) { mHour -= 24; } } public void incMin() { if ( ++mMin >= 60 ) { mMin -= 60; incHour(); } } public void incSec() { if ( ++mSec >= 60 ) { mSec -= 60; incMin(); } } public void incMilliSec() { if ( ++mMilliSec >= 1000 ) { mMilliSec -= 1000; incSec(); } } } private static class Difference { private StandIn mFrom, mDelta = new StandIn(); public Difference( SimpleTime pFrom ) { mDelta.lesserResolution( mFrom = new StandIn( pFrom ) ); } public SimpleTimespan till( SimpleTime pThem ) // !null and is in our future { StandIn to = new StandIn( pThem ); mDelta.lesserResolution( to ); mFrom.lesserResolution( to.mResolutionIndex ); if ( mFrom.mHour != to.mHour ) { advanceMilliSecsTo( 0 ); advanceSecsTo( 0 ); advanceMinsTo( 0 ); for (; mFrom.mHour != to.mHour; mFrom.incHour() ) { mDelta.incHour(); } } if ( mFrom.mMin != to.mMin ) { advanceMilliSecsTo( 0 ); advanceSecsTo( 0 ); advanceMinsTo( to.mMin ); } if ( mFrom.mSec != to.mSec ) { advanceMilliSecsTo( 0 ); advanceSecsTo( to.mSec ); } advanceMilliSecsTo( to.mMilliSec ); return mDelta.toSimpleTimespan(); } private void advanceMinsTo( int pTarget ) { for (; mFrom.mMin != pTarget; mFrom.incMin() ) { mDelta.incMin(); } } private void advanceSecsTo( int pTarget ) { for (; mFrom.mSec != pTarget; mFrom.incSec() ) { mDelta.incSec(); } } private void advanceMilliSecsTo( int pTarget ) { for (; mFrom.mMilliSec != pTarget; mFrom.incMilliSec() ) { mDelta.incMilliSec(); } } } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/simpletypes/temporal/SimpleTime.java
Revision | Author | Commited | Message |
---|---|---|---|
942 Diff | GeorgeS | Mon 02 Jun, 2014 23:41:46 +0000 | Extracting commonfoundation |
912 Diff | GeorgeS | Fri 28 Jun, 2013 06:48:05 +0000 | Revert to mixed & Working (PEDS & Prioritizer) code! |
897 Diff | GeorgeS | Mon 25 Feb, 2013 02:04:34 +0000 | |
868 Diff | GeorgeS | Mon 26 Nov, 2012 01:28:51 +0000 | |
865 Diff | GeorgeS | Mon 19 Nov, 2012 02:25:29 +0000 | |
864 Diff | GeorgeS | Mon 19 Nov, 2012 01:48:25 +0000 | |
851 Diff | GeorgeS | Mon 08 Oct, 2012 00:05:32 +0000 | Breaking the code as Temporal changes are implemented... |
850 Diff | GeorgeS | Tue 11 Sep, 2012 17:23:33 +0000 | |
849 Diff | GeorgeS | Tue 11 Sep, 2012 17:11:59 +0000 | Clean up serialization |
847 | GeorgeS | Fri 07 Sep, 2012 15:53:36 +0000 |