litesoft
@ 947
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 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.core.simpletypes.temporal.nonpublic; import java.io.*; import org.litesoft.commonfoundation.typeutils.*; import org.litesoft.core.simpletypes.nonpublic.*; import org.litesoft.commonfoundation.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 <T> T undefinedResultIfNull( String pWhat, T pToCheck ) throws IllegalArgumentException { if ( pToCheck == null ) { throw new IllegalArgumentException( "Result is undefined for: " + pWhat + "( null )" ); } return pToCheck; } 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, CalendarAccessorYMD pYMDsource ); } protected static final class LiteralChunk implements Chunk { private String mLiteral; public LiteralChunk( String pLiteral ) { mLiteral = pLiteral; } @Override public void appendTo( StringBuilder pSB, CalendarAccessorYMD 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, CalendarAccessorYMD pYMDsource ) { pSB.append( pYMDsource.getDay() ); } }; protected static final Chunk dd_Chunk = new Chunk() { @Override public void appendTo( StringBuilder pSB, CalendarAccessorYMD pYMDsource ) { pSB.append( Integers.zeroPadIt( 2, pYMDsource.getDay() ) ); } }; protected static abstract class MonthChunk implements Chunk { @Override public final void appendTo( StringBuilder pSB, CalendarAccessorYMD pYMDsource ) { appendMonthTo(pYMDsource, pSB); } public abstract void appendMonthTo( CalendarAccessorYM pYMsource, StringBuilder pSB ); } protected static final MonthChunk M_Chunk = new MonthChunk() { @Override public void appendMonthTo( CalendarAccessorYM pYMsource, StringBuilder pSB ) { pSB.append( pYMsource.getMonth() ); } }; protected static final MonthChunk MM_Chunk = new MonthChunk() { @Override public void appendMonthTo( CalendarAccessorYM pYMsource, StringBuilder pSB ) { pSB.append( Integers.zeroPadIt( 2, pYMsource.getMonth() ) ); } }; protected static final MonthChunk MMMM_Chunk = new MonthChunk() { @Override public void appendMonthTo( CalendarAccessorYM pYMsource, StringBuilder pSB ) { pSB.append( Month.nameFromMonthNumber( pYMsource.getMonth() ) ); } }; protected static final MonthChunk MMM_Chunk = new MonthChunk() { @Override public void appendMonthTo( CalendarAccessorYM pYMsource, StringBuilder pSB ) { pSB.append( Month.shortNameFromMonthNumber( pYMsource.getMonth() ) ); } }; protected static abstract class YearChunk implements Chunk { @Override public final void appendTo( StringBuilder pSB, CalendarAccessorYMD pYMDsource ) { appendYearTo( pYMDsource, pSB ); } public abstract void appendYearTo( CalendarAccessorY pYsource, StringBuilder pSB ); } protected static final YearChunk y_Chunk = new YearChunk() { @Override public void appendYearTo( CalendarAccessorY pYsource, StringBuilder pSB ) { pSB.append( pYsource.getYear() ); } }; protected static final YearChunk yy_Chunk = new YearChunk() { @Override public void appendYearTo( CalendarAccessorY pYsource, StringBuilder pSB ) { String zYear = Integers.zeroPadIt( 2, pYsource.getYear() ); pSB.append( (zYear.length() == 2) ? zYear : zYear.substring( zYear.length() - 2 ) ); } }; protected static final YearChunk yyyy_Chunk = new YearChunk() { @Override public void appendYearTo( CalendarAccessorY pYsource, StringBuilder pSB ) { String zYear = Integers.zeroPadIt( 4, pYsource.getYear() ); 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, CalendarAccessorYMD pYMDsource ) { pSB.append( Integers.zeroPadIt( pMinLength, pYMDsource.getYear() ) ); } }; } protected static Chunk literalChunk( String pLiteral ) { return new LiteralChunk( pLiteral ); } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/simpletypes/temporal/nonpublic/CalendarSupport.java
Revision | Author | Commited | Message |
---|---|---|---|
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
942 Diff | GeorgeS | Mon 02 Jun, 2014 23:41:46 +0000 | Extracting commonfoundation |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
862 Diff | GeorgeS | Thu 15 Nov, 2012 02:23:36 +0000 | On the Way... |
859 Diff | GeorgeS | Mon 05 Nov, 2012 01:26:38 +0000 | |
857 Diff | GeorgeS | Sun 04 Nov, 2012 16:01:37 +0000 | |
853 Diff | GeorgeS | Sun 04 Nov, 2012 15:18:21 +0000 | |
851 Diff | GeorgeS | Mon 08 Oct, 2012 00:05:32 +0000 | Breaking the code as Temporal changes are implemented... |
50 Diff | GeorgeS | Tue 13 Apr, 2010 11:51:38 +0000 | |
49 | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |