mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#121 support for converting java 8 dates and times to calendar and date
This commit is contained in:
parent
378e23a964
commit
6cd96fcb6f
@ -0,0 +1,106 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.conversion;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
import org.mapstruct.ap.util.Collections;
|
||||
import org.mapstruct.ap.util.Strings;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Base type for mapping Java 8 time types to String and vice versa.
|
||||
* </p>
|
||||
* <p>
|
||||
* In general each type comes with a "parse" method to convert a string to this particular type.
|
||||
* For formatting a dedicated instance of {@link java.time.format.DateTimeFormatter} is used.
|
||||
* </p>
|
||||
* <p>
|
||||
* If no date format for mapping is specified predefined ISO* formatters from
|
||||
* {@link java.time.format.DateTimeFormatter} are used.
|
||||
* </p>
|
||||
* <p>
|
||||
* An overview of date and time types shipped with Java 8 can be found at
|
||||
* http://docs.oracle.com/javase/tutorial/datetime/iso/index.html.
|
||||
* </p>
|
||||
*/
|
||||
public abstract class AbstractJavaTimeToStringConversion extends SimpleConversion {
|
||||
|
||||
@Override
|
||||
protected String getToExpression(ConversionContext conversionContext) {
|
||||
return dateTimeFormatter( conversionContext ) + ".format( <SOURCE> )";
|
||||
}
|
||||
|
||||
private String dateTimeFormatter(ConversionContext conversionContext) {
|
||||
if ( !Strings.isEmpty( conversionContext.getDateFormat() ) ) {
|
||||
return "DateTimeFormatter.ofPattern( \"" + conversionContext.getDateFormat() + "\" )";
|
||||
}
|
||||
else {
|
||||
return "DateTimeFormatter." + defaultFormatterSuffix();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract String defaultFormatterSuffix();
|
||||
|
||||
@Override
|
||||
protected String getFromExpression(ConversionContext conversionContext) {
|
||||
// See http://docs.oracle.com/javase/tutorial/datetime/iso/format.html for how to parse Dates
|
||||
return new StringBuilder().append( conversionContext.getTargetType().getFullyQualifiedName() )
|
||||
.append( ".parse( " )
|
||||
.append( parametersListForParsing( conversionContext ) )
|
||||
.append( " )" ).toString();
|
||||
}
|
||||
|
||||
private String parametersListForParsing(ConversionContext conversionContext) {
|
||||
// See http://docs.oracle.com/javase/tutorial/datetime/iso/format.html for how to format Dates
|
||||
StringBuilder parameterBuilder = new StringBuilder( "<SOURCE>" );
|
||||
if ( !Strings.isEmpty( conversionContext.getDateFormat() ) ) {
|
||||
parameterBuilder.append( ", DateTimeFormatter.ofPattern( \"" )
|
||||
.append( conversionContext.getDateFormat() )
|
||||
.append( "\" )" );
|
||||
}
|
||||
return parameterBuilder.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) {
|
||||
return Collections.asSet(
|
||||
conversionContext.getTypeFactory().getType( dateTimeFormatterClass() )
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
|
||||
return Collections.asSet(
|
||||
conversionContext.getTypeFactory().getType( dateTimeFormatterClass() )
|
||||
);
|
||||
}
|
||||
|
||||
private Class dateTimeFormatterClass() {
|
||||
try {
|
||||
return Class.forName( "java.time.format.DateTimeFormatter" );
|
||||
}
|
||||
catch ( ClassNotFoundException e ) {
|
||||
throw new RuntimeException( "java.time.format.DateTimeFormatter not found on classpath" );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -18,13 +18,13 @@
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import javax.lang.model.util.Elements;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.lang.model.util.Elements;
|
||||
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
import org.mapstruct.ap.model.common.TypeFactory;
|
||||
@ -181,6 +181,8 @@ public class Conversions {
|
||||
|
||||
registerJodaConversions();
|
||||
|
||||
registerJava8TimeConversions();
|
||||
|
||||
//misc.
|
||||
register( Enum.class, String.class, new EnumStringConversion() );
|
||||
register( Date.class, String.class, new DateToStringConversion() );
|
||||
@ -239,10 +241,54 @@ public class Conversions {
|
||||
);
|
||||
}
|
||||
|
||||
private void registerJava8TimeConversions() {
|
||||
if ( !isJava8TimeAvailable() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Java 8 time to String
|
||||
register(
|
||||
getClass( JavaTimeConstants.ZONED_DATE_TIME_FQN ),
|
||||
String.class,
|
||||
new JavaZonedDateTimeToStringConversion()
|
||||
);
|
||||
register(
|
||||
getClass( JavaTimeConstants.LOCAL_DATE_FQN ),
|
||||
String.class,
|
||||
new JavaLocalDateToStringConversion()
|
||||
);
|
||||
register(
|
||||
getClass( JavaTimeConstants.LOCAL_DATE_TIME_FQN ),
|
||||
String.class,
|
||||
new JavaLocalDateTimeToStringConversion()
|
||||
);
|
||||
register(
|
||||
getClass( JavaTimeConstants.LOCAL_TIME_FQN ),
|
||||
String.class,
|
||||
new JavaLocalTimeToStringConversion()
|
||||
);
|
||||
|
||||
// Java 8 to Date
|
||||
register(
|
||||
getClass( JavaTimeConstants.ZONED_DATE_TIME_FQN ),
|
||||
Date.class,
|
||||
new JavaZonedDateTimeToDateConversion()
|
||||
);
|
||||
|
||||
register(
|
||||
getClass( JavaTimeConstants.LOCAL_DATE_TIME_FQN ),
|
||||
Date.class,
|
||||
new JavaLocalDateTimeToDateConversion()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
private static boolean isJodaTimeAvailable() {
|
||||
return NativeTypes.isTypeAvailable( JodaTimeConstants.DATE_TIME_FQN );
|
||||
}
|
||||
|
||||
private static boolean isJava8TimeAvailable() {
|
||||
return NativeTypes.isTypeAvailable( JavaTimeConstants.ZONED_DATE_TIME_FQN );
|
||||
}
|
||||
private void registerNativeTypeConversion(Class<?> sourceType, Class<?> targetType) {
|
||||
if ( sourceType.isPrimitive() && targetType.isPrimitive() ) {
|
||||
register( sourceType, targetType, new PrimitiveToPrimitiveConversion( sourceType ) );
|
||||
|
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.conversion;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
|
||||
/**
|
||||
* SimpleConversion for mapping {@link java.time.LocalDateTime} to
|
||||
* {@link java.util.Date} and vice versa.
|
||||
*/
|
||||
public class JavaLocalDateTimeToDateConversion extends SimpleConversion {
|
||||
|
||||
@Override
|
||||
protected String getToExpression(ConversionContext conversionContext) {
|
||||
return "java.util.Date.from( <SOURCE>.toInstant( java.time.ZoneOffset.UTC ) )";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFromExpression(ConversionContext conversionContext) {
|
||||
return "java.time.LocalDateTime.ofInstant( <SOURCE>.toInstant(), java.time.ZoneId.systemDefault() )";
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.conversion;
|
||||
|
||||
/**
|
||||
* Specialization of {@link AbstractJavaTimeToStringConversion} for converting {@link java.time.LocalDateTime}
|
||||
*/
|
||||
public class JavaLocalDateTimeToStringConversion extends AbstractJavaTimeToStringConversion {
|
||||
@Override
|
||||
protected String defaultFormatterSuffix() {
|
||||
return "ISO_LOCAL_DATE_TIME";
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.conversion;
|
||||
|
||||
/**
|
||||
* Specialization of {@link AbstractJavaTimeToStringConversion} for converting {@link java.time.LocalDate}
|
||||
*/
|
||||
public class JavaLocalDateToStringConversion extends AbstractJavaTimeToStringConversion {
|
||||
|
||||
@Override
|
||||
protected String defaultFormatterSuffix() {
|
||||
return "ISO_LOCAL_DATE";
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.conversion;
|
||||
|
||||
/**
|
||||
* Specialization of {@link AbstractJavaTimeToStringConversion} for converting {@link java.time.LocalTime}
|
||||
*/
|
||||
public class JavaLocalTimeToStringConversion extends AbstractJavaTimeToStringConversion {
|
||||
|
||||
@Override
|
||||
protected String defaultFormatterSuffix() {
|
||||
return "ISO_LOCAL_TIME";
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.conversion;
|
||||
|
||||
/**
|
||||
* Helper holding Java time full qualified class names for conversion registration
|
||||
*/
|
||||
public final class JavaTimeConstants {
|
||||
|
||||
public static final String ZONED_DATE_TIME_FQN = "java.time.ZonedDateTime";
|
||||
public static final String LOCAL_DATE_TIME_FQN = "java.time.LocalDateTime";
|
||||
public static final String LOCAL_DATE_FQN = "java.time.LocalDate";
|
||||
public static final String LOCAL_TIME_FQN = "java.time.LocalTime";
|
||||
public static final String ZONE_ID = "java.time.ZoneId";
|
||||
public static final String DATE_TIME_FORMATTER = "java.time.format.DateTimeFormatter";
|
||||
|
||||
|
||||
|
||||
private JavaTimeConstants() {
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.conversion;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
|
||||
/**
|
||||
* SimpleConversion for mapping {@link java.time.ZonedDateTime} to
|
||||
* {@link java.util.Date} and vice versa.
|
||||
*/
|
||||
|
||||
public class JavaZonedDateTimeToDateConversion extends SimpleConversion {
|
||||
|
||||
@Override
|
||||
protected String getToExpression(ConversionContext conversionContext) {
|
||||
return "java.util.Date.from( <SOURCE>.toInstant() )";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFromExpression(ConversionContext conversionContext) {
|
||||
return "java.time.ZonedDateTime.ofInstant( <SOURCE>.toInstant(), java.time.ZoneId.systemDefault() )";
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.conversion;
|
||||
|
||||
/**
|
||||
* Specialization of {@link AbstractJavaTimeToStringConversion} for converting {@link java.time.ZonedDateTime}
|
||||
*/
|
||||
public class JavaZonedDateTimeToStringConversion extends AbstractJavaTimeToStringConversion {
|
||||
|
||||
@Override
|
||||
protected String defaultFormatterSuffix() {
|
||||
return "ISO_DATE_TIME";
|
||||
}
|
||||
}
|
@ -41,7 +41,10 @@ public class BuiltInMappingMethods {
|
||||
new StringToXmlGregorianCalendar( typeFactory ),
|
||||
new XmlGregorianCalendarToString( typeFactory ),
|
||||
new CalendarToXmlGregorianCalendar( typeFactory ),
|
||||
new XmlGregorianCalendarToCalendar( typeFactory )
|
||||
new XmlGregorianCalendarToCalendar( typeFactory ),
|
||||
new ZonedDateTimeToCalendar( typeFactory ),
|
||||
new CalendarToZonedDateTime( typeFactory )
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.model.source.builtin;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Set;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.mapstruct.ap.model.common.Parameter;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
import org.mapstruct.ap.model.common.TypeFactory;
|
||||
import org.mapstruct.ap.util.Collections;
|
||||
import org.mapstruct.ap.util.JavaTimeConstants;
|
||||
|
||||
/**
|
||||
* {@link BuiltInMethod} for mapping between {@link java.util.Calendar}
|
||||
* and {@link java.time.ZonedDateTime}.
|
||||
* <br />
|
||||
* Template is at org.mapstruct.ap.model.builtin.CalendarToZonedDateTime.ftl
|
||||
*/
|
||||
public class CalendarToZonedDateTime extends BuiltInMethod {
|
||||
|
||||
private final Type returnType;
|
||||
private final Parameter parameter;
|
||||
private final Set<Type> importedTypes;
|
||||
|
||||
CalendarToZonedDateTime(TypeFactory typeFactory) {
|
||||
this.returnType = typeFactory.getType( JavaTimeConstants.ZONED_DATE_TIME_FQN );
|
||||
this.parameter = new Parameter( "cal", typeFactory.getType( Calendar.class ) );
|
||||
this.importedTypes = Collections.asSet(
|
||||
typeFactory.getType( Calendar.class ),
|
||||
typeFactory.getType( TimeZone.class ),
|
||||
typeFactory.getType( JavaTimeConstants.ZONED_DATE_TIME_FQN )
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parameter getParameter() {
|
||||
return parameter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
return importedTypes;
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.model.source.builtin;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.common.Parameter;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
import org.mapstruct.ap.model.common.TypeFactory;
|
||||
import org.mapstruct.ap.util.Collections;
|
||||
import org.mapstruct.ap.util.JavaTimeConstants;
|
||||
|
||||
/**
|
||||
* {@link BuiltInMethod} for mapping between {@link java.util.Calendar}
|
||||
* and {@link java.time.ZonedDateTime}.
|
||||
* <br />
|
||||
* Template is at org.mapstruct.ap.model.builtin.ZonedDateTimeToCalendar.ftl
|
||||
*/
|
||||
public class ZonedDateTimeToCalendar extends BuiltInMethod {
|
||||
private final Type returnType;
|
||||
private final Parameter parameter;
|
||||
private final Set<Type> importedTypes;
|
||||
|
||||
ZonedDateTimeToCalendar(TypeFactory typeFactory) {
|
||||
this.returnType = typeFactory.getType( Calendar.class );
|
||||
this.parameter = new Parameter( "dateTime", typeFactory.getType( JavaTimeConstants.ZONED_DATE_TIME_FQN ) );
|
||||
this.importedTypes = Collections.asSet(
|
||||
typeFactory.getType( Calendar.class ),
|
||||
typeFactory.getType( JavaTimeConstants.ZONED_DATE_TIME_FQN )
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parameter getParameter() {
|
||||
return parameter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
return importedTypes;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.util;
|
||||
|
||||
/**
|
||||
* Helper holding Java time full qualified class names for conversion registration
|
||||
*/
|
||||
public final class JavaTimeConstants {
|
||||
|
||||
public static final String ZONED_DATE_TIME_FQN = "java.time.ZonedDateTime";
|
||||
public static final String LOCAL_DATE_TIME_FQN = "java.time.LocalDateTime";
|
||||
public static final String LOCAL_DATE_FQN = "java.time.LocalDate";
|
||||
public static final String LOCAL_TIME_FQN = "java.time.LocalTime";
|
||||
|
||||
private JavaTimeConstants() {
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
<#--
|
||||
|
||||
Copyright 2012-2014 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 ZonedDateTime ${name}( Calendar cal ) {
|
||||
if ( cal == null) {
|
||||
return null;
|
||||
}
|
||||
return ZonedDateTime.ofInstant( cal.toInstant(), cal.getTimeZone().toZoneId());
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<#--
|
||||
|
||||
Copyright 2012-2014 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 Calendar ${name}( ZonedDateTime dateTime ) {
|
||||
if (dateTime == null) {
|
||||
return null;
|
||||
}
|
||||
Calendar instance = Calendar.getInstance( TimeZone.getTimeZone( dateTime.getZone() ) );
|
||||
instance.setTimeInMillis( dateTime.toInstant().toEpochMilli() );
|
||||
return instance;
|
||||
}
|
@ -0,0 +1,272 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.conversion.java8time;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@RunWith(AnnotationProcessorTestRunner.class)
|
||||
@WithClasses({ Source.class, Target.class, SourceTargetMapper.class })
|
||||
@IssueKey("121")
|
||||
public class Java8TimeConversionTest {
|
||||
|
||||
@Test
|
||||
public void testDateTimeToString() {
|
||||
Source src = new Source();
|
||||
src.setZonedDateTime( ZonedDateTime.of( java.time.LocalDateTime.of( 2014, 1, 1, 0, 0 ), ZoneId.of( "UTC" ) ) );
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTargetDateTimeMapped( src );
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getZonedDateTime() ).isEqualTo( "01.01.2014 00:00 UTC" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalDateTimeToString() {
|
||||
Source src = new Source();
|
||||
src.setLocalDateTime( LocalDateTime.of( 2014, 1, 1, 0, 0 ) );
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTargetLocalDateTimeMapped( src );
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getLocalDateTime() ).isEqualTo( "01.01.2014 00:00" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalDateToString() {
|
||||
Source src = new Source();
|
||||
src.setLocalDate( LocalDate.of( 2014, 1, 1 ) );
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTargetLocalDateMapped( src );
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getLocalDate() ).isEqualTo( "01.01.2014" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalTimeToString() {
|
||||
Source src = new Source();
|
||||
src.setLocalTime( LocalTime.of( 0, 0 ) );
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTargetLocalTimeMapped( src );
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getLocalTime() ).isEqualTo( "00:00" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSourceToTargetMappingForStrings() {
|
||||
Source src = new Source();
|
||||
src.setLocalTime( LocalTime.of( 0, 0 ) );
|
||||
src.setLocalDate( LocalDate.of( 2014, 1, 1 ) );
|
||||
src.setLocalDateTime( LocalDateTime.of( 2014, 1, 1, 0, 0 ) );
|
||||
src.setZonedDateTime( ZonedDateTime.of( java.time.LocalDateTime.of( 2014, 1, 1, 0, 0 ), ZoneId.of( "UTC" ) ) );
|
||||
|
||||
// with given format
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( src );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getZonedDateTime() ).isEqualTo( "01.01.2014 00:00 UTC" );
|
||||
assertThat( target.getLocalDateTime() ).isEqualTo( "01.01.2014 00:00" );
|
||||
assertThat( target.getLocalDate() ).isEqualTo( "01.01.2014" );
|
||||
assertThat( target.getLocalTime() ).isEqualTo( "00:00" );
|
||||
|
||||
// and now with default mappings
|
||||
target = SourceTargetMapper.INSTANCE.sourceToTargetDefaultMapping( src );
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getZonedDateTime() ).isEqualTo( "01.01.2014 00:00 UTC" );
|
||||
assertThat( target.getLocalDateTime() ).isEqualTo( "01.01.2014 00:00" );
|
||||
assertThat( target.getLocalDate() ).isEqualTo( "01.01.2014" );
|
||||
assertThat( target.getLocalTime() ).isEqualTo( "00:00" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToDateTime() {
|
||||
String dateTimeAsString = "01.01.2014 00:00 UTC";
|
||||
Target target = new Target();
|
||||
target.setZonedDateTime( dateTimeAsString );
|
||||
ZonedDateTime sourceDateTime =
|
||||
ZonedDateTime.of( java.time.LocalDateTime.of( 2014, 1, 1, 0, 0 ), ZoneId.of( "UTC" ) );
|
||||
|
||||
Source src = SourceTargetMapper.INSTANCE.targetToSourceDateTimeMapped( target );
|
||||
assertThat( src ).isNotNull();
|
||||
assertThat( src.getZonedDateTime() ).isEqualTo( sourceDateTime );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToLocalDateTime() {
|
||||
String dateTimeAsString = "01.01.2014 00:00";
|
||||
Target target = new Target();
|
||||
target.setLocalDateTime( dateTimeAsString );
|
||||
LocalDateTime sourceDateTime =
|
||||
LocalDateTime.of( 2014, 1, 1, 0, 0, 0 );
|
||||
|
||||
Source src = SourceTargetMapper.INSTANCE.targetToSourceLocalDateTimeMapped( target );
|
||||
assertThat( src ).isNotNull();
|
||||
assertThat( src.getLocalDateTime() ).isEqualTo( sourceDateTime );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToLocalDate() {
|
||||
String dateTimeAsString = "01.01.2014";
|
||||
Target target = new Target();
|
||||
target.setLocalDate( dateTimeAsString );
|
||||
LocalDate sourceDate =
|
||||
LocalDate.of( 2014, 1, 1 );
|
||||
|
||||
Source src = SourceTargetMapper.INSTANCE.targetToSourceLocalDateMapped( target );
|
||||
assertThat( src ).isNotNull();
|
||||
assertThat( src.getLocalDate() ).isEqualTo( sourceDate );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToLocalTime() {
|
||||
String dateTimeAsString = "00:00";
|
||||
Target target = new Target();
|
||||
target.setLocalTime( dateTimeAsString );
|
||||
LocalTime sourceTime =
|
||||
LocalTime.of( 0, 0 );
|
||||
|
||||
Source src = SourceTargetMapper.INSTANCE.targetToSourceLocalTimeMapped( target );
|
||||
assertThat( src ).isNotNull();
|
||||
assertThat( src.getLocalTime() ).isEqualTo( sourceTime );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTargetToSourceNullMapping() {
|
||||
Target target = new Target();
|
||||
Source src = SourceTargetMapper.INSTANCE.targetToSource( target );
|
||||
|
||||
assertThat( src ).isNotNull();
|
||||
assertThat( src.getZonedDateTime() ).isNull();
|
||||
assertThat( src.getLocalDate() ).isNull();
|
||||
assertThat( src.getLocalDateTime() ).isNull();
|
||||
assertThat( src.getLocalTime() ).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTargetToSourceMappingForStrings() {
|
||||
Target target = new Target();
|
||||
|
||||
target.setZonedDateTime( "01.01.2014 00:00 UTC" );
|
||||
target.setLocalDateTime( "01.01.2014 00:00" );
|
||||
target.setLocalDate( "01.01.2014" );
|
||||
target.setLocalTime( "00:00" );
|
||||
|
||||
Source src = SourceTargetMapper.INSTANCE.targetToSource( target );
|
||||
|
||||
assertThat( src.getZonedDateTime() ).isEqualTo(
|
||||
ZonedDateTime.of(
|
||||
java.time.LocalDateTime.of(
|
||||
2014,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
0 ), ZoneId.of( "UTC" ) ) );
|
||||
assertThat( src.getLocalDateTime() ).isEqualTo( LocalDateTime.of( 2014, 1, 1, 0, 0 ) );
|
||||
assertThat( src.getLocalDate() ).isEqualTo( LocalDate.of( 2014, 1, 1 ) );
|
||||
assertThat( src.getLocalTime() ).isEqualTo( LocalTime.of( 0, 0 ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCalendarMapping() {
|
||||
Source source = new Source();
|
||||
ZonedDateTime dateTime = ZonedDateTime.of( LocalDateTime.of( 2014, 1, 1, 0, 0 ), ZoneId.of( "UTC" ) );
|
||||
source.setForCalendarConversion(
|
||||
dateTime );
|
||||
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||
|
||||
assertThat( target.getForCalendarConversion() ).isNotNull();
|
||||
assertThat( target.getForCalendarConversion().getTimeZone() ).isEqualTo(
|
||||
TimeZone.getTimeZone(
|
||||
"UTC" ) );
|
||||
assertThat( target.getForCalendarConversion().get( Calendar.YEAR ) ).isEqualTo( dateTime.getYear() );
|
||||
assertThat( target.getForCalendarConversion().get( Calendar.MONTH ) ).isEqualTo(
|
||||
dateTime.getMonthValue() - 1 );
|
||||
assertThat( target.getForCalendarConversion().get( Calendar.DATE ) ).isEqualTo( dateTime.getDayOfMonth() );
|
||||
assertThat( target.getForCalendarConversion().get( Calendar.MINUTE ) ).isEqualTo( dateTime.getMinute() );
|
||||
assertThat( target.getForCalendarConversion().get( Calendar.HOUR ) ).isEqualTo( dateTime.getHour() );
|
||||
|
||||
source = SourceTargetMapper.INSTANCE.targetToSource( target );
|
||||
|
||||
assertThat( source.getForCalendarConversion() ).isEqualTo( dateTime );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZonedDateTimeToDateMapping() {
|
||||
TimeZone.setDefault( TimeZone.getTimeZone( "UTC" ) );
|
||||
Source source = new Source();
|
||||
ZonedDateTime dateTime = ZonedDateTime.of( LocalDateTime.of( 2014, 1, 1, 0, 0 ), ZoneId.of( "UTC" ) );
|
||||
source.setForDateConversionWithZonedDateTime(
|
||||
dateTime );
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTargetDefaultMapping( source );
|
||||
|
||||
assertThat( target.getForDateConversionWithZonedDateTime() ).isNotNull();
|
||||
|
||||
Calendar instance = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) );
|
||||
instance.setTimeInMillis( target.getForDateConversionWithZonedDateTime().getTime() );
|
||||
|
||||
assertThat( instance.get( Calendar.YEAR ) ).isEqualTo( dateTime.getYear() );
|
||||
assertThat( instance.get( Calendar.MONTH ) ).isEqualTo( dateTime.getMonthValue() - 1 );
|
||||
assertThat( instance.get( Calendar.DATE ) ).isEqualTo( dateTime.getDayOfMonth() );
|
||||
assertThat( instance.get( Calendar.MINUTE ) ).isEqualTo( dateTime.getMinute() );
|
||||
assertThat( instance.get( Calendar.HOUR ) ).isEqualTo( dateTime.getHour() );
|
||||
|
||||
source = SourceTargetMapper.INSTANCE.targetToSource( target );
|
||||
|
||||
assertThat( source.getForDateConversionWithZonedDateTime() ).isEqualTo( dateTime );
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalDateTimeToDateMapping() {
|
||||
TimeZone.setDefault( TimeZone.getTimeZone( "UTC" ) );
|
||||
Source source = new Source();
|
||||
LocalDateTime dateTime = LocalDateTime.of( 2014, 1, 1, 0, 0 );
|
||||
source.setForDateConversionWithLocalDateTime( dateTime );
|
||||
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTargetDefaultMapping( source );
|
||||
|
||||
assertThat( target.getForDateConversionWithLocalDateTime() ).isNotNull();
|
||||
|
||||
Calendar instance = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) );
|
||||
instance.setTimeInMillis( target.getForDateConversionWithLocalDateTime().getTime() );
|
||||
|
||||
assertThat( instance.get( Calendar.YEAR ) ).isEqualTo( dateTime.getYear() );
|
||||
assertThat( instance.get( Calendar.MONTH ) ).isEqualTo( dateTime.getMonthValue() - 1 );
|
||||
assertThat( instance.get( Calendar.DATE ) ).isEqualTo( dateTime.getDayOfMonth() );
|
||||
assertThat( instance.get( Calendar.MINUTE ) ).isEqualTo( dateTime.getMinute() );
|
||||
assertThat( instance.get( Calendar.HOUR ) ).isEqualTo( dateTime.getHour() );
|
||||
|
||||
source = SourceTargetMapper.INSTANCE.targetToSource( target );
|
||||
|
||||
assertThat( source.getForDateConversionWithLocalDateTime() ).isEqualTo( dateTime );
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.conversion.java8time;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class Source {
|
||||
|
||||
private ZonedDateTime zonedDateTime;
|
||||
|
||||
private LocalDateTime localDateTime;
|
||||
|
||||
private LocalDate localDate;
|
||||
|
||||
private LocalTime localTime;
|
||||
|
||||
private ZonedDateTime forCalendarConversion;
|
||||
|
||||
private ZonedDateTime forDateConversionWithZonedDateTime;
|
||||
|
||||
private LocalDateTime forDateConversionWithLocalDateTime;
|
||||
|
||||
public ZonedDateTime getZonedDateTime() {
|
||||
return zonedDateTime;
|
||||
}
|
||||
|
||||
public void setZonedDateTime(ZonedDateTime dateTime) {
|
||||
this.zonedDateTime = dateTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getLocalDateTime() {
|
||||
return localDateTime;
|
||||
}
|
||||
|
||||
public void setLocalDateTime(LocalDateTime localDateTime) {
|
||||
this.localDateTime = localDateTime;
|
||||
}
|
||||
|
||||
public LocalDate getLocalDate() {
|
||||
return localDate;
|
||||
}
|
||||
|
||||
public void setLocalDate(LocalDate localDate) {
|
||||
this.localDate = localDate;
|
||||
}
|
||||
|
||||
public LocalTime getLocalTime() {
|
||||
return localTime;
|
||||
}
|
||||
|
||||
public void setLocalTime(LocalTime localTime) {
|
||||
this.localTime = localTime;
|
||||
}
|
||||
|
||||
public ZonedDateTime getForCalendarConversion() {
|
||||
return forCalendarConversion;
|
||||
}
|
||||
|
||||
public void setForCalendarConversion(ZonedDateTime forCalendarConversion) {
|
||||
this.forCalendarConversion = forCalendarConversion;
|
||||
}
|
||||
|
||||
public ZonedDateTime getForDateConversionWithZonedDateTime() {
|
||||
return forDateConversionWithZonedDateTime;
|
||||
}
|
||||
|
||||
public void setForDateConversionWithZonedDateTime(ZonedDateTime forDateConversionWithZonedDateTime) {
|
||||
this.forDateConversionWithZonedDateTime = forDateConversionWithZonedDateTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getForDateConversionWithLocalDateTime() {
|
||||
return forDateConversionWithLocalDateTime;
|
||||
}
|
||||
|
||||
public void setForDateConversionWithLocalDateTime(LocalDateTime forDateConversionWithLocalDateTime) {
|
||||
this.forDateConversionWithLocalDateTime = forDateConversionWithLocalDateTime;
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.conversion.java8time;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Mapper
|
||||
public interface SourceTargetMapper {
|
||||
|
||||
String DATE_TIME_FORMAT = "dd.MM.yyyy HH:mm z";
|
||||
|
||||
String LOCAL_DATE_TIME_FORMAT = "dd.MM.yyyy HH:mm";
|
||||
|
||||
String LOCAL_DATE_FORMAT = "dd.MM.yyyy";
|
||||
|
||||
String LOCAL_TIME_FORMAT = "HH:mm";
|
||||
|
||||
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
||||
@Mappings( { @Mapping( source = "zonedDateTime", dateFormat = DATE_TIME_FORMAT ),
|
||||
@Mapping( source = "localDateTime", dateFormat = LOCAL_DATE_TIME_FORMAT ),
|
||||
@Mapping( source = "localDate", dateFormat = LOCAL_DATE_FORMAT ),
|
||||
@Mapping( source = "localTime", dateFormat = LOCAL_TIME_FORMAT ) } )
|
||||
Target sourceToTarget(Source source);
|
||||
|
||||
Target sourceToTargetDefaultMapping(Source source);
|
||||
|
||||
@Mapping( source = "zonedDateTime", dateFormat = DATE_TIME_FORMAT )
|
||||
Target sourceToTargetDateTimeMapped(Source source);
|
||||
|
||||
@Mapping( source = "localDateTime", dateFormat = LOCAL_DATE_TIME_FORMAT )
|
||||
Target sourceToTargetLocalDateTimeMapped(Source source);
|
||||
|
||||
@Mapping( source = "localDate", dateFormat = LOCAL_DATE_FORMAT )
|
||||
Target sourceToTargetLocalDateMapped(Source source);
|
||||
|
||||
@Mapping( source = "localTime", dateFormat = LOCAL_TIME_FORMAT )
|
||||
Target sourceToTargetLocalTimeMapped(Source source);
|
||||
|
||||
@Mappings( { @Mapping( source = "zonedDateTime", dateFormat = DATE_TIME_FORMAT ),
|
||||
@Mapping( source = "localDateTime", dateFormat = LOCAL_DATE_TIME_FORMAT ),
|
||||
@Mapping( source = "localDate", dateFormat = LOCAL_DATE_FORMAT ),
|
||||
@Mapping( source = "localTime", dateFormat = LOCAL_TIME_FORMAT ) } )
|
||||
Source targetToSource(Target target);
|
||||
|
||||
@Mapping( source = "zonedDateTime", dateFormat = DATE_TIME_FORMAT )
|
||||
Source targetToSourceDateTimeMapped(Target target);
|
||||
|
||||
@Mapping( source = "localDateTime", dateFormat = LOCAL_DATE_TIME_FORMAT )
|
||||
Source targetToSourceLocalDateTimeMapped(Target target);
|
||||
|
||||
@Mapping( source = "localDate", dateFormat = LOCAL_DATE_FORMAT )
|
||||
Source targetToSourceLocalDateMapped(Target target);
|
||||
|
||||
@Mapping( source = "localTime", dateFormat = LOCAL_TIME_FORMAT )
|
||||
Source targetToSourceLocalTimeMapped(Target target);
|
||||
|
||||
Source targetToSourceDefaultMapping(Target target);
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
/**
|
||||
* Copyright 2012-2014 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.conversion.java8time;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class Target {
|
||||
|
||||
private String zonedDateTime;
|
||||
|
||||
private String localDateTime;
|
||||
|
||||
private String localDate;
|
||||
|
||||
private String localTime;
|
||||
|
||||
private Calendar forCalendarConversion;
|
||||
|
||||
private Date forDateConversionWithZonedDateTime;
|
||||
|
||||
private Date forDateConversionWithLocalDateTime;
|
||||
|
||||
public String getZonedDateTime() {
|
||||
return zonedDateTime;
|
||||
}
|
||||
|
||||
public void setZonedDateTime(String zonedDateTime) {
|
||||
this.zonedDateTime = zonedDateTime;
|
||||
}
|
||||
|
||||
public String getLocalDateTime() {
|
||||
return localDateTime;
|
||||
}
|
||||
|
||||
public void setLocalDateTime(String localDateTime) {
|
||||
this.localDateTime = localDateTime;
|
||||
}
|
||||
|
||||
public String getLocalDate() {
|
||||
return localDate;
|
||||
}
|
||||
|
||||
public void setLocalDate(String localDate) {
|
||||
this.localDate = localDate;
|
||||
}
|
||||
|
||||
public String getLocalTime() {
|
||||
return localTime;
|
||||
}
|
||||
|
||||
public void setLocalTime(String localTime) {
|
||||
this.localTime = localTime;
|
||||
}
|
||||
|
||||
public Calendar getForCalendarConversion() {
|
||||
return forCalendarConversion;
|
||||
}
|
||||
|
||||
public void setForCalendarConversion(Calendar forCalendarConversion) {
|
||||
this.forCalendarConversion = forCalendarConversion;
|
||||
}
|
||||
|
||||
public Date getForDateConversionWithZonedDateTime() {
|
||||
return forDateConversionWithZonedDateTime;
|
||||
}
|
||||
|
||||
public void setForDateConversionWithZonedDateTime(Date forDateConversionWithZonedDateTime) {
|
||||
this.forDateConversionWithZonedDateTime = forDateConversionWithZonedDateTime;
|
||||
}
|
||||
|
||||
public Date getForDateConversionWithLocalDateTime() {
|
||||
return forDateConversionWithLocalDateTime;
|
||||
}
|
||||
|
||||
public void setForDateConversionWithLocalDateTime(Date forDateConversionWithLocalDateTime) {
|
||||
this.forDateConversionWithLocalDateTime = forDateConversionWithLocalDateTime;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user