#689 Joda LocalTime to XmlGregorianCalendar built in and vice versa

This commit is contained in:
sjaakd 2016-09-05 17:19:29 +02:00
parent aec5922bd0
commit a1131e68c9
9 changed files with 442 additions and 0 deletions

View File

@ -63,6 +63,8 @@ public class BuiltInMappingMethods {
builtInMethods.add( new XmlGregorianCalendarToJodaLocalDateTime( typeFactory ) );
builtInMethods.add( new JodaLocalDateToXmlGregorianCalendar( typeFactory ) );
builtInMethods.add( new XmlGregorianCalendarToJodaLocalDate( typeFactory ) );
builtInMethods.add( new JodaLocalTimeToXmlGregorianCalendar( typeFactory ) );
builtInMethods.add( new XmlGregorianCalendarToJodaLocalTime( 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 JodaLocalTimeToXmlGregorianCalendar extends BuiltInMethod {
private final Parameter parameter;
private final Type returnType;
private final Set<Type> importTypes;
public JodaLocalTimeToXmlGregorianCalendar(TypeFactory typeFactory) {
this.parameter = new Parameter(
"dt",
typeFactory.getType( JodaTimeConstants.LOCAL_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 XmlGregorianCalendarToJodaLocalTime extends BuiltInMethod {
private final Parameter parameter;
private final Type returnType;
private final Set<Type> importTypes;
public XmlGregorianCalendarToJodaLocalTime(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.returnType = typeFactory.getType( JodaTimeConstants.LOCAL_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,37 @@
<#--
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}( LocalTime dt ) {
if ( dt == null ) {
return null;
}
try {
return DatatypeFactory.newInstance().newXMLGregorianCalendarTime(
dt.getHourOfDay(),
dt.getMinuteOfHour(),
dt.getSecondOfMinute(),
dt.getMillisOfSecond(),
DatatypeConstants.FIELD_UNDEFINED );
}
catch ( DatatypeConfigurationException ex ) {
throw new RuntimeException( ex );
}
}

View File

@ -0,0 +1,50 @@
<#--
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 LocalTime ${name}( XMLGregorianCalendar xcal ) {
if ( xcal == null ) {
return null;
}
if ( xcal.getHour() != DatatypeConstants.FIELD_UNDEFINED
&& xcal.getMinute() != DatatypeConstants.FIELD_UNDEFINED ) {
if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED
&& xcal.getMillisecond() != DatatypeConstants.FIELD_UNDEFINED ) {
return new LocalTime( xcal.getHour(),
xcal.getMinute(),
xcal.getSecond(),
xcal.getMillisecond()
);
}
else if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED ) {
return new LocalTime(
xcal.getHour(),
xcal.getMinute(),
xcal.getSecond()
);
}
else {
return new LocalTime( xcal.getHour(),
xcal.getMinute()
);
}
}
return null;
}

View File

@ -27,18 +27,22 @@ import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
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.LocalDateBean;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.LocalDateTimeBean;
import org.mapstruct.ap.test.builtin.bean.jodatime.bean.LocalTimeBean;
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.LocalDateToXmlGregorianCalendar;
import org.mapstruct.ap.test.builtin.bean.jodatime.mapper.LocalTimeToXmlGregorianCalendar;
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.XmlGregorianCalendarToLocalTime;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
@ -49,6 +53,7 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
*/
@WithClasses({
DateTimeBean.class,
LocalTimeBean.class,
LocalDateBean.class,
LocalDateTimeBean.class,
XmlGregorianCalendarBean.class
@ -447,4 +452,99 @@ public class JodaTimeTest {
assertThat( res.getLocalDate() ).isNull();
}
@Test
@WithClasses(LocalTimeToXmlGregorianCalendar.class)
public void shouldMapIncompleteLocalTimeToXmlGregorianCalendar() {
LocalTimeBean in = new LocalTimeBean();
LocalTime dt = new LocalTime( 1, 1, 0, 100 );
in.setLocalTime( dt );
XmlGregorianCalendarBean res = LocalTimeToXmlGregorianCalendar.INSTANCE.toXmlGregorianCalendarBean( in );
assertThat( res.getxMLGregorianCalendar().getYear() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED );
assertThat( res.getxMLGregorianCalendar().getMonth() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED );
assertThat( res.getxMLGregorianCalendar().getDay() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED );
assertThat( res.getxMLGregorianCalendar().getHour() ).isEqualTo( 1 );
assertThat( res.getxMLGregorianCalendar().getMinute() ).isEqualTo( 1 );
assertThat( res.getxMLGregorianCalendar().getSecond() ).isEqualTo( 0 );
assertThat( res.getxMLGregorianCalendar().getMillisecond() ).isEqualTo( 100 );
assertThat( res.getxMLGregorianCalendar().getTimezone() ).isEqualTo( DatatypeConstants.FIELD_UNDEFINED );
}
@Test
@WithClasses(XmlGregorianCalendarToLocalTime.class)
public void shouldMapXmlGregorianCalendarToLocalTime() throws Exception {
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
XMLGregorianCalendar xcal =
DatatypeFactory.newInstance().newXMLGregorianCalendarTime( 1, 1, 1, 100, 60 );
in.setxMLGregorianCalendar( xcal );
LocalTimeBean res = XmlGregorianCalendarToLocalTime.INSTANCE.toLocalTimeBean( in );
assertThat( res.getLocalTime().getHourOfDay() ).isEqualTo( 1 );
assertThat( res.getLocalTime().getMinuteOfHour() ).isEqualTo( 1 );
assertThat( res.getLocalTime().getSecondOfMinute() ).isEqualTo( 1 );
assertThat( res.getLocalTime().getMillisOfSecond() ).isEqualTo( 100 );
}
@Test
@WithClasses(XmlGregorianCalendarToLocalTime.class)
public void shouldMapXmlGregorianCalendarWithoutMillisToLocalTime() throws Exception {
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
xcal.setHour( 23 );
xcal.setMinute( 34 );
xcal.setSecond( 45 );
in.setxMLGregorianCalendar( xcal );
LocalTimeBean res = XmlGregorianCalendarToLocalTime.INSTANCE.toLocalTimeBean( in );
assertThat( res.getLocalTime().getHourOfDay() ).isEqualTo( 23 );
assertThat( res.getLocalTime().getMinuteOfHour() ).isEqualTo( 34 );
assertThat( res.getLocalTime().getSecondOfMinute() ).isEqualTo( 45 );
assertThat( res.getLocalTime().getMillisOfSecond() ).isEqualTo( 0 );
}
@Test
@WithClasses(XmlGregorianCalendarToLocalTime.class)
public void shouldMapXmlGregorianCalendarWithoutSecondsToLocalTime() throws Exception {
XmlGregorianCalendarBean in = new XmlGregorianCalendarBean();
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
xcal.setHour( 23 );
xcal.setMinute( 34 );
xcal.setTimezone( 60 );
in.setxMLGregorianCalendar( xcal );
LocalTimeBean res = XmlGregorianCalendarToLocalTime.INSTANCE.toLocalTimeBean( in );
assertThat( res.getLocalTime().getHourOfDay() ).isEqualTo( 23 );
assertThat( res.getLocalTime().getMinuteOfHour() ).isEqualTo( 34 );
assertThat( res.getLocalTime().getSecondOfMinute() ).isEqualTo( 0 );
assertThat( res.getLocalTime().getMillisOfSecond() ).isEqualTo( 0 );
}
@Test
@WithClasses(XmlGregorianCalendarToLocalTime.class)
public void shouldNotMapXmlGregorianCalendarWithoutMinutesToLocalTime() 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 );
LocalTimeBean res = XmlGregorianCalendarToLocalTime.INSTANCE.toLocalTimeBean( in );
assertThat( res.getLocalTime() ).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.LocalTime;
/**
*
* @author Sjaak Derksen
*/
public class LocalTimeBean {
private LocalTime localTime;
public LocalTime getLocalTime() {
return localTime;
}
public void setLocalTime(LocalTime localTime) {
this.localTime = localTime;
}
}

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