#689 Joda LocalDateTime to XmlGregorianCalendar built in and vice versa

This commit is contained in:
sjaakd 2016-09-04 22:40:27 +02:00
parent 13395f5958
commit e43b00d29c
9 changed files with 484 additions and 0 deletions

View File

@ -59,6 +59,8 @@ public class BuiltInMappingMethods {
if ( isJodaTimeAvailable( typeFactory ) ) {
builtInMethods.add( new JodaDateTimeToXmlGregorianCalendar( typeFactory ) );
builtInMethods.add( new XmlGregorianCalendarToJodaDateTime( typeFactory ) );
builtInMethods.add( new JodaLocalDateTimeToXmlGregorianCalendar( typeFactory ) );
builtInMethods.add( new XmlGregorianCalendarToJodaLocalDateTime( typeFactory ) );
}
}

View File

@ -0,0 +1,74 @@
/**
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.internal.model.source.builtin;
import static org.mapstruct.ap.internal.util.Collections.asSet;
import java.util.Set;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.JodaTimeConstants;
/**
* @author Sjaak Derksen
*/
public class JodaLocalDateTimeToXmlGregorianCalendar extends BuiltInMethod {
private final Parameter parameter;
private final Type returnType;
private final Set<Type> importTypes;
public JodaLocalDateTimeToXmlGregorianCalendar(TypeFactory typeFactory) {
this.parameter = new Parameter(
"dt",
typeFactory.getType( JodaTimeConstants.LOCAL_DATE_TIME_FQN )
);
this.returnType = typeFactory.getType( XMLGregorianCalendar.class );
this.importTypes = asSet(
returnType,
parameter.getType(),
typeFactory.getType( DatatypeConstants.class ),
typeFactory.getType( DatatypeFactory.class ),
typeFactory.getType( DatatypeConfigurationException.class )
);
}
@Override
public Set<Type> getImportTypes() {
return importTypes;
}
@Override
public Parameter getParameter() {
return parameter;
}
@Override
public Type getReturnType() {
return returnType;
}
}

View File

@ -0,0 +1,64 @@
/**
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.internal.model.source.builtin;
import static org.mapstruct.ap.internal.util.Collections.asSet;
import java.util.Set;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.JodaTimeConstants;
/**
* @author Sjaak Derksen
*/
public class XmlGregorianCalendarToJodaLocalDateTime extends BuiltInMethod {
private final Parameter parameter;
private final Type returnType;
private final Set<Type> importTypes;
public XmlGregorianCalendarToJodaLocalDateTime(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.returnType = typeFactory.getType( JodaTimeConstants.LOCAL_DATE_TIME_FQN );
this.importTypes = asSet(
typeFactory.getType( DatatypeConstants.class ),
returnType,
parameter.getType() );
}
@Override
public Parameter getParameter() {
return parameter;
}
@Override
public Type getReturnType() {
return returnType;
}
@Override
public Set<Type> getImportTypes() {
return importTypes;
}
}

View File

@ -0,0 +1,40 @@
<#--
Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
and/or other contributors as indicated by the @authors tag. See the
copyright.txt file in the distribution for a full listing of all
contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
private XMLGregorianCalendar ${name}( LocalDateTime dt ) {
if ( dt == null ) {
return null;
}
try {
return DatatypeFactory.newInstance().newXMLGregorianCalendar(
dt.getYear(),
dt.getMonthOfYear(),
dt.getDayOfMonth(),
dt.getHourOfDay(),
dt.getMinuteOfHour(),
dt.getSecondOfMinute(),
dt.getMillisOfSecond(),
DatatypeConstants.FIELD_UNDEFINED );
}
catch ( DatatypeConfigurationException ex ) {
throw new RuntimeException( ex );
}
}

View File

@ -0,0 +1,62 @@
<#--
Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
and/or other contributors as indicated by the @authors tag. See the
copyright.txt file in the distribution for a full listing of all
contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
private static LocalDateTime ${name}( XMLGregorianCalendar xcal ) {
if ( xcal == null ) {
return null;
}
if ( xcal.getYear() != DatatypeConstants.FIELD_UNDEFINED
&& xcal.getMonth() != DatatypeConstants.FIELD_UNDEFINED
&& xcal.getDay() != DatatypeConstants.FIELD_UNDEFINED
&& xcal.getHour() != DatatypeConstants.FIELD_UNDEFINED
&& xcal.getMinute() != DatatypeConstants.FIELD_UNDEFINED
) {
if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED
&& xcal.getMillisecond() != DatatypeConstants.FIELD_UNDEFINED ) {
return new LocalDateTime( xcal.getYear(),
xcal.getMonth(),
xcal.getDay(),
xcal.getHour(),
xcal.getMinute(),
xcal.getSecond(),
xcal.getMillisecond()
);
}
else if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED ) {
return new LocalDateTime( xcal.getYear(),
xcal.getMonth(),
xcal.getDay(),
xcal.getHour(),
xcal.getMinute(),
xcal.getSecond()
);
}
else {
return new LocalDateTime( xcal.getYear(),
xcal.getMonth(),
xcal.getDay(),
xcal.getHour(),
xcal.getMinute()
);
}
}
return null;
}

View File

@ -19,17 +19,22 @@
package org.mapstruct.ap.test.builtin.bean.jodatime;
import java.util.TimeZone;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import static org.assertj.core.api.Assertions.assertThat;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.DateTimeBean;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.LocalDateTimeBean;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.XmlGregorianCalendarBean;
import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.DateTimeToXmlGregorianCalendar;
import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.LocalDateTimeToXmlGregorianCalendar;
import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.XmlGregorianCalendarToDateTime;
import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.XmlGregorianCalendarToLocalDateTime;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
@ -40,6 +45,7 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
*/
@WithClasses({
DateTimeBean.class,
LocalDateTimeBean.class,
XmlGregorianCalendarBean.class
})
@RunWith(AnnotationProcessorTestRunner.class)
@ -260,4 +266,125 @@ public class JodaTimeTest {
}
@Test
@WithClasses(LocalDateTimeToXmlGregorianCalendar.class)
public void shouldMapLocalDateTimeToXmlGregorianCalendar() {
LocalDateTimeBean in = new LocalDateTimeBean();
LocalDateTime dt = new LocalDateTime(2010, 1, 15, 1, 1, 1, 100 );
in.setLocalDateTime( dt );
XmlGregorianCalendarBean res = LocalDateTimeToXmlGregorianCalendar.INSTANCE.toXmlGregorianCalendarBean( in );
assertThat( res.getxMLGregorianCalendar().getYear() ).isEqualTo( 2010 );
assertThat( res.getxMLGregorianCalendar().getMonth() ).isEqualTo( 1 );
assertThat( res.getxMLGregorianCalendar().getDay() ).isEqualTo( 15 );
assertThat( res.getxMLGregorianCalendar().getHour() ).isEqualTo( 1 );
assertThat( res.getxMLGregorianCalendar().getMinute() ).isEqualTo( 1 );
assertThat( res.getxMLGregorianCalendar().getSecond() ).isEqualTo( 1 );
assertThat( res.getxMLGregorianCalendar().getMillisecond() ).isEqualTo( 100 );
assertThat( res.getxMLGregorianCalendar().getTimezone() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED );
}
@Test
@WithClasses(LocalDateTimeToXmlGregorianCalendar.class)
public void shouldMapIncompleteLocalDateTimeToXmlGregorianCalendar() {
LocalDateTimeBean in = new LocalDateTimeBean();
LocalDateTime dt = new LocalDateTime( 2010, 1, 15, 1, 1 );
in.setLocalDateTime( dt );
XmlGregorianCalendarBean res = LocalDateTimeToXmlGregorianCalendar.INSTANCE.toXmlGregorianCalendarBean( in );
assertThat( res.getxMLGregorianCalendar().getYear() ).isEqualTo( 2010 );
assertThat( res.getxMLGregorianCalendar().getMonth() ).isEqualTo( 1 );
assertThat( res.getxMLGregorianCalendar().getDay() ).isEqualTo( 15 );
assertThat( res.getxMLGregorianCalendar().getHour() ).isEqualTo( 1 );
assertThat( res.getxMLGregorianCalendar().getMinute() ).isEqualTo( 1 );
assertThat( res.getxMLGregorianCalendar().getSecond() ).isEqualTo( 0 );
assertThat( res.getxMLGregorianCalendar().getMillisecond() ).isEqualTo( 0 );
assertThat( res.getxMLGregorianCalendar().getTimezone() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED );
}
@Test
@WithClasses(XmlGregorianCalendarToLocalDateTime.class)
public void shouldMapXmlGregorianCalendarToLocalDateTime() throws Exception {
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
XMLGregorianCalendar xcal =
DatatypeFactory.newInstance().newXMLGregorianCalendar( 2010, 1, 15, 1, 1, 1, 100, 60 );
in.setxMLGregorianCalendar( xcal );
LocalDateTimeBean res = XmlGregorianCalendarToLocalDateTime.INSTANCE.toDateTimeBean( in );
assertThat( res.getLocalDateTime().getYear() ).isEqualTo( 2010 );
assertThat( res.getLocalDateTime().getMonthOfYear() ).isEqualTo( 1 );
assertThat( res.getLocalDateTime().getDayOfMonth() ).isEqualTo( 15 );
assertThat( res.getLocalDateTime().getHourOfDay() ).isEqualTo( 1 );
assertThat( res.getLocalDateTime().getMinuteOfHour() ).isEqualTo( 1 );
assertThat( res.getLocalDateTime().getSecondOfMinute() ).isEqualTo( 1 );
assertThat( res.getLocalDateTime().getMillisOfSecond() ).isEqualTo( 100 );
}
@Test
@WithClasses(XmlGregorianCalendarToLocalDateTime.class)
public void shouldMapXmlGregorianCalendarWithoutMillisToLocalDateTime() throws Exception {
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
xcal.setYear( 1999 );
xcal.setMonth( 5 );
xcal.setDay( 25 );
xcal.setHour( 23 );
xcal.setMinute( 34 );
xcal.setSecond( 45 );
in.setxMLGregorianCalendar( xcal );
LocalDateTimeBean res = XmlGregorianCalendarToLocalDateTime.INSTANCE.toDateTimeBean( in );
assertThat( res.getLocalDateTime().getYear() ).isEqualTo( 1999 );
assertThat( res.getLocalDateTime().getMonthOfYear() ).isEqualTo( 5 );
assertThat( res.getLocalDateTime().getDayOfMonth() ).isEqualTo( 25 );
assertThat( res.getLocalDateTime().getHourOfDay() ).isEqualTo( 23 );
assertThat( res.getLocalDateTime().getMinuteOfHour() ).isEqualTo( 34 );
assertThat( res.getLocalDateTime().getSecondOfMinute() ).isEqualTo( 45 );
assertThat( res.getLocalDateTime().getMillisOfSecond() ).isEqualTo( 0 );
}
@Test
@WithClasses(XmlGregorianCalendarToLocalDateTime.class)
public void shouldMapXmlGregorianCalendarWithoutSecondsToLocalDateTime() throws Exception {
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
xcal.setYear( 1999 );
xcal.setMonth( 5 );
xcal.setDay( 25 );
xcal.setHour( 23 );
xcal.setMinute( 34 );
xcal.setTimezone( 60 );
in.setxMLGregorianCalendar( xcal );
LocalDateTimeBean res = XmlGregorianCalendarToLocalDateTime.INSTANCE.toDateTimeBean( in );
assertThat( res.getLocalDateTime().getYear() ).isEqualTo( 1999 );
assertThat( res.getLocalDateTime().getMonthOfYear() ).isEqualTo( 5 );
assertThat( res.getLocalDateTime().getDayOfMonth() ).isEqualTo( 25 );
assertThat( res.getLocalDateTime().getHourOfDay() ).isEqualTo( 23 );
assertThat( res.getLocalDateTime().getMinuteOfHour() ).isEqualTo( 34 );
assertThat( res.getLocalDateTime().getSecondOfMinute() ).isEqualTo( 0 );
assertThat( res.getLocalDateTime().getMillisOfSecond() ).isEqualTo( 0 );
}
@Test
@WithClasses(XmlGregorianCalendarToLocalDateTime.class)
public void shouldNotMapXmlGregorianCalendarWithoutMinutesToLocalDateTime() throws Exception {
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
xcal.setYear( 1999 );
xcal.setMonth( 5 );
xcal.setDay( 25 );
xcal.setHour( 23 );
in.setxMLGregorianCalendar( xcal );
LocalDateTimeBean res = XmlGregorianCalendarToLocalDateTime.INSTANCE.toDateTimeBean( in );
assertThat( res.getLocalDateTime() ).isNull();
}
}

View File

@ -0,0 +1,38 @@
/**
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.builtin.bean.jodatime.bean;
import org.joda.time.LocalDateTime;
/**
*
* @author Sjaak Derksen
*/
public class LocalDateTimeBean {
private LocalDateTime localDateTime;
public LocalDateTime getLocalDateTime() {
return localDateTime;
}
public void setLocalDateTime(LocalDateTime localDateTime) {
this.localDateTime = localDateTime;
}
}

View File

@ -0,0 +1,39 @@
/**
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.builtin.bean.jodatime.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.LocalDateTimeBean;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.XmlGregorianCalendarBean;
import org.mapstruct.factory.Mappers;
/**
*
* @author Sjaak Derksen
*/
@Mapper
public interface LocalDateTimeToXmlGregorianCalendar {
LocalDateTimeToXmlGregorianCalendar INSTANCE = Mappers.getMapper( LocalDateTimeToXmlGregorianCalendar.class );
@Mapping( target = "xMLGregorianCalendar", source = "localDateTime")
XmlGregorianCalendarBean toXmlGregorianCalendarBean( LocalDateTimeBean in );
}

View File

@ -0,0 +1,38 @@
/**
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.builtin.bean.jodatime.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.LocalDateTimeBean;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.XmlGregorianCalendarBean;
import org.mapstruct.factory.Mappers;
/**
*
* @author Sjaak Derksen
*/
@Mapper
public interface XmlGregorianCalendarToLocalDateTime {
XmlGregorianCalendarToLocalDateTime INSTANCE = Mappers.getMapper( XmlGregorianCalendarToLocalDateTime.class );
@Mapping( target = "localDateTime", source = "xMLGregorianCalendar" )
LocalDateTimeBean toDateTimeBean( XmlGregorianCalendarBean in );
}