#689 Joda LocalDate to XmlGregorianCalendar built in and vice versa

This commit is contained in:
sjaakd 2016-09-04 23:16:07 +02:00
parent e43b00d29c
commit aec5922bd0
9 changed files with 384 additions and 0 deletions

View File

@ -61,6 +61,8 @@ public class BuiltInMappingMethods {
builtInMethods.add( new XmlGregorianCalendarToJodaDateTime( typeFactory ) ); builtInMethods.add( new XmlGregorianCalendarToJodaDateTime( typeFactory ) );
builtInMethods.add( new JodaLocalDateTimeToXmlGregorianCalendar( typeFactory ) ); builtInMethods.add( new JodaLocalDateTimeToXmlGregorianCalendar( typeFactory ) );
builtInMethods.add( new XmlGregorianCalendarToJodaLocalDateTime( typeFactory ) ); builtInMethods.add( new XmlGregorianCalendarToJodaLocalDateTime( typeFactory ) );
builtInMethods.add( new JodaLocalDateToXmlGregorianCalendar( typeFactory ) );
builtInMethods.add( new XmlGregorianCalendarToJodaLocalDate( 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 JodaLocalDateToXmlGregorianCalendar extends BuiltInMethod {
private final Parameter parameter;
private final Type returnType;
private final Set<Type> importTypes;
public JodaLocalDateToXmlGregorianCalendar(TypeFactory typeFactory) {
this.parameter = new Parameter(
"dt",
typeFactory.getType( JodaTimeConstants.LOCAL_DATE_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 XmlGregorianCalendarToJodaLocalDate extends BuiltInMethod {
private final Parameter parameter;
private final Type returnType;
private final Set<Type> importTypes;
public XmlGregorianCalendarToJodaLocalDate(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.returnType = typeFactory.getType( JodaTimeConstants.LOCAL_DATE_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,36 @@
<#--
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}( LocalDate dt ) {
if ( dt == null ) {
return null;
}
try {
return DatatypeFactory.newInstance().newXMLGregorianCalendarDate(
dt.getYear(),
dt.getMonthOfYear(),
dt.getDayOfMonth(),
DatatypeConstants.FIELD_UNDEFINED );
}
catch ( DatatypeConfigurationException ex ) {
throw new RuntimeException( ex );
}
}

View File

@ -0,0 +1,33 @@
<#--
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 LocalDate ${name}( XMLGregorianCalendar xcal ) {
if ( xcal == null ) {
return null;
}
if ( xcal.getYear() != DatatypeConstants.FIELD_UNDEFINED
&& xcal.getMonth() != DatatypeConstants.FIELD_UNDEFINED
&& xcal.getDay() != DatatypeConstants.FIELD_UNDEFINED ) {
return new LocalDate( xcal.getYear(), xcal.getMonth(), xcal.getDay() );
}
return null;
}

View File

@ -25,15 +25,19 @@ import javax.xml.datatype.XMLGregorianCalendar;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime; import org.joda.time.LocalDateTime;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.DateTimeBean; import org.mapstruct.ap.test.builtin.bean.jodatime.bean.DateTimeBean;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.LocalDateBean;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.LocalDateTimeBean; 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.bean.XmlGregorianCalendarBean;
import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.DateTimeToXmlGregorianCalendar; 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.LocalDateTimeToXmlGregorianCalendar;
import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.LocalDateToXmlGregorianCalendar;
import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.XmlGregorianCalendarToDateTime; import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.XmlGregorianCalendarToDateTime;
import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.XmlGregorianCalendarToLocalDate;
import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.XmlGregorianCalendarToLocalDateTime; import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.XmlGregorianCalendarToLocalDateTime;
import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.WithClasses;
@ -45,6 +49,7 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
*/ */
@WithClasses({ @WithClasses({
DateTimeBean.class, DateTimeBean.class,
LocalDateBean.class,
LocalDateTimeBean.class, LocalDateTimeBean.class,
XmlGregorianCalendarBean.class XmlGregorianCalendarBean.class
}) })
@ -385,6 +390,61 @@ public class JodaTimeTest {
LocalDateTimeBean res = XmlGregorianCalendarToLocalDateTime.INSTANCE.toDateTimeBean( in ); LocalDateTimeBean res = XmlGregorianCalendarToLocalDateTime.INSTANCE.toDateTimeBean( in );
assertThat( res.getLocalDateTime() ).isNull(); assertThat( res.getLocalDateTime() ).isNull();
} }
@Test
@WithClasses(LocalDateToXmlGregorianCalendar.class)
public void shouldMapLocalDateToXmlGregorianCalendar() {
LocalDateBean in = new LocalDateBean();
LocalDate dt = new LocalDate(2010, 1, 15 );
in.setLocalDate( dt );
XmlGregorianCalendarBean res = LocalDateToXmlGregorianCalendar.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( DatatypeConstants.FIELD_UNDEFINED );
assertThat( res.getxMLGregorianCalendar().getMinute() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED );
assertThat( res.getxMLGregorianCalendar().getSecond() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED );
assertThat( res.getxMLGregorianCalendar().getMillisecond() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED );
assertThat( res.getxMLGregorianCalendar().getTimezone() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED );
}
@Test
@WithClasses(XmlGregorianCalendarToLocalDate.class)
public void shouldMapXmlGregorianCalendarToLocalDate() throws Exception {
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(
1999,
5,
25,
DatatypeConstants.FIELD_UNDEFINED );
in.setxMLGregorianCalendar( xcal );
LocalDateBean res = XmlGregorianCalendarToLocalDate.INSTANCE.toLocalDateBean( in );
assertThat( res.getLocalDate().getYear() ).isEqualTo( 1999 );
assertThat( res.getLocalDate().getMonthOfYear() ).isEqualTo( 5 );
assertThat( res.getLocalDate().getDayOfMonth() ).isEqualTo( 25 );
}
@Test
@WithClasses(XmlGregorianCalendarToLocalDate.class)
public void shouldNotMapXmlGregorianCalendarWithoutDaysToLocalDate() throws Exception {
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(
1999,
5,
DatatypeConstants.FIELD_UNDEFINED,
DatatypeConstants.FIELD_UNDEFINED );
in.setxMLGregorianCalendar( xcal );
LocalDateBean res = XmlGregorianCalendarToLocalDate.INSTANCE.toLocalDateBean( in );
assertThat( res.getLocalDate() ).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.LocalDate;
/**
*
* @author Sjaak Derksen
*/
public class LocalDateBean {
private LocalDate localDate;
public LocalDate getLocalDate() {
return localDate;
}
public void setLocalDate(LocalDate localDate) {
this.localDate = localDate;
}
}

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.LocalDateBean;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.XmlGregorianCalendarBean;
import org.mapstruct.factory.Mappers;
/**
*
* @author Sjaak Derksen
*/
@Mapper
public interface LocalDateToXmlGregorianCalendar {
LocalDateToXmlGregorianCalendar INSTANCE = Mappers.getMapper( LocalDateToXmlGregorianCalendar.class );
@Mapping( target = "xMLGregorianCalendar", source = "localDate")
XmlGregorianCalendarBean toXmlGregorianCalendarBean( LocalDateBean 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.LocalDateBean;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.XmlGregorianCalendarBean;
import org.mapstruct.factory.Mappers;
/**
*
* @author Sjaak Derksen
*/
@Mapper
public interface XmlGregorianCalendarToLocalDate {
XmlGregorianCalendarToLocalDate INSTANCE = Mappers.getMapper( XmlGregorianCalendarToLocalDate.class );
@Mapping( target = "localDate", source = "xMLGregorianCalendar" )
LocalDateBean toLocalDateBean( XmlGregorianCalendarBean in );
}