Skip to content

Commit a891490

Browse files
committed
More JavaDoc
1 parent 1c61aa7 commit a891490

File tree

2 files changed

+174
-38
lines changed

2 files changed

+174
-38
lines changed

src/main/java/com/toddfast/util/convert/TypeConverter.java

+163-38
Original file line numberDiff line numberDiff line change
@@ -427,190 +427,315 @@ private static Conversion<?> getTypeConversion(
427427
/**
428428
* Return the value converted to a byte
429429
*
430+
* @param value
431+
* The value to be converted
432+
* @throws IllegalArgumentException
433+
* If the value cannot be converted
430434
*/
431435
public static byte asByte(Object value) {
432436
return asByte(value,(byte)0);
433437
}
434438

435439
/**
436-
* Return the value converted to a byte or the default value
440+
* Return the value converted to a byte
441+
* or the specified alternate value if the original value is null. Note,
442+
* this method still throws {@link IllegalArgumentException} if the value
443+
* is not null and could not be converted.
437444
*
438-
*/
439-
public static byte asByte(Object value, byte defaultValue) {
445+
* @param value
446+
* The value to be converted
447+
* @param nullValue
448+
* The value to be returned if {@link value} is null. Note, this
449+
* value will not be returned if the conversion fails otherwise.
450+
* @throws IllegalArgumentException
451+
* If the value cannot be converted
452+
*/
453+
public static byte asByte(Object value, byte nullValue) {
440454
value=convert(Byte.class,value);
441455
if (value!=null) {
442456
return ((Byte)value).byteValue();
443457
}
444458
else {
445-
return defaultValue;
459+
return nullValue;
446460
}
447461
}
448462

449463
/**
450464
* Return the value converted to a short
451465
*
466+
* @param value
467+
* The value to be converted
468+
* @throws IllegalArgumentException
469+
* If the value cannot be converted
452470
*/
453471
public static short asShort(Object value) {
454472
return asShort(value,(short)0);
455473
}
456474

457475
/**
458-
* Return the value converted to a short or the default value
476+
* Return the value converted to a short
477+
* or the specified alternate value if the original value is null. Note,
478+
* this method still throws {@link IllegalArgumentException} if the value
479+
* is not null and could not be converted.
459480
*
460-
*/
461-
public static short asShort(Object value, short defaultValue) {
481+
* @param value
482+
* The value to be converted
483+
* @param nullValue
484+
* The value to be returned if {@link value} is null. Note, this
485+
* value will not be returned if the conversion fails otherwise.
486+
* @throws IllegalArgumentException
487+
* If the value cannot be converted
488+
*/
489+
public static short asShort(Object value, short nullValue) {
462490
value=convert(Short.class,value);
463491
if (value!=null) {
464492
return ((Short)value).shortValue();
465493
}
466494
else {
467-
return defaultValue;
495+
return nullValue;
468496
}
469497
}
470498

471499
/**
472500
* Return the value converted to an int
473501
*
502+
* @param value
503+
* The value to be converted
504+
* @throws IllegalArgumentException
505+
* If the value cannot be converted
474506
*/
475507
public static int asInt(Object value) {
476508
return asInt(value,0);
477509
}
478510

479511
/**
480-
* Return the value converted to an int or the default value
512+
* Return the value converted to an int
513+
* or the specified alternate value if the original value is null. Note,
514+
* this method still throws {@link IllegalArgumentException} if the value
515+
* is not null and could not be converted.
481516
*
482-
*/
483-
public static int asInt(Object value, int defaultValue) {
517+
* @param value
518+
* The value to be converted
519+
* @param nullValue
520+
* The value to be returned if {@link value} is null. Note, this
521+
* value will not be returned if the conversion fails otherwise.
522+
* @throws IllegalArgumentException
523+
* If the value cannot be converted
524+
*/
525+
public static int asInt(Object value, int nullValue) {
484526
value=convert(Integer.class,value);
485527
if (value!=null) {
486528
return ((Integer)value).intValue();
487529
}
488530
else {
489-
return defaultValue;
531+
return nullValue;
490532
}
491533
}
492534

493535
/**
494536
* Return the value converted to a long
495537
*
538+
* @param value
539+
* The value to be converted
540+
* @throws IllegalArgumentException
541+
* If the value cannot be converted
496542
*/
497543
public static long asLong(Object value) {
498544
return asLong(value,0L);
499545
}
500546

501547
/**
502-
* Return the value converted to a long or the default value
548+
* Return the value converted to a long
549+
* or the specified alternate value if the original value is null. Note,
550+
* this method still throws {@link IllegalArgumentException} if the value
551+
* is not null and could not be converted.
503552
*
504-
*/
505-
public static long asLong(Object value, long defaultValue) {
553+
* @param value
554+
* The value to be converted
555+
* @param nullValue
556+
* The value to be returned if {@link value} is null. Note, this
557+
* value will not be returned if the conversion fails otherwise.
558+
* @throws IllegalArgumentException
559+
* If the value cannot be converted
560+
*/
561+
public static long asLong(Object value, long nullValue) {
506562
value=convert(Long.class,value);
507563
if (value!=null) {
508564
return ((Long)value).longValue();
509565
}
510566
else {
511-
return defaultValue;
567+
return nullValue;
512568
}
513569
}
514570

515571
/**
516572
* Return the value converted to a float
517573
*
574+
* @param value
575+
* The value to be converted
576+
* @throws IllegalArgumentException
577+
* If the value cannot be converted
518578
*/
519579
public static float asFloat(Object value) {
520580
return asFloat(value,0F);
521581
}
522582

523583
/**
524-
* Return the value converted to a float or the default value
584+
* Return the value converted to a float
585+
* or the specified alternate value if the original value is null. Note,
586+
* this method still throws {@link IllegalArgumentException} if the value
587+
* is not null and could not be converted.
525588
*
526-
*/
527-
public static float asFloat(Object value, float defaultValue) {
589+
* @param value
590+
* The value to be converted
591+
* @param nullValue
592+
* The value to be returned if {@link value} is null. Note, this
593+
* value will not be returned if the conversion fails otherwise.
594+
* @throws IllegalArgumentException
595+
* If the value cannot be converted
596+
*/
597+
public static float asFloat(Object value, float nullValue) {
528598
value=convert(Float.class,value);
529599
if (value!=null) {
530600
return ((Float)value).floatValue();
531601
}
532602
else {
533-
return defaultValue;
603+
return nullValue;
534604
}
535605
}
536606

537607
/**
538608
* Return the value converted to a double
539609
*
610+
* @param value
611+
* The value to be converted
612+
* @throws IllegalArgumentException
613+
* If the value cannot be converted
540614
*/
541-
public static double asDouble(Object value)
542-
{
615+
public static double asDouble(Object value) {
543616
return asDouble(value,0D);
544617
}
545618

546619
/**
547-
* Return the value converted to a double or the default value
620+
* Return the value converted to a double
621+
* or the specified alternate value if the original value is null. Note,
622+
* this method still throws {@link IllegalArgumentException} if the value
623+
* is not null and could not be converted.
548624
*
549-
*/
550-
public static double asDouble(Object value, double defaultValue) {
625+
* @param value
626+
* The value to be converted
627+
* @param nullValue
628+
* The value to be returned if {@link value} is null. Note, this
629+
* value will not be returned if the conversion fails otherwise.
630+
* @throws IllegalArgumentException
631+
* If the value cannot be converted
632+
*/
633+
public static double asDouble(Object value, double nullValue) {
551634
value=convert(Double.class,value);
552635
return (value!=null)
553636
? ((Double)value).doubleValue()
554-
: defaultValue;
637+
: nullValue;
555638
}
556639

557640

558641
/**
559642
* Return the value converted to a char
560643
*
644+
* @param value
645+
* The value to be converted
646+
* @throws IllegalArgumentException
647+
* If the value cannot be converted
561648
*/
562649
public static char asChar(Object value) {
563650
return asChar(value,(char)0);
564651
}
565652

566653

567654
/**
568-
* Return the value converted to a char or the default value
655+
* Return the value converted to a char
656+
* or the specified alternate value if the original value is null. Note,
657+
* this method still throws {@link IllegalArgumentException} if the value
658+
* is not null and could not be converted.
569659
*
570-
*/
571-
public static char asChar(Object value, char defaultValue) {
660+
* @param value
661+
* The value to be converted
662+
* @param nullValue
663+
* The value to be returned if {@link value} is null. Note, this
664+
* value will not be returned if the conversion fails otherwise.
665+
* @throws IllegalArgumentException
666+
* If the value cannot be converted
667+
*/
668+
public static char asChar(Object value, char nullValue) {
572669
value=convert(Character.class,value);
573670
return (value!=null)
574671
? ((Character)value).charValue()
575-
: defaultValue;
672+
: nullValue;
576673
}
577674

578675
/**
579676
* Return the value converted to a boolean
580677
*
678+
* @param value
679+
* The value to be converted
680+
* @throws IllegalArgumentException
681+
* If the value cannot be converted
581682
*/
582683
public static boolean asBoolean(Object value) {
583684
return asBoolean(value,false);
584685
}
585686

586687
/**
587-
* Return the value converted to a boolean or the default value
688+
* Return the value converted to a boolean
689+
* or the specified alternate value if the original value is null. Note,
690+
* this method still throws {@link IllegalArgumentException} if the value
691+
* is not null and could not be converted.
588692
*
589-
*/
590-
public static boolean asBoolean(Object value, boolean defaultValue) {
693+
* @param value
694+
* The value to be converted
695+
* @param nullValue
696+
* The value to be returned if {@link value} is null. Note, this
697+
* value will not be returned if the conversion fails otherwise.
698+
* @throws IllegalArgumentException
699+
* If the value cannot be converted
700+
*/
701+
public static boolean asBoolean(Object value, boolean nullValue) {
591702
value=convert(Boolean.class,value);
592703
return (value!=null)
593704
? ((Boolean)value).booleanValue()
594-
: defaultValue;
705+
: nullValue;
595706
}
596707

597708
/**
598709
* Return the value converted to a string
599710
*
711+
* @param value
712+
* The value to be converted
713+
* @throws IllegalArgumentException
714+
* If the value cannot be converted
600715
*/
601716
public static String asString(Object value) {
602717
return (String)convert(String.class,value);
603718
}
604719

605720
/**
606-
* Return the value converted to a string or the default value
721+
* Return the value converted to a string
722+
* or the specified alternate value if the original value is null. Note,
723+
* this method still throws {@link IllegalArgumentException} if the value
724+
* is not null and could not be converted.
607725
*
608-
*/
609-
public static String asString(Object value, String defaultValue) {
726+
* @param value
727+
* The value to be converted
728+
* @param nullValue
729+
* The value to be returned if {@link value} is null. Note, this
730+
* value will not be returned if the conversion fails otherwise.
731+
* @throws IllegalArgumentException
732+
* If the value cannot be converted
733+
*/
734+
public static String asString(Object value, String nullValue) {
610735
value=convert(String.class,value);
611736
return (value!=null)
612737
? (String)value
613-
: defaultValue;
738+
: nullValue;
614739
}
615740

616741
/**

src/test/java/com/toddfast/util/convert/TypeConverterTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,15 @@ public void testConverter() {
131131
out=converter.convert(in);
132132
assertEquals("1.1",out);
133133
}
134+
135+
@Test(expected=IllegalArgumentException.class)
136+
public void testBogusInteger() {
137+
TypeConverter.asInt("bogus");
138+
}
139+
140+
@Test
141+
public void testBogusIntegerWithDefaultValue() {
142+
int out=TypeConverter.asInt(null,12);
143+
assertEquals(12,out);
144+
}
134145
}

0 commit comments

Comments
 (0)