litesoft
@ 851
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 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.simpletypes.temporal.nonpublic; import java.io.*; import org.litesoft.core.simpletypes.nonpublic.*; import org.litesoft.core.simpletypes.temporal.*; import org.litesoft.core.typeutils.*; import org.litesoft.core.typeutils.gregorian.*; public abstract class CalendarSupport<T extends CalendarSupport> extends CompareSupport<T> implements Serializable { public static int parseInt( String pToParse, String pWhat, String pFrom ) throws IllegalArgumentException { pToParse = pToParse.trim(); if ( pToParse.length() == 0 ) { throw new IllegalArgumentException( "No " + pWhat + " in '" + pFrom + "'" ); } try { return Integer.parseInt( pToParse ); } catch ( NumberFormatException e ) { throw new IllegalArgumentException( "Invalid " + pWhat + " '" + pToParse + "' in '" + pFrom + "': " + e.getMessage() ); } } protected static void undefinedResultIfNull( String pWhat, Object pToCheck ) throws IllegalArgumentException { if ( pToCheck == null ) { throw new IllegalArgumentException( "Result is undefined for: " + pWhat + "( null )" ); } } protected static int LLcheckMonth( int pYear, int pMonth ) { if ( (pMonth < 1) || (12 < pMonth) ) { throw new IllegalArgumentException( "Invalid Month provided (Year: " + pYear + "): " + pMonth ); } return pMonth; } protected static int LLcheckDay( int pYear, int pMonth, int pDay ) { if ( (pDay < 1) || (Month.daysIn( pYear, pMonth ) < pDay) ) { throw new IllegalArgumentException( "Invalid Day provided (Year: " + pYear + ", Month: " + pMonth + "): " + pDay ); } return pDay; } protected int validate( String pWhat, int pNewValue, int pMaxValue ) throws IllegalArgumentException { if ( (pNewValue < 0) || (pMaxValue < pNewValue) ) { 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 ); } return pNewValue; } protected static String minLength( int pMinLength, String pValue ) { while ( pValue.length() < pMinLength ) { pValue = "0" + pValue; } return pValue; } public interface Chunk { void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource ); } private static final class LiteralChunk implements Chunk { private String mLiteral; public LiteralChunk( String pLiteral ) { mLiteral = pLiteral; } @Override public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource ) { pSB.append( mLiteral ); } } protected static final Chunk SLASH = new LiteralChunk( "/" ); protected static final Chunk SPACE = new LiteralChunk( " " ); protected static final Chunk COLON = new LiteralChunk( ":" ); protected static final Chunk COMMA = new LiteralChunk( "," ); protected static final Chunk DASH = new LiteralChunk( "-" ); protected static final Chunk d_Chunk = new Chunk() { @Override public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource ) { pSB.append( pYMDsource.getDay() ); } }; protected static final Chunk dd_Chunk = new Chunk() { @Override public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource ) { pSB.append( Integers.zeroPadIt( pYMDsource.getDay(), 2 ) ); } }; protected static final Chunk M_Chunk = new Chunk() { @Override public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource ) { pSB.append( pYMDsource.getMonth() ); } }; protected static final Chunk MM_Chunk = new Chunk() { @Override public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource ) { pSB.append( Integers.zeroPadIt( pYMDsource.getMonth(), 2 ) ); } }; protected static final Chunk MMMM_Chunk = new Chunk() { @Override public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource ) { pSB.append( Month.nameFromMonthNumber( pYMDsource.getMonth() ) ); } }; protected static final Chunk MMM_Chunk = new Chunk() { @Override public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource ) { pSB.append( Month.shortNameFromMonthNumber( pYMDsource.getMonth() ) ); } }; protected static final Chunk y_Chunk = new Chunk() { @Override public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource ) { pSB.append( pYMDsource.getYear() ); } }; protected static final Chunk yy_Chunk = new Chunk() { @Override public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource ) { String zYear = Integers.zeroPadIt( pYMDsource.getYear(), 2 ); pSB.append( (zYear.length() == 2) ? zYear : zYear.substring( zYear.length() - 2 ) ); } }; protected static final Chunk yyyy_Chunk = new Chunk() { @Override public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource ) { String zYear = Integers.zeroPadIt( pYMDsource.getYear(), 4 ); pSB.append( (zYear.length() == 4) ? zYear : zYear.substring( zYear.length() - 4 ) ); } }; protected static Chunk yyy_Chunk( final int pMinLength ) { return new Chunk() { @Override public void appendTo( StringBuilder pSB, YearMonthDayAccessor pYMDsource ) { pSB.append( Integers.zeroPadIt( pYMDsource.getYear(), pMinLength ) ); } }; } protected static Chunk literalChunk( String pLiteral ) { return new LiteralChunk( pLiteral ); } } |