#120 Making built-in methods null-safe

This commit is contained in:
Gunnar Morling 2014-02-22 19:54:54 +01:00
parent 32f0bc0e22
commit bc5dda82f8
7 changed files with 35 additions and 6 deletions

View File

@ -19,6 +19,10 @@
--> -->
private XMLGregorianCalendar ${name}( Calendar cal ) { private XMLGregorianCalendar ${name}( Calendar cal ) {
if ( cal == null ) {
return null;
}
try { try {
GregorianCalendar gcal = new GregorianCalendar(); GregorianCalendar gcal = new GregorianCalendar();
gcal.setTimeInMillis( cal.getTimeInMillis() ); gcal.setTimeInMillis( cal.getTimeInMillis() );
@ -27,4 +31,4 @@ private XMLGregorianCalendar ${name}( Calendar cal ) {
catch ( DatatypeConfigurationException ex ) { catch ( DatatypeConfigurationException ex ) {
throw new RuntimeException( ex ); throw new RuntimeException( ex );
} }
} }

View File

@ -19,6 +19,10 @@
--> -->
private XMLGregorianCalendar ${name}( Date date ) { private XMLGregorianCalendar ${name}( Date date ) {
if ( date == null ) {
return null;
}
try { try {
GregorianCalendar c = new GregorianCalendar(); GregorianCalendar c = new GregorianCalendar();
c.setTime( date ); c.setTime( date );
@ -27,4 +31,4 @@ private XMLGregorianCalendar ${name}( Date date ) {
catch ( DatatypeConfigurationException ex ) { catch ( DatatypeConfigurationException ex ) {
throw new RuntimeException( ex ); throw new RuntimeException( ex );
} }
} }

View File

@ -19,5 +19,9 @@
--> -->
private <T> T ${name}( JAXBElement <T> element ) { private <T> T ${name}( JAXBElement <T> element ) {
if ( element == null ) {
return null;
}
return element.isNil() ? null : element.getValue(); return element.isNil() ? null : element.getValue();
} }

View File

@ -19,6 +19,10 @@
--> -->
private XMLGregorianCalendar ${name}( String date, String dateFormat ) { private XMLGregorianCalendar ${name}( String date, String dateFormat ) {
if ( date == null ) {
return null;
}
try { try {
DateFormat df = dateFormat != null ? new SimpleDateFormat( dateFormat ) : SimpleDateFormat.getInstance(); DateFormat df = dateFormat != null ? new SimpleDateFormat( dateFormat ) : SimpleDateFormat.getInstance();
GregorianCalendar c = new GregorianCalendar(); GregorianCalendar c = new GregorianCalendar();
@ -31,4 +35,4 @@ private XMLGregorianCalendar ${name}( String date, String dateFormat ) {
catch ( ParseException ex ) { catch ( ParseException ex ) {
throw new RuntimeException( ex ); throw new RuntimeException( ex );
} }
} }

View File

@ -19,6 +19,10 @@
--> -->
private Calendar ${name}( XMLGregorianCalendar xcal ) { private Calendar ${name}( XMLGregorianCalendar xcal ) {
if ( xcal == null ) {
return null;
}
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.setTimeInMillis( xcal.toGregorianCalendar().getTimeInMillis() ); cal.setTimeInMillis( xcal.toGregorianCalendar().getTimeInMillis() );
return cal; return cal;

View File

@ -19,5 +19,9 @@
--> -->
private static Date ${name}( XMLGregorianCalendar xcal ) { private static Date ${name}( XMLGregorianCalendar xcal ) {
if ( xcal == null ) {
return null;
}
return xcal.toGregorianCalendar().getTime(); return xcal.toGregorianCalendar().getTime();
} }

View File

@ -19,11 +19,16 @@
--> -->
private String ${name}( XMLGregorianCalendar xcal, String dateFormat ) { private String ${name}( XMLGregorianCalendar xcal, String dateFormat ) {
if ( xcal == null ) {
return null;
}
if (dateFormat == null ) { if (dateFormat == null ) {
return xcal.toString(); return xcal.toString();
} else { }
else {
Date d = xcal.toGregorianCalendar().getTime(); Date d = xcal.toGregorianCalendar().getTime();
SimpleDateFormat sdf = new SimpleDateFormat( dateFormat ); SimpleDateFormat sdf = new SimpleDateFormat( dateFormat );
return sdf.format( d ); return sdf.format( d );
} }
} }