Skip to content

Commit c649198

Browse files
committed
mapstruct#852 Adding explicit conversion for LocalDate <> XMLGregorianCalendar
1 parent e136961 commit c649198

10 files changed

Lines changed: 206 additions & 9 deletions

File tree

processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMappingMethods.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public BuiltInMappingMethods(TypeFactory typeFactory) {
5151
if ( isJava8TimeAvailable( typeFactory ) ) {
5252
builtInMethods.add( new ZonedDateTimeToCalendar( typeFactory ) );
5353
builtInMethods.add( new CalendarToZonedDateTime( typeFactory ) );
54+
builtInMethods.add( new XmlGregorianCalendarToLocalDate( typeFactory ) );
55+
builtInMethods.add( new LocalDateToXmlGregorianCalendar( typeFactory ) );
5456
}
5557
}
5658

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.internal.model.source.builtin;
20+
21+
import static org.mapstruct.ap.internal.util.Collections.asSet;
22+
23+
import java.time.LocalDate;
24+
import java.util.Set;
25+
26+
import javax.xml.datatype.DatatypeConfigurationException;
27+
import javax.xml.datatype.DatatypeConstants;
28+
import javax.xml.datatype.DatatypeFactory;
29+
import javax.xml.datatype.XMLGregorianCalendar;
30+
31+
import org.mapstruct.ap.internal.model.common.Parameter;
32+
import org.mapstruct.ap.internal.model.common.Type;
33+
import org.mapstruct.ap.internal.model.common.TypeFactory;
34+
35+
/**
36+
* @author Gunnar Morling
37+
*/
38+
public class LocalDateToXmlGregorianCalendar extends BuiltInMethod {
39+
40+
private final Parameter parameter;
41+
private final Type returnType;
42+
private final Set<Type> importTypes;
43+
44+
public LocalDateToXmlGregorianCalendar(TypeFactory typeFactory) {
45+
this.parameter = new Parameter( "localDate", typeFactory.getType( LocalDate.class ) );
46+
this.returnType = typeFactory.getType( XMLGregorianCalendar.class );
47+
this.importTypes = asSet(
48+
returnType,
49+
parameter.getType(),
50+
typeFactory.getType( DatatypeFactory.class ),
51+
typeFactory.getType( DatatypeConfigurationException.class ),
52+
typeFactory.getType( DatatypeConstants.class )
53+
);
54+
}
55+
56+
@Override
57+
public Parameter getParameter() {
58+
return parameter;
59+
}
60+
61+
@Override
62+
public Type getReturnType() {
63+
return returnType;
64+
}
65+
66+
@Override
67+
public Set<Type> getImportTypes() {
68+
return importTypes;
69+
}
70+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.internal.model.source.builtin;
20+
21+
import static org.mapstruct.ap.internal.util.Collections.asSet;
22+
23+
import java.time.LocalDate;
24+
import java.util.Set;
25+
26+
import javax.xml.datatype.XMLGregorianCalendar;
27+
28+
import org.mapstruct.ap.internal.model.common.Parameter;
29+
import org.mapstruct.ap.internal.model.common.Type;
30+
import org.mapstruct.ap.internal.model.common.TypeFactory;
31+
32+
/**
33+
* @author Gunnar Morling
34+
*/
35+
public class XmlGregorianCalendarToLocalDate extends BuiltInMethod {
36+
37+
private final Parameter parameter;
38+
private final Type returnType;
39+
private final Set<Type> importTypes;
40+
41+
public XmlGregorianCalendarToLocalDate(TypeFactory typeFactory) {
42+
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
43+
this.returnType = typeFactory.getType( LocalDate.class );
44+
this.importTypes = asSet( returnType, parameter.getType() );
45+
}
46+
47+
@Override
48+
public Parameter getParameter() {
49+
return parameter;
50+
}
51+
52+
@Override
53+
public Type getReturnType() {
54+
return returnType;
55+
}
56+
57+
@Override
58+
public Set<Type> getImportTypes() {
59+
return importTypes;
60+
}
61+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<#--
2+
3+
Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
4+
and/or other contributors as indicated by the @authors tag. See the
5+
copyright.txt file in the distribution for a full listing of all
6+
contributors.
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
20+
-->
21+
private static XMLGregorianCalendar ${name}( java.time.LocalDate localDate ) {
22+
if ( localDate == null ) {
23+
return null;
24+
}
25+
26+
try {
27+
return DatatypeFactory.newInstance().newXMLGregorianCalendarDate(
28+
localDate.getYear(),
29+
localDate.getMonthValue(),
30+
localDate.getDayOfMonth(),
31+
DatatypeConstants.FIELD_UNDEFINED
32+
);
33+
}
34+
catch ( DatatypeConfigurationException ex ) {
35+
throw new RuntimeException( ex );
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<#--
2+
3+
Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
4+
and/or other contributors as indicated by the @authors tag. See the
5+
copyright.txt file in the distribution for a full listing of all
6+
contributors.
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
20+
-->
21+
private static java.time.LocalDate ${name}( XMLGregorianCalendar xcal ) {
22+
if ( xcal == null ) {
23+
return null;
24+
}
25+
26+
return java.time.LocalDate.of( xcal.getYear(), xcal.getMonth(), xcal.getDay() );
27+
}

processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/Java8TimeConversionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
3636

3737
/**
38-
*
38+
* Tests for conversions to/from Java 8 date and time types.
3939
*/
4040
@RunWith(AnnotationProcessorTestRunner.class)
4141
@WithClasses({ Source.class, Target.class, SourceTargetMapper.class })

processor/src/test/java/org/mapstruct/ap/test/bugs/_580/Issue580Test.java renamed to processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/LocalDateToXMLGregorianCalendarConversionTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
package org.mapstruct.ap.test.bugs._580;
19+
package org.mapstruct.ap.test.conversion.java8time;
2020

2121
import static org.assertj.core.api.Assertions.assertThat;
2222

@@ -28,9 +28,9 @@
2828

2929
import org.junit.Test;
3030
import org.junit.runner.RunWith;
31-
import org.mapstruct.ap.test.bugs._580.java8.Source;
32-
import org.mapstruct.ap.test.bugs._580.java8.SourceTargetMapper;
33-
import org.mapstruct.ap.test.bugs._580.java8.Target;
31+
import org.mapstruct.ap.test.conversion.java8time.localdatetoxmlgregoriancalendarconversion.Source;
32+
import org.mapstruct.ap.test.conversion.java8time.localdatetoxmlgregoriancalendarconversion.SourceTargetMapper;
33+
import org.mapstruct.ap.test.conversion.java8time.localdatetoxmlgregoriancalendarconversion.Target;
3434
import org.mapstruct.ap.testutil.IssueKey;
3535
import org.mapstruct.ap.testutil.WithClasses;
3636
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
@@ -41,7 +41,7 @@
4141
@IssueKey("580")
4242
@WithClasses({ Source.class, Target.class, SourceTargetMapper.class })
4343
@RunWith(AnnotationProcessorTestRunner.class)
44-
public class Issue580Test {
44+
public class LocalDateToXMLGregorianCalendarConversionTest {
4545

4646
@Test
4747
public void shouldNullCheckOnBuiltinAndConversion() {

processor/src/test/java/org/mapstruct/ap/test/bugs/_580/java8/Source.java renamed to processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/localdatetoxmlgregoriancalendarconversion/Source.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
package org.mapstruct.ap.test.bugs._580.java8;
19+
package org.mapstruct.ap.test.conversion.java8time.localdatetoxmlgregoriancalendarconversion;
2020

2121
import javax.xml.datatype.XMLGregorianCalendar;
2222

processor/src/test/java/org/mapstruct/ap/test/bugs/_580/java8/SourceTargetMapper.java renamed to processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/localdatetoxmlgregoriancalendarconversion/SourceTargetMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
package org.mapstruct.ap.test.bugs._580.java8;
19+
package org.mapstruct.ap.test.conversion.java8time.localdatetoxmlgregoriancalendarconversion;
2020

2121
import org.mapstruct.Mapper;
2222
import org.mapstruct.factory.Mappers;

processor/src/test/java/org/mapstruct/ap/test/bugs/_580/java8/Target.java renamed to processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/localdatetoxmlgregoriancalendarconversion/Target.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
package org.mapstruct.ap.test.bugs._580.java8;
19+
package org.mapstruct.ap.test.conversion.java8time.localdatetoxmlgregoriancalendarconversion;
2020

2121
import java.time.LocalDate;
2222

0 commit comments

Comments
 (0)