litesoft
@ 898
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 |
package org.litesoft.core.simpletypes.temporal; import org.litesoft.core.typeutils.*; public abstract class AbstractCalendarYM<T extends AbstractCalendarYM, M extends AbstractCalendarYM.Mutable> extends AbstractCalendarY<T, M> implements CalendarAccessorYM { private static final long serialVersionUID = 1L; /** * Return a T with the Month set to the parameter, if there are insufficient days in the new month, the date will be set to the last day of the target month. */ public final T month( int pMonth ) { validateMonth( getYear(), pMonth ); return (getMonth() == pMonth) ? us() : LLsetMonth( pMonth ); } /** * if after add pMonth there are insufficient days in the new month, the date will be set to the last day of the target month. */ public final T addMonths( int pMonths ) { return (pMonths == 0) ? us() : LLsetMonth( getMonth() + pMonths ); } /** * if after minus pMonths there are insufficient days in the new month, the date will be set to the last day of the target month. */ public final T minusMonths( int pMonths ) { return (pMonths == 0) ? us() : LLsetMonth( getMonth() - pMonths ); } // vvvvvvvvvvvvvvvvvv Support methods vvvvvvvvvvvvvvvvv protected static final int parseMonth( String pStringForm, String... pParts ) { return parseInt( pParts[1], "Month", pStringForm ); } protected static final StringBuilder formatSQLvalueYM( CalendarAccessorYM pAccessor, StringBuilder pSB ) { formatSQLvalueY( pAccessor, pSB ).append( '-' ); return formatMonth( pAccessor, pSB ); } protected static final StringBuilder formatSortableDisplayFormYM( CalendarAccessorYM pAccessor, StringBuilder pSB ) { formatSortableDisplayFormY( pAccessor, pSB ).append( '/' ); return formatMonth( pAccessor, pSB ); } protected static final StringBuilder formatMonth( CalendarAccessorYM pAccessor, StringBuilder pSB ) { MM_Chunk.appendMonthTo( pAccessor, pSB ); return pSB; } protected static final int validateMonth( int pYear, int pMonth ) throws IllegalArgumentException { if ( (pMonth < 1) || (12 < pMonth) ) { throw new IllegalArgumentException( "Invalid Month provided (Year: " + pYear + "): " + pMonth ); } return pMonth; } protected int LL_hashCode() { return hashCodeEm( super.hashCode(), getMonth() ); } /** * T is non-null and of the same class, so should check everything! */ protected boolean LL_equalsNonNullStrict( T them ) { return super.LL_equalsNonNullStrict( them ) && this.getMonth() == them.getMonth(); } protected Compare LL_compareNonNullLoose( Object them ) { Compare zCompare = super.LL_compareNonNullLoose( them ); if ( (zCompare.result() == EQUAL) && (them instanceof CalendarAccessorYM) ) { zCompare = zCompare.then( this.getMonth(), ((CalendarAccessorYM) them).getMonth() ); } return zCompare; } /** * @param pNewMonth != current */ protected final T LLsetMonth( int pNewMonth ) throws IllegalArgumentException { M zMutable = createMutable( getYear(), pNewMonth ); zMutable.normalize(); return createTypeFrom( zMutable ); } protected final M createMutable( int pNewYear ) { return createMutable( pNewYear, getMonth() ); } protected abstract M createMutable( int pYear, int pNewMonth ); protected abstract T createTypeFrom( M pMutable ); protected static class Mutable extends AbstractCalendarY.Mutable implements CalendarAccessorYM { private int mMonth; public Mutable( int pYear, int pMonth ) { super( pYear ); mMonth = pMonth; } public final int getMonth() { return mMonth; } public void normalize() { while ( mMonth > 12 ) { tooLargeMonth(); } while ( mMonth < 1 ) { tooSmallMonth(); } } protected final void decrementMonth() { if ( --mMonth < 1 ) { tooSmallMonth(); } } protected final void incrementMonth() { if ( 12 < ++mMonth ) { tooLargeMonth(); } } private void tooSmallMonth() { decrementYear(); mMonth += 12; } private void tooLargeMonth() { incrementYear(); mMonth -= 12; } } } |