|
@@ -6,6 +6,7 @@ |
6 |
6 |
|
import org.litesoft.core.simpletypes.nonpublic.*; |
7 |
7 |
|
import org.litesoft.core.simpletypes.temporal.nonpublic.*; |
8 |
8 |
|
import org.litesoft.core.typeutils.*; |
|
9 |
+ |
import org.litesoft.core.typeutils.gregorian.*; |
9 |
10 |
|
|
10 |
11 |
|
/** |
11 |
12 |
|
* Format Control for SimpleDate which provides varying levels of resolution based on the Format String |
|
@@ -23,460 +24,459 @@ |
23 |
24 |
|
* </pre> |
24 |
25 |
|
*/ |
25 |
26 |
|
@SuppressWarnings("deprecation") |
26 |
|
- |
public class DateFormatControl<T extends DateFormatControl> extends CalendarSupport<T> implements TemporalParsingSupport, |
27 |
|
- |
SimpleType |
|
27 |
+ |
public class DateFormatControl<T extends DateFormatControl> // extends CalendarSupport<T> implements TemporalParsingSupport, SimpleType |
28 |
28 |
|
{ |
29 |
|
- |
private static final long serialVersionUID = 1L; |
30 |
|
- |
|
31 |
|
- |
public static final String DEFAULT_DATE_FORMAT = "dd MMM yyyy"; |
32 |
|
- |
|
33 |
|
- |
private static final char[] ACCEPTABLE_INDICATORS = // |
34 |
|
- |
{ // |
35 |
|
- |
DateRes.INDICATOR_YEAR, // |
36 |
|
- |
DateRes.INDICATOR_MONTH, // |
37 |
|
- |
DateRes.INDICATOR_DAY, // |
38 |
|
- |
}; |
39 |
|
- |
|
40 |
|
- |
private String mFormat = DEFAULT_DATE_FORMAT; |
41 |
|
- |
|
42 |
|
- |
private int mResolutionIndex = DateRes.DAYindex; |
43 |
|
- |
|
44 |
|
- |
// Late Bound: |
45 |
|
- |
private transient String mFieldOrder = null; |
46 |
|
- |
private transient Chunk[] mToStringChunks = null; |
47 |
|
- |
|
48 |
|
- |
@SuppressWarnings({"deprecation", "UnusedDeclaration"}) @Deprecated /** for Serialization */ |
49 |
|
- |
protected DateFormatControl() |
50 |
|
- |
{ |
51 |
|
- |
} |
52 |
|
- |
|
53 |
|
- |
public DateFormatControl( String pFormat ) |
54 |
|
- |
{ |
55 |
|
- |
if ( null == (pFormat = Strings.noEmpty( pFormat )) ) |
56 |
|
- |
{ |
57 |
|
- |
mFormat = DEFAULT_DATE_FORMAT; |
58 |
|
- |
} |
59 |
|
- |
else |
60 |
|
- |
{ |
61 |
|
- |
mFormat = pFormat; |
62 |
|
- |
} |
63 |
|
- |
int zIndicatorBits = indicatorBit( 1, DateRes.ToYEAR ) + // |
64 |
|
- |
indicatorBit( 2, DateRes.ToMONTH ) + // |
65 |
|
- |
indicatorBit( 4, DateRes.ToDAY ); |
66 |
|
- |
switch ( zIndicatorBits ) |
67 |
|
- |
{ |
68 |
|
- |
case 6: // Month & Day |
69 |
|
- |
// fall thru *** Common format used when the Year is implicit (e.g. 4JAN means the next January 4th) |
70 |
|
- |
case 7: // Year,Month, & Day |
71 |
|
- |
mResolutionIndex = DateRes.DAYindex; |
72 |
|
- |
break; |
73 |
|
- |
case 3: // Year & Month |
74 |
|
- |
mResolutionIndex = DateRes.MONTHindex; |
75 |
|
- |
break; |
76 |
|
- |
case 1: // Year Only |
77 |
|
- |
mResolutionIndex = DateRes.YEARindex; |
78 |
|
- |
break; |
79 |
|
- |
case 5: // Year & Day ? |
80 |
|
- |
throw doesNotContain( DateRes.ToMONTH, "" ); |
81 |
|
- |
case 2: // Month Only ? |
82 |
|
- |
// Fall thru |
83 |
|
- |
case 4: // Day Only ? |
84 |
|
- |
// Fall thru |
85 |
|
- |
case 0: // None! |
86 |
|
- |
throw doesNotContain( DateRes.ToYEAR, "" ); |
87 |
|
- |
} |
88 |
|
- |
} |
89 |
|
- |
|
90 |
|
- |
public String getFormat() |
91 |
|
- |
{ |
92 |
|
- |
return mFormat; |
93 |
|
- |
} |
94 |
|
- |
|
95 |
|
- |
public DateRes getDateRes() |
96 |
|
- |
{ |
97 |
|
- |
return DateRes.fromIndex( mResolutionIndex ); |
98 |
|
- |
} |
99 |
|
- |
|
100 |
|
- |
public boolean isValidToMonth() |
101 |
|
- |
{ |
102 |
|
- |
return DateRes.MONTHindex <= mResolutionIndex; |
103 |
|
- |
} |
104 |
|
- |
|
105 |
|
- |
public boolean isValidToDay() |
106 |
|
- |
{ |
107 |
|
- |
return DateRes.DAYindex <= mResolutionIndex; |
108 |
|
- |
} |
109 |
|
- |
|
110 |
|
- |
@Override |
111 |
|
- |
public String toString() |
112 |
|
- |
{ |
113 |
|
- |
return getFormat(); |
114 |
|
- |
} |
115 |
|
- |
|
116 |
|
- |
/** |
117 |
|
- |
* Validate the date indicated against this' resolution. |
118 |
|
- |
* <p/> |
119 |
|
- |
* If the resolution is less than ToDAY, then pDay may be 0. |
120 |
|
- |
* If the resolution is less than ToMONTH, then pMonth may be 0, but then pDay MUST be 0. |
121 |
|
- |
* <p/> |
122 |
|
- |
* If pDay != 0, the complete date will be validated regardless of the resolution. |
123 |
|
- |
* If pMonth != 0, the month will be validated regardless of the resolution. |
124 |
|
- |
* |
125 |
|
- |
* @param pMonth 0-12, (0 ok if pDay is 0, AND resolution is ToYEAR) |
126 |
|
- |
* @param pDay 0-(28-31), (0 ok if the resolution is less than ToDAY) |
127 |
|
- |
*/ |
128 |
|
- |
public void validate( int pYear, int pMonth, int pDay ) |
129 |
|
- |
throws IllegalArgumentException |
130 |
|
- |
{ |
131 |
|
- |
if ( isValidToDay() || (pDay != 0) ) |
132 |
|
- |
{ |
133 |
|
- |
String error = validDate( pYear, pMonth, pDay ); |
134 |
|
- |
if ( error != null ) |
135 |
|
- |
{ |
136 |
|
- |
throw new IllegalArgumentException( error ); |
137 |
|
- |
} |
138 |
|
- |
} |
139 |
|
- |
else if ( isValidToMonth() || (pMonth != 0) ) |
140 |
|
- |
{ |
141 |
|
- |
LLcheckMonth( pMonth ); |
142 |
|
- |
} |
143 |
|
- |
// Any Year is OK |
144 |
|
- |
} |
145 |
|
- |
|
146 |
|
- |
/** |
147 |
|
- |
* The expected order of the expected fields of a SimpleDate. |
148 |
|
- |
* <p/> |
149 |
|
- |
* E.g. "dd MMM yyyy" -> "dMy" |
150 |
|
- |
* E.g. "MM/dd/yy" -> "Mdy" |
151 |
|
- |
* E.g. "ddMMM" -> "dM" |
152 |
|
- |
*/ |
153 |
|
- |
public synchronized String getFieldOrder() |
154 |
|
- |
{ |
155 |
|
- |
if ( mFieldOrder == null ) |
156 |
|
- |
{ |
157 |
|
- |
mFieldOrder = determineFieldOrder( ACCEPTABLE_INDICATORS ); |
158 |
|
- |
} |
159 |
|
- |
return mFieldOrder; |
160 |
|
- |
} |
161 |
|
- |
|
162 |
|
- |
protected synchronized Chunk[] getToStringChunks( YMDaccessor pYMDaccessor ) |
163 |
|
- |
{ |
164 |
|
- |
if ( mToStringChunks == null ) |
165 |
|
- |
{ |
166 |
|
- |
mToStringChunks = createChunks( pYMDaccessor, ACCEPTABLE_INDICATORS ); |
167 |
|
- |
} |
168 |
|
- |
return mToStringChunks; |
169 |
|
- |
} |
170 |
|
- |
|
171 |
|
- |
protected IllegalArgumentException formatPlusIndicatorWhy( String pPlus, char pIndicator, String pWhy ) |
172 |
|
- |
{ |
173 |
|
- |
return new IllegalArgumentException( "Format '" + mFormat + "' " + pPlus + // |
174 |
|
- |
" '" + pIndicator + "' indicator" + pWhy ); |
175 |
|
- |
} |
176 |
|
- |
|
177 |
|
- |
protected IllegalArgumentException doesNotContain( DateRes pDateRes, String pWhy ) |
178 |
|
- |
{ |
179 |
|
- |
return formatPlusIndicatorWhy( "does NOT contain a " + pDateRes.getFriendlyName(), // |
180 |
|
- |
pDateRes.getIndicator(), // |
181 |
|
- |
pWhy ); |
182 |
|
- |
} |
183 |
|
- |
|
184 |
|
- |
protected IllegalArgumentException doesContain( DateRes pDateRes ) |
185 |
|
- |
{ |
186 |
|
- |
return formatPlusIndicatorWhy( "contains a " + pDateRes.getFriendlyName(), // |
187 |
|
- |
pDateRes.getIndicator(), // |
188 |
|
- |
", but no " + pDateRes.getFriendlyName() + " provided" ); |
189 |
|
- |
} |
190 |
|
- |
|
191 |
|
- |
protected void dayProvided( int pDay ) |
192 |
|
- |
throws IllegalArgumentException |
193 |
|
- |
{ |
194 |
|
- |
if ( !isValidToDay() ) |
195 |
|
- |
{ |
196 |
|
- |
throw doesNotContain( DateRes.ToDAY, ", but Day provided" ); |
197 |
|
- |
} |
198 |
|
- |
LLcheckDay( pDay ); |
199 |
|
- |
} |
200 |
|
- |
|
201 |
|
- |
protected void monthProvided( int pMonth ) |
202 |
|
- |
throws IllegalArgumentException |
203 |
|
- |
{ |
204 |
|
- |
if ( !isValidToMonth() ) |
205 |
|
- |
{ |
206 |
|
- |
throw doesNotContain( DateRes.ToMONTH, ", but Month provided" ); |
207 |
|
- |
} |
208 |
|
- |
LLcheckMonth( pMonth ); |
209 |
|
- |
} |
210 |
|
- |
|
211 |
|
- |
protected void noMonthProvided() |
212 |
|
- |
throws IllegalArgumentException |
213 |
|
- |
{ |
214 |
|
- |
if ( isValidToMonth() ) |
215 |
|
- |
{ |
216 |
|
- |
throw doesContain( DateRes.ToMONTH ); |
217 |
|
- |
} |
218 |
|
- |
} |
219 |
|
- |
|
220 |
|
- |
protected void noDayProvided() |
221 |
|
- |
throws IllegalArgumentException |
222 |
|
- |
{ |
223 |
|
- |
if ( isValidToDay() ) |
224 |
|
- |
{ |
225 |
|
- |
throw doesContain( DateRes.ToDAY ); |
226 |
|
- |
} |
227 |
|
- |
} |
228 |
|
- |
|
229 |
|
- |
private String determineFieldOrder( char[] pAcceptableIndicators ) |
230 |
|
- |
{ |
231 |
|
- |
List<FoundIndicator> zFounds = new ArrayList<FoundIndicator>(); |
232 |
|
- |
for ( char indicator : pAcceptableIndicators ) |
233 |
|
- |
{ |
234 |
|
- |
int at = mFormat.indexOf( indicator ); |
235 |
|
- |
if ( at != -1 ) |
236 |
|
- |
{ |
237 |
|
- |
zFounds.add( new FoundIndicator( at, indicator ) ); |
238 |
|
- |
} |
239 |
|
- |
} |
240 |
|
- |
if ( zFounds.size() > 1 ) |
241 |
|
- |
{ |
242 |
|
- |
Collections.sort( zFounds ); |
243 |
|
- |
} |
244 |
|
- |
StringBuilder sb = new StringBuilder( zFounds.size() ); |
245 |
|
- |
for ( FoundIndicator zFound : zFounds ) |
246 |
|
- |
{ |
247 |
|
- |
sb.append( zFound ); |
248 |
|
- |
} |
249 |
|
- |
return sb.toString(); |
250 |
|
- |
} |
251 |
|
- |
|
252 |
|
- |
private int indicatorBit( int pBit, DateRes pDateRes ) |
253 |
|
- |
{ |
254 |
|
- |
return (-1 != mFormat.indexOf( pDateRes.getIndicator() )) ? pBit : 0; |
255 |
|
- |
} |
256 |
|
- |
|
257 |
|
- |
private static class FoundIndicator implements Comparable |
258 |
|
- |
{ |
259 |
|
- |
private int mIndex; |
260 |
|
- |
private char mIndicator; |
261 |
|
- |
|
262 |
|
- |
public FoundIndicator( int pIndex, char pIndicator ) |
263 |
|
- |
{ |
264 |
|
- |
mIndex = pIndex; |
265 |
|
- |
mIndicator = pIndicator; |
266 |
|
- |
} |
267 |
|
- |
|
268 |
|
- |
@Override |
269 |
|
- |
public String toString() |
270 |
|
- |
{ |
271 |
|
- |
return Character.toString( mIndicator ); |
272 |
|
- |
} |
273 |
|
- |
|
274 |
|
- |
@Override |
275 |
|
- |
public boolean equals( Object o ) |
276 |
|
- |
{ |
277 |
|
- |
return (this == o) || // |
278 |
|
- |
((o instanceof FoundIndicator) && equals( (FoundIndicator) o )); |
279 |
|
- |
} |
280 |
|
- |
|
281 |
|
- |
public boolean equals( FoundIndicator them ) |
282 |
|
- |
{ |
283 |
|
- |
return (this == them) || // |
284 |
|
- |
((them != null) // |
285 |
|
- |
&& equal( this.mIndex, them.mIndex ) // |
286 |
|
- |
&& equal( this.mIndicator, them.mIndicator ) // |
287 |
|
- |
); |
288 |
|
- |
} |
289 |
|
- |
|
290 |
|
- |
@Override |
291 |
|
- |
public int hashCode() |
292 |
|
- |
{ |
293 |
|
- |
return hashCodeEm( calcHashCode( mIndex ), // |
294 |
|
- |
calcHashCode( (int) mIndicator ) ); |
295 |
|
- |
} |
296 |
|
- |
|
297 |
|
- |
@Override |
298 |
|
- |
public int compareTo( Object pObject ) |
299 |
|
- |
{ |
300 |
|
- |
FoundIndicator them = (FoundIndicator) pObject; |
301 |
|
- |
return compare( this.mIndex, them.mIndex ); |
302 |
|
- |
} |
303 |
|
- |
} |
304 |
|
- |
|
305 |
|
- |
protected Chunk[] createChunks( YMDaccessor pYMDaccessor, char[] pAcceptableIndicators ) |
306 |
|
- |
{ |
307 |
|
- |
ChunkCollector zCC = new ChunkCollector( pYMDaccessor, pAcceptableIndicators ); |
308 |
|
- |
int from = 0; |
309 |
|
- |
char curChunkChar = 0; |
310 |
|
- |
for ( int to = 0; to < mFormat.length(); to++ ) |
311 |
|
- |
{ |
312 |
|
- |
char c = mFormat.charAt( to ); |
313 |
|
- |
if ( curChunkChar == 0 ) |
314 |
|
- |
{ |
315 |
|
- |
if ( zCC.isAcceptableIndicators( c ) ) |
316 |
|
- |
{ |
317 |
|
- |
zCC.addChunk( from, to, curChunkChar ); |
318 |
|
- |
from = to; |
319 |
|
- |
curChunkChar = c; |
320 |
|
- |
} |
321 |
|
- |
} |
322 |
|
- |
else if ( curChunkChar != c ) |
323 |
|
- |
{ |
324 |
|
- |
zCC.addChunk( from, to, curChunkChar ); |
325 |
|
- |
from = to; |
326 |
|
- |
curChunkChar = zCC.isAcceptableIndicators( c ) ? c : 0; |
327 |
|
- |
} |
328 |
|
- |
} |
329 |
|
- |
zCC.addChunk( from, curChunkChar ); |
330 |
|
- |
return zCC.getChunks(); |
331 |
|
- |
} |
332 |
|
- |
|
333 |
|
- |
private class ChunkCollector |
334 |
|
- |
{ |
335 |
|
- |
List<Chunk> mChunks = new ArrayList<Chunk>(); |
336 |
|
- |
private YMDaccessor mYMDaccessor; |
337 |
|
- |
private char[] mAcceptableIndicators; |
338 |
|
- |
|
339 |
|
- |
public ChunkCollector( YMDaccessor pYMDaccessor, char[] pAcceptableIndicators ) |
340 |
|
- |
{ |
341 |
|
- |
mYMDaccessor = pYMDaccessor; |
342 |
|
- |
mAcceptableIndicators = pAcceptableIndicators; |
343 |
|
- |
} |
344 |
|
- |
|
345 |
|
- |
public boolean isAcceptableIndicators( char pChar ) |
346 |
|
- |
{ |
347 |
|
- |
for ( char zAcceptableIndicator : mAcceptableIndicators ) |
348 |
|
- |
{ |
349 |
|
- |
if ( pChar == zAcceptableIndicator ) |
350 |
|
- |
{ |
351 |
|
- |
return true; |
352 |
|
- |
} |
353 |
|
- |
} |
354 |
|
- |
return false; |
355 |
|
- |
} |
356 |
|
- |
|
357 |
|
- |
public Chunk[] getChunks() |
358 |
|
- |
{ |
359 |
|
- |
return mChunks.toArray( new Chunk[mChunks.size()] ); |
360 |
|
- |
} |
361 |
|
- |
|
362 |
|
- |
public void addChunk( int pFrom, char pCurChunkChar ) |
363 |
|
- |
{ |
364 |
|
- |
addChunk( pFrom, mFormat.length(), pCurChunkChar ); |
365 |
|
- |
} |
366 |
|
- |
|
367 |
|
- |
public void addChunk( int pFrom, int pTo, char pCurChunkChar ) |
368 |
|
- |
{ |
369 |
|
- |
Chunk chunk = getChunk( pFrom, pTo, pCurChunkChar ); |
370 |
|
- |
if ( chunk != null ) |
371 |
|
- |
{ |
372 |
|
- |
mChunks.add( chunk ); |
373 |
|
- |
} |
374 |
|
- |
} |
375 |
|
- |
|
376 |
|
- |
public Chunk getChunk( int pFrom, int pTo, char pCurChunkChar ) |
377 |
|
- |
{ |
378 |
|
- |
if ( pFrom == pTo ) |
379 |
|
- |
{ |
380 |
|
- |
return null; |
381 |
|
- |
} |
382 |
|
- |
if ( pCurChunkChar != 0 ) |
383 |
|
- |
{ |
384 |
|
- |
int length = pTo - pFrom; |
385 |
|
- |
switch ( pCurChunkChar ) |
386 |
|
- |
{ |
387 |
|
- |
case DateRes.INDICATOR_YEAR: |
388 |
|
- |
return get_y_nChunk( mYMDaccessor, length ); |
389 |
|
- |
case DateRes.INDICATOR_MONTH: |
390 |
|
- |
return get_M_nChunk( mYMDaccessor, length ); |
391 |
|
- |
case DateRes.INDICATOR_DAY: |
392 |
|
- |
return get_d_nChunk( mYMDaccessor, length ); |
393 |
|
- |
default: |
394 |
|
- |
break; |
395 |
|
- |
} |
396 |
|
- |
} |
397 |
|
- |
return getLiteralChunk( mFormat.substring( pFrom, pTo ) ); |
398 |
|
- |
} |
399 |
|
- |
} |
400 |
|
- |
|
401 |
|
- |
protected static Chunk getLiteralChunk( String pLiteral ) |
402 |
|
- |
{ |
403 |
|
- |
if ( pLiteral.length() == 1 ) |
404 |
|
- |
{ |
405 |
|
- |
switch ( pLiteral.charAt( 0 ) ) |
406 |
|
- |
{ |
407 |
|
- |
case '/': |
408 |
|
- |
return SLASH; |
409 |
|
- |
case ' ': |
410 |
|
- |
return SPACE; |
411 |
|
- |
case ':': |
412 |
|
- |
return COLON; |
413 |
|
- |
case ',': |
414 |
|
- |
return COMMA; |
415 |
|
- |
case '-': |
416 |
|
- |
return DASH; |
417 |
|
- |
default: |
418 |
|
- |
break; |
419 |
|
- |
} |
420 |
|
- |
} |
421 |
|
- |
return new LiteralChunk( pLiteral ); |
422 |
|
- |
} |
423 |
|
- |
|
424 |
|
- |
protected static Chunk get_M_nChunk( YMDaccessor pYMDaccessor, int pLength ) |
425 |
|
- |
{ |
426 |
|
- |
switch ( pLength ) |
427 |
|
- |
{ |
428 |
|
- |
case 1: |
429 |
|
- |
return new M_Chunk( pYMDaccessor ); |
430 |
|
- |
case 2: |
431 |
|
- |
return new MM_Chunk( pYMDaccessor ); |
432 |
|
- |
case 3: |
433 |
|
- |
return new MMM_Chunk( pYMDaccessor ); |
434 |
|
- |
default: |
435 |
|
- |
return new MMMM_Chunk( pYMDaccessor ); |
436 |
|
- |
} |
437 |
|
- |
} |
438 |
|
- |
|
439 |
|
- |
protected static Chunk get_y_nChunk( YMDaccessor pYMDaccessor, int pLength ) |
440 |
|
- |
{ |
441 |
|
- |
switch ( pLength ) |
442 |
|
- |
{ |
443 |
|
- |
default: |
444 |
|
- |
return new yyy_Chunk( pYMDaccessor, pLength ); |
445 |
|
- |
case 2: |
446 |
|
- |
return new yy_Chunk( pYMDaccessor ); |
447 |
|
- |
case 1: |
448 |
|
- |
return new y_Chunk( pYMDaccessor ); |
449 |
|
- |
} |
450 |
|
- |
} |
451 |
|
- |
|
452 |
|
- |
protected static Chunk get_d_nChunk( YMDaccessor pYMDaccessor, int pLength ) |
453 |
|
- |
{ |
454 |
|
- |
return (pLength == 2) ? new dd_Chunk( pYMDaccessor ) : new d_Chunk( pYMDaccessor ); |
455 |
|
- |
} |
456 |
|
- |
|
457 |
|
- |
@Override |
458 |
|
- |
public boolean equals( Object o ) |
459 |
|
- |
{ |
460 |
|
- |
return (o instanceof DateFormatControl) && equals( (DateFormatControl) o ); |
461 |
|
- |
} |
462 |
|
- |
|
463 |
|
- |
public boolean equals( DateFormatControl them ) |
464 |
|
- |
{ |
465 |
|
- |
return (this == them) || // |
466 |
|
- |
((them != null) // |
467 |
|
- |
&& this.mFormat.equals( them.mFormat ) // |
468 |
|
- |
); |
469 |
|
- |
} |
470 |
|
- |
|
471 |
|
- |
@Override |
472 |
|
- |
public int hashCode() |
473 |
|
- |
{ |
474 |
|
- |
return mFormat.hashCode(); |
475 |
|
- |
} |
476 |
|
- |
|
477 |
|
- |
@Override |
478 |
|
- |
public int compareTo( T them ) |
479 |
|
- |
{ |
480 |
|
- |
return this.mFormat.compareTo( them.mFormat ); |
481 |
|
- |
} |
|
29 |
+ |
// private static final long serialVersionUID = 1L; |
|
30 |
+ |
// |
|
31 |
+ |
// public static final String DEFAULT_DATE_FORMAT = "dd MMM yyyy"; |
|
32 |
+ |
// |
|
33 |
+ |
// private static final char[] ACCEPTABLE_INDICATORS = // |
|
34 |
+ |
// { // |
|
35 |
+ |
// DateRes.INDICATOR_YEAR, // |
|
36 |
+ |
// DateRes.INDICATOR_MONTH, // |
|
37 |
+ |
// DateRes.INDICATOR_DAY, // |
|
38 |
+ |
// }; |
|
39 |
+ |
// |
|
40 |
+ |
// private String mFormat = DEFAULT_DATE_FORMAT; |
|
41 |
+ |
// |
|
42 |
+ |
// private int mResolutionIndex = DateRes.DAYindex; |
|
43 |
+ |
// |
|
44 |
+ |
// // Late Bound: |
|
45 |
+ |
// private transient String mFieldOrder = null; |
|
46 |
+ |
// private transient Chunk[] mToStringChunks = null; |
|
47 |
+ |
// |
|
48 |
+ |
// @SuppressWarnings({"deprecation", "UnusedDeclaration"}) @Deprecated /** for Serialization */ |
|
49 |
+ |
// protected DateFormatControl() |
|
50 |
+ |
// { |
|
51 |
+ |
// } |
|
52 |
+ |
// |
|
53 |
+ |
// public DateFormatControl( String pFormat ) |
|
54 |
+ |
// { |
|
55 |
+ |
// if ( null == (pFormat = Strings.noEmpty( pFormat )) ) |
|
56 |
+ |
// { |
|
57 |
+ |
// mFormat = DEFAULT_DATE_FORMAT; |
|
58 |
+ |
// } |
|
59 |
+ |
// else |
|
60 |
+ |
// { |
|
61 |
+ |
// mFormat = pFormat; |
|
62 |
+ |
// } |
|
63 |
+ |
// int zIndicatorBits = indicatorBit( 1, DateRes.ToYEAR ) + // |
|
64 |
+ |
// indicatorBit( 2, DateRes.ToMONTH ) + // |
|
65 |
+ |
// indicatorBit( 4, DateRes.ToDAY ); |
|
66 |
+ |
// switch ( zIndicatorBits ) |
|
67 |
+ |
// { |
|
68 |
+ |
// case 6: // Month & Day |
|
69 |
+ |
// // fall thru *** Common format used when the Year is implicit (e.g. 4JAN means the next January 4th) |
|
70 |
+ |
// case 7: // Year,Month, & Day |
|
71 |
+ |
// mResolutionIndex = DateRes.DAYindex; |
|
72 |
+ |
// break; |
|
73 |
+ |
// case 3: // Year & Month |
|
74 |
+ |
// mResolutionIndex = DateRes.MONTHindex; |
|
75 |
+ |
// break; |
|
76 |
+ |
// case 1: // Year Only |
|
77 |
+ |
// mResolutionIndex = DateRes.YEARindex; |
|
78 |
+ |
// break; |
|
79 |
+ |
// case 5: // Year & Day ? |
|
80 |
+ |
// throw doesNotContain( DateRes.ToMONTH, "" ); |
|
81 |
+ |
// case 2: // Month Only ? |
|
82 |
+ |
// // Fall thru |
|
83 |
+ |
// case 4: // Day Only ? |
|
84 |
+ |
// // Fall thru |
|
85 |
+ |
// case 0: // None! |
|
86 |
+ |
// throw doesNotContain( DateRes.ToYEAR, "" ); |
|
87 |
+ |
// } |
|
88 |
+ |
// } |
|
89 |
+ |
// |
|
90 |
+ |
// public String getFormat() |
|
91 |
+ |
// { |
|
92 |
+ |
// return mFormat; |
|
93 |
+ |
// } |
|
94 |
+ |
// |
|
95 |
+ |
// public DateRes getDateRes() |
|
96 |
+ |
// { |
|
97 |
+ |
// return DateRes.fromIndex( mResolutionIndex ); |
|
98 |
+ |
// } |
|
99 |
+ |
// |
|
100 |
+ |
// public boolean isValidToMonth() |
|
101 |
+ |
// { |
|
102 |
+ |
// return DateRes.MONTHindex <= mResolutionIndex; |
|
103 |
+ |
// } |
|
104 |
+ |
// |
|
105 |
+ |
// public boolean isValidToDay() |
|
106 |
+ |
// { |
|
107 |
+ |
// return DateRes.DAYindex <= mResolutionIndex; |
|
108 |
+ |
// } |
|
109 |
+ |
// |
|
110 |
+ |
// @Override |
|
111 |
+ |
// public String toString() |
|
112 |
+ |
// { |
|
113 |
+ |
// return getFormat(); |
|
114 |
+ |
// } |
|
115 |
+ |
// |
|
116 |
+ |
// /** |
|
117 |
+ |
// * Validate the date indicated against this' resolution. |
|
118 |
+ |
// * <p/> |
|
119 |
+ |
// * If the resolution is less than ToDAY, then pDay may be 0. |
|
120 |
+ |
// * If the resolution is less than ToMONTH, then pMonth may be 0, but then pDay MUST be 0. |
|
121 |
+ |
// * <p/> |
|
122 |
+ |
// * If pDay != 0, the complete date will be validated regardless of the resolution. |
|
123 |
+ |
// * If pMonth != 0, the month will be validated regardless of the resolution. |
|
124 |
+ |
// * |
|
125 |
+ |
// * @param pMonth 0-12, (0 ok if pDay is 0, AND resolution is ToYEAR) |
|
126 |
+ |
// * @param pDay 0-(28-31), (0 ok if the resolution is less than ToDAY) |
|
127 |
+ |
// */ |
|
128 |
+ |
// public void validate( int pYear, int pMonth, int pDay ) |
|
129 |
+ |
// throws IllegalArgumentException |
|
130 |
+ |
// { |
|
131 |
+ |
// if ( isValidToDay() || (pDay != 0) ) |
|
132 |
+ |
// { |
|
133 |
+ |
// String error = Dates.checkValidity( pYear, pMonth, pDay ); |
|
134 |
+ |
// if ( error != null ) |
|
135 |
+ |
// { |
|
136 |
+ |
// throw new IllegalArgumentException( error ); |
|
137 |
+ |
// } |
|
138 |
+ |
// } |
|
139 |
+ |
// else if ( isValidToMonth() || (pMonth != 0) ) |
|
140 |
+ |
// { |
|
141 |
+ |
// LLcheckMonth( pMonth ); |
|
142 |
+ |
// } |
|
143 |
+ |
// // Any Year is OK |
|
144 |
+ |
// } |
|
145 |
+ |
// |
|
146 |
+ |
// /** |
|
147 |
+ |
// * The expected order of the expected fields of a SimpleDate. |
|
148 |
+ |
// * <p/> |
|
149 |
+ |
// * E.g. "dd MMM yyyy" -> "dMy" |
|
150 |
+ |
// * E.g. "MM/dd/yy" -> "Mdy" |
|
151 |
+ |
// * E.g. "ddMMM" -> "dM" |
|
152 |
+ |
// */ |
|
153 |
+ |
// public synchronized String getFieldOrder() |
|
154 |
+ |
// { |
|
155 |
+ |
// if ( mFieldOrder == null ) |
|
156 |
+ |
// { |
|
157 |
+ |
// mFieldOrder = determineFieldOrder( ACCEPTABLE_INDICATORS ); |
|
158 |
+ |
// } |
|
159 |
+ |
// return mFieldOrder; |
|
160 |
+ |
// } |
|
161 |
+ |
// |
|
162 |
+ |
// protected synchronized Chunk[] getToStringChunks( YearMonthDayAccessor pYMDaccessor ) |
|
163 |
+ |
// { |
|
164 |
+ |
// if ( mToStringChunks == null ) |
|
165 |
+ |
// { |
|
166 |
+ |
// mToStringChunks = createChunks( pYMDaccessor, ACCEPTABLE_INDICATORS ); |
|
167 |
+ |
// } |
|
168 |
+ |
// return mToStringChunks; |
|
169 |
+ |
// } |
|
170 |
+ |
// |
|
171 |
+ |
// protected IllegalArgumentException formatPlusIndicatorWhy( String pPlus, char pIndicator, String pWhy ) |
|
172 |
+ |
// { |
|
173 |
+ |
// return new IllegalArgumentException( "Format '" + mFormat + "' " + pPlus + // |
|
174 |
+ |
// " '" + pIndicator + "' indicator" + pWhy ); |
|
175 |
+ |
// } |
|
176 |
+ |
// |
|
177 |
+ |
// protected IllegalArgumentException doesNotContain( DateRes pDateRes, String pWhy ) |
|
178 |
+ |
// { |
|
179 |
+ |
// return formatPlusIndicatorWhy( "does NOT contain a " + pDateRes.getFriendlyName(), // |
|
180 |
+ |
// pDateRes.getIndicator(), // |
|
181 |
+ |
// pWhy ); |
|
182 |
+ |
// } |
|
183 |
+ |
// |
|
184 |
+ |
// protected IllegalArgumentException doesContain( DateRes pDateRes ) |
|
185 |
+ |
// { |
|
186 |
+ |
// return formatPlusIndicatorWhy( "contains a " + pDateRes.getFriendlyName(), // |
|
187 |
+ |
// pDateRes.getIndicator(), // |
|
188 |
+ |
// ", but no " + pDateRes.getFriendlyName() + " provided" ); |
|
189 |
+ |
// } |
|
190 |
+ |
// |
|
191 |
+ |
// protected void dayProvided( int pDay ) |
|
192 |
+ |
// throws IllegalArgumentException |
|
193 |
+ |
// { |
|
194 |
+ |
// if ( !isValidToDay() ) |
|
195 |
+ |
// { |
|
196 |
+ |
// throw doesNotContain( DateRes.ToDAY, ", but Day provided" ); |
|
197 |
+ |
// } |
|
198 |
+ |
// LLcheckDay( pDay ); |
|
199 |
+ |
// } |
|
200 |
+ |
// |
|
201 |
+ |
// protected void monthProvided( int pMonth ) |
|
202 |
+ |
// throws IllegalArgumentException |
|
203 |
+ |
// { |
|
204 |
+ |
// if ( !isValidToMonth() ) |
|
205 |
+ |
// { |
|
206 |
+ |
// throw doesNotContain( DateRes.ToMONTH, ", but Month provided" ); |
|
207 |
+ |
// } |
|
208 |
+ |
// LLcheckMonth( pMonth ); |
|
209 |
+ |
// } |
|
210 |
+ |
// |
|
211 |
+ |
// protected void noMonthProvided() |
|
212 |
+ |
// throws IllegalArgumentException |
|
213 |
+ |
// { |
|
214 |
+ |
// if ( isValidToMonth() ) |
|
215 |
+ |
// { |
|
216 |
+ |
// throw doesContain( DateRes.ToMONTH ); |
|
217 |
+ |
// } |
|
218 |
+ |
// } |
|
219 |
+ |
// |
|
220 |
+ |
// protected void noDayProvided() |
|
221 |
+ |
// throws IllegalArgumentException |
|
222 |
+ |
// { |
|
223 |
+ |
// if ( isValidToDay() ) |
|
224 |
+ |
// { |
|
225 |
+ |
// throw doesContain( DateRes.ToDAY ); |
|
226 |
+ |
// } |
|
227 |
+ |
// } |
|
228 |
+ |
// |
|
229 |
+ |
// private String determineFieldOrder( char[] pAcceptableIndicators ) |
|
230 |
+ |
// { |
|
231 |
+ |
// List<FoundIndicator> zFounds = new ArrayList<FoundIndicator>(); |
|
232 |
+ |
// for ( char indicator : pAcceptableIndicators ) |
|
233 |
+ |
// { |
|
234 |
+ |
// int at = mFormat.indexOf( indicator ); |
|
235 |
+ |
// if ( at != -1 ) |
|
236 |
+ |
// { |
|
237 |
+ |
// zFounds.add( new FoundIndicator( at, indicator ) ); |
|
238 |
+ |
// } |
|
239 |
+ |
// } |
|
240 |
+ |
// if ( zFounds.size() > 1 ) |
|
241 |
+ |
// { |
|
242 |
+ |
// Collections.sort( zFounds ); |
|
243 |
+ |
// } |
|
244 |
+ |
// StringBuilder sb = new StringBuilder( zFounds.size() ); |
|
245 |
+ |
// for ( FoundIndicator zFound : zFounds ) |
|
246 |
+ |
// { |
|
247 |
+ |
// sb.append( zFound ); |
|
248 |
+ |
// } |
|
249 |
+ |
// return sb.toString(); |
|
250 |
+ |
// } |
|
251 |
+ |
// |
|
252 |
+ |
// private int indicatorBit( int pBit, DateRes pDateRes ) |
|
253 |
+ |
// { |
|
254 |
+ |
// return (-1 != mFormat.indexOf( pDateRes.getIndicator() )) ? pBit : 0; |
|
255 |
+ |
// } |
|
256 |
+ |
// |
|
257 |
+ |
// private static class FoundIndicator implements Comparable |
|
258 |
+ |
// { |
|
259 |
+ |
// private int mIndex; |
|
260 |
+ |
// private char mIndicator; |
|
261 |
+ |
// |
|
262 |
+ |
// public FoundIndicator( int pIndex, char pIndicator ) |
|
263 |
+ |
// { |
|
264 |
+ |
// mIndex = pIndex; |
|
265 |
+ |
// mIndicator = pIndicator; |
|
266 |
+ |
// } |
|
267 |
+ |
// |
|
268 |
+ |
// @Override |
|
269 |
+ |
// public String toString() |
|
270 |
+ |
// { |
|
271 |
+ |
// return Character.toString( mIndicator ); |
|
272 |
+ |
// } |
|
273 |
+ |
// |
|
274 |
+ |
// @Override |
|
275 |
+ |
// public boolean equals( Object o ) |
|
276 |
+ |
// { |
|
277 |
+ |
// return (this == o) || // |
|
278 |
+ |
// ((o instanceof FoundIndicator) && equals( (FoundIndicator) o )); |
|
279 |
+ |
// } |
|
280 |
+ |
// |
|
281 |
+ |
// public boolean equals( FoundIndicator them ) |
|
282 |
+ |
// { |
|
283 |
+ |
// return (this == them) || // |
|
284 |
+ |
// ((them != null) // |
|
285 |
+ |
// && equal( this.mIndex, them.mIndex ) // |
|
286 |
+ |
// && equal( this.mIndicator, them.mIndicator ) // |
|
287 |
+ |
// ); |
|
288 |
+ |
// } |
|
289 |
+ |
// |
|
290 |
+ |
// @Override |
|
291 |
+ |
// public int hashCode() |
|
292 |
+ |
// { |
|
293 |
+ |
// return hashCodeEm( calcHashCode( mIndex ), // |
|
294 |
+ |
// calcHashCode( (int) mIndicator ) ); |
|
295 |
+ |
// } |
|
296 |
+ |
// |
|
297 |
+ |
// @Override |
|
298 |
+ |
// public int compareTo( Object pObject ) |
|
299 |
+ |
// { |
|
300 |
+ |
// FoundIndicator them = (FoundIndicator) pObject; |
|
301 |
+ |
// return compare( this.mIndex, them.mIndex ); |
|
302 |
+ |
// } |
|
303 |
+ |
// } |
|
304 |
+ |
// |
|
305 |
+ |
// protected Chunk[] createChunks( YearMonthDayAccessor pYMDaccessor, char[] pAcceptableIndicators ) |
|
306 |
+ |
// { |
|
307 |
+ |
// ChunkCollector zCC = new ChunkCollector( pYMDaccessor, pAcceptableIndicators ); |
|
308 |
+ |
// int from = 0; |
|
309 |
+ |
// char curChunkChar = 0; |
|
310 |
+ |
// for ( int to = 0; to < mFormat.length(); to++ ) |
|
311 |
+ |
// { |
|
312 |
+ |
// char c = mFormat.charAt( to ); |
|
313 |
+ |
// if ( curChunkChar == 0 ) |
|
314 |
+ |
// { |
|
315 |
+ |
// if ( zCC.isAcceptableIndicators( c ) ) |
|
316 |
+ |
// { |
|
317 |
+ |
// zCC.addChunk( from, to, curChunkChar ); |
|
318 |
+ |
// from = to; |
|
319 |
+ |
// curChunkChar = c; |
|
320 |
+ |
// } |
|
321 |
+ |
// } |
|
322 |
+ |
// else if ( curChunkChar != c ) |
|
323 |
+ |
// { |
|
324 |
+ |
// zCC.addChunk( from, to, curChunkChar ); |
|
325 |
+ |
// from = to; |
|
326 |
+ |
// curChunkChar = zCC.isAcceptableIndicators( c ) ? c : 0; |
|
327 |
+ |
// } |
|
328 |
+ |
// } |
|
329 |
+ |
// zCC.addChunk( from, curChunkChar ); |
|
330 |
+ |
// return zCC.getChunks(); |
|
331 |
+ |
// } |
|
332 |
+ |
// |
|
333 |
+ |
// private class ChunkCollector |
|
334 |
+ |
// { |
|
335 |
+ |
// List<Chunk> mChunks = new ArrayList<Chunk>(); |
|
336 |
+ |
// private YearMonthDayAccessor mYMDaccessor; |
|
337 |
+ |
// private char[] mAcceptableIndicators; |
|
338 |
+ |
// |
|
339 |
+ |
// public ChunkCollector( YearMonthDayAccessor pYMDaccessor, char[] pAcceptableIndicators ) |
|
340 |
+ |
// { |
|
341 |
+ |
// mYMDaccessor = pYMDaccessor; |
|
342 |
+ |
// mAcceptableIndicators = pAcceptableIndicators; |
|
343 |
+ |
// } |
|
344 |
+ |
// |
|
345 |
+ |
// public boolean isAcceptableIndicators( char pChar ) |
|
346 |
+ |
// { |
|
347 |
+ |
// for ( char zAcceptableIndicator : mAcceptableIndicators ) |
|
348 |
+ |
// { |
|
349 |
+ |
// if ( pChar == zAcceptableIndicator ) |
|
350 |
+ |
// { |
|
351 |
+ |
// return true; |
|
352 |
+ |
// } |
|
353 |
+ |
// } |
|
354 |
+ |
// return false; |
|
355 |
+ |
// } |
|
356 |
+ |
// |
|
357 |
+ |
// public Chunk[] getChunks() |
|
358 |
+ |
// { |
|
359 |
+ |
// return mChunks.toArray( new Chunk[mChunks.size()] ); |
|
360 |
+ |
// } |
|
361 |
+ |
// |
|
362 |
+ |
// public void addChunk( int pFrom, char pCurChunkChar ) |
|
363 |
+ |
// { |
|
364 |
+ |
// addChunk( pFrom, mFormat.length(), pCurChunkChar ); |
|
365 |
+ |
// } |
|
366 |
+ |
// |
|
367 |
+ |
// public void addChunk( int pFrom, int pTo, char pCurChunkChar ) |
|
368 |
+ |
// { |
|
369 |
+ |
// Chunk chunk = getChunk( pFrom, pTo, pCurChunkChar ); |
|
370 |
+ |
// if ( chunk != null ) |
|
371 |
+ |
// { |
|
372 |
+ |
// mChunks.add( chunk ); |
|
373 |
+ |
// } |
|
374 |
+ |
// } |
|
375 |
+ |
// |
|
376 |
+ |
// public Chunk getChunk( int pFrom, int pTo, char pCurChunkChar ) |
|
377 |
+ |
// { |
|
378 |
+ |
// if ( pFrom == pTo ) |
|
379 |
+ |
// { |
|
380 |
+ |
// return null; |
|
381 |
+ |
// } |
|
382 |
+ |
// if ( pCurChunkChar != 0 ) |
|
383 |
+ |
// { |
|
384 |
+ |
// int length = pTo - pFrom; |
|
385 |
+ |
// switch ( pCurChunkChar ) |
|
386 |
+ |
// { |
|
387 |
+ |
// case DateRes.INDICATOR_YEAR: |
|
388 |
+ |
// return get_y_nChunk( mYMDaccessor, length ); |
|
389 |
+ |
// case DateRes.INDICATOR_MONTH: |
|
390 |
+ |
// return get_M_nChunk( mYMDaccessor, length ); |
|
391 |
+ |
// case DateRes.INDICATOR_DAY: |
|
392 |
+ |
// return get_d_nChunk( mYMDaccessor, length ); |
|
393 |
+ |
// default: |
|
394 |
+ |
// break; |
|
395 |
+ |
// } |
|
396 |
+ |
// } |
|
397 |
+ |
// return getLiteralChunk( mFormat.substring( pFrom, pTo ) ); |
|
398 |
+ |
// } |
|
399 |
+ |
// } |
|
400 |
+ |
// |
|
401 |
+ |
// protected static Chunk getLiteralChunk( String pLiteral ) |
|
402 |
+ |
// { |
|
403 |
+ |
// if ( pLiteral.length() == 1 ) |
|
404 |
+ |
// { |
|
405 |
+ |
// switch ( pLiteral.charAt( 0 ) ) |
|
406 |
+ |
// { |
|
407 |
+ |
// case '/': |
|
408 |
+ |
// return SLASH; |
|
409 |
+ |
// case ' ': |
|
410 |
+ |
// return SPACE; |
|
411 |
+ |
// case ':': |
|
412 |
+ |
// return COLON; |
|
413 |
+ |
// case ',': |
|
414 |
+ |
// return COMMA; |
|
415 |
+ |
// case '-': |
|
416 |
+ |
// return DASH; |
|
417 |
+ |
// default: |
|
418 |
+ |
// break; |
|
419 |
+ |
// } |
|
420 |
+ |
// } |
|
421 |
+ |
// return new LiteralChunk( pLiteral ); |
|
422 |
+ |
// } |
|
423 |
+ |
// |
|
424 |
+ |
// protected static Chunk get_M_nChunk( YearMonthDayAccessor pYMDaccessor, int pLength ) |
|
425 |
+ |
// { |
|
426 |
+ |
// switch ( pLength ) |
|
427 |
+ |
// { |
|
428 |
+ |
// case 1: |
|
429 |
+ |
// return new M_Chunk( pYMDaccessor ); |
|
430 |
+ |
// case 2: |
|
431 |
+ |
// return new MM_Chunk( pYMDaccessor ); |
|
432 |
+ |
// case 3: |
|
433 |
+ |
// return new MMM_Chunk( pYMDaccessor ); |
|
434 |
+ |
// default: |
|
435 |
+ |
// return new MMMM_Chunk( pYMDaccessor ); |
|
436 |
+ |
// } |
|
437 |
+ |
// } |
|
438 |
+ |
// |
|
439 |
+ |
// protected static Chunk get_y_nChunk( YearMonthDayAccessor pYMDaccessor, int pLength ) |
|
440 |
+ |
// { |
|
441 |
+ |
// switch ( pLength ) |
|
442 |
+ |
// { |
|
443 |
+ |
// default: |
|
444 |
+ |
// return new yyy_Chunk( pYMDaccessor, pLength ); |
|
445 |
+ |
// case 2: |
|
446 |
+ |
// return new yy_Chunk( pYMDaccessor ); |
|
447 |
+ |
// case 1: |
|
448 |
+ |
// return new y_Chunk( pYMDaccessor ); |
|
449 |
+ |
// } |
|
450 |
+ |
// } |
|
451 |
+ |
// |
|
452 |
+ |
// protected static Chunk get_d_nChunk( YearMonthDayAccessor pYMDaccessor, int pLength ) |
|
453 |
+ |
// { |
|
454 |
+ |
// return (pLength == 2) ? new dd_Chunk( pYMDaccessor ) : new d_Chunk( pYMDaccessor ); |
|
455 |
+ |
// } |
|
456 |
+ |
// |
|
457 |
+ |
// @Override |
|
458 |
+ |
// public boolean equals( Object o ) |
|
459 |
+ |
// { |
|
460 |
+ |
// return (o instanceof DateFormatControl) && equals( (DateFormatControl) o ); |
|
461 |
+ |
// } |
|
462 |
+ |
// |
|
463 |
+ |
// public boolean equals( DateFormatControl them ) |
|
464 |
+ |
// { |
|
465 |
+ |
// return (this == them) || // |
|
466 |
+ |
// ((them != null) // |
|
467 |
+ |
// && this.mFormat.equals( them.mFormat ) // |
|
468 |
+ |
// ); |
|
469 |
+ |
// } |
|
470 |
+ |
// |
|
471 |
+ |
// @Override |
|
472 |
+ |
// public int hashCode() |
|
473 |
+ |
// { |
|
474 |
+ |
// return mFormat.hashCode(); |
|
475 |
+ |
// } |
|
476 |
+ |
// |
|
477 |
+ |
// @Override |
|
478 |
+ |
// public int compareTo( T them ) |
|
479 |
+ |
// { |
|
480 |
+ |
// return this.mFormat.compareTo( them.mFormat ); |
|
481 |
+ |
// } |
482 |
482 |
|
} |