This commit is contained in:
Gunnar Morling 2014-05-08 00:44:32 +02:00
parent ac01dda742
commit d6a6cb25b6
13 changed files with 60 additions and 42 deletions

View File

@ -28,7 +28,9 @@ import org.mapstruct.ap.util.Strings;
import static org.mapstruct.ap.util.Collections.asSet; import static org.mapstruct.ap.util.Collections.asSet;
/** /**
* Base class for conversions between Joda-Time types and String.
* *
* @author Timo Eckhardt
*/ */
public abstract class AbstractJodaTypeToStringConversion extends SimpleConversion { public abstract class AbstractJodaTypeToStringConversion extends SimpleConversion {
@ -83,11 +85,17 @@ public abstract class AbstractJodaTypeToStringConversion extends SimpleConversio
return conversionString.toString(); return conversionString.toString();
} }
public String defaultDateFormatPattern() { private String defaultDateFormatPattern() {
return "DateTimeFormat.patternForStyle( \"" + formatStyle() + "\", Locale.getDefault() )"; return "DateTimeFormat.patternForStyle( \"" + formatStyle() + "\", Locale.getDefault() )";
} }
/**
* Returns the default format style to be applied if non is given explicitly.
*/
protected abstract String formatStyle(); protected abstract String formatStyle();
/**
* Returns the name of the parse method for converting a String into a specific Joda-Time type.
*/
protected abstract String parseMethod(); protected abstract String parseMethod();
} }

View File

@ -179,7 +179,7 @@ public class Conversions {
register( BigInteger.class, String.class, new BigIntegerToStringConversion() ); register( BigInteger.class, String.class, new BigIntegerToStringConversion() );
register( BigDecimal.class, String.class, new BigDecimalToStringConversion() ); register( BigDecimal.class, String.class, new BigDecimalToStringConversion() );
registerJoda(); registerJodaConversions();
//misc. //misc.
register( Enum.class, String.class, new EnumStringConversion() ); register( Enum.class, String.class, new EnumStringConversion() );
@ -187,7 +187,7 @@ public class Conversions {
register( BigDecimal.class, BigInteger.class, new BigDecimalToBigIntegerConversion() ); register( BigDecimal.class, BigInteger.class, new BigDecimalToBigIntegerConversion() );
} }
private void registerJoda() { private void registerJodaConversions() {
if ( !isJodaTimeAvailable() ) { if ( !isJodaTimeAvailable() ) {
return; return;
} }
@ -235,7 +235,7 @@ public class Conversions {
register( register(
getClass( JodaTimeConstants.DATE_TIME_FQN ), getClass( JodaTimeConstants.DATE_TIME_FQN ),
Calendar.class, Calendar.class,
new JodaTimeToCalendarConversion() new JodaDateTimeToCalendarConversion()
); );
} }

View File

@ -18,6 +18,7 @@
*/ */
package org.mapstruct.ap.conversion; package org.mapstruct.ap.conversion;
import java.util.Calendar;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
@ -27,9 +28,11 @@ import org.mapstruct.ap.model.common.Type;
import static org.mapstruct.ap.util.Collections.asSet; import static org.mapstruct.ap.util.Collections.asSet;
/** /**
* Conversion between {@code DateTime} and {@link Calendar}.
* *
* @author Timo Eckhardt
*/ */
public class JodaTimeToCalendarConversion extends SimpleConversion { public class JodaDateTimeToCalendarConversion extends SimpleConversion {
@Override @Override
protected String getToExpression(ConversionContext conversionContext) { protected String getToExpression(ConversionContext conversionContext) {

View File

@ -19,7 +19,9 @@
package org.mapstruct.ap.conversion; package org.mapstruct.ap.conversion;
/** /**
* Conversion between {@code DateTime} and {@code String}.
* *
* @author Timo Eckhardt
*/ */
public class JodaDateTimeToStringConversion extends AbstractJodaTypeToStringConversion { public class JodaDateTimeToStringConversion extends AbstractJodaTypeToStringConversion {

View File

@ -19,7 +19,9 @@
package org.mapstruct.ap.conversion; package org.mapstruct.ap.conversion;
/** /**
* Conversion between {@code LocalDateTime} and {@code String}.
* *
* @author Timo Eckhardt
*/ */
public class JodaLocalDateTimeToStringConversion extends AbstractJodaTypeToStringConversion { public class JodaLocalDateTimeToStringConversion extends AbstractJodaTypeToStringConversion {

View File

@ -19,7 +19,9 @@
package org.mapstruct.ap.conversion; package org.mapstruct.ap.conversion;
/** /**
* Conversion between {@code LocalDate} and {@code String}.
* *
* @author Timo Eckhardt
*/ */
public class JodaLocalDateToStringConversion extends AbstractJodaTypeToStringConversion { public class JodaLocalDateToStringConversion extends AbstractJodaTypeToStringConversion {

View File

@ -19,7 +19,9 @@
package org.mapstruct.ap.conversion; package org.mapstruct.ap.conversion;
/** /**
* Conversion between {@code LocalTime} and {@code String}.
* *
* @author Timo Eckhardt
*/ */
public class JodaLocalTimeToStringConversion extends AbstractJodaTypeToStringConversion { public class JodaLocalTimeToStringConversion extends AbstractJodaTypeToStringConversion {

View File

@ -19,7 +19,9 @@
package org.mapstruct.ap.conversion; package org.mapstruct.ap.conversion;
/** /**
* Helper holding constants for working with joda time * Helper holding constants for working with Joda-Time.
*
* @author Timo Eckhardt
*/ */
public final class JodaTimeConstants { public final class JodaTimeConstants {

View File

@ -18,6 +18,7 @@
*/ */
package org.mapstruct.ap.conversion; package org.mapstruct.ap.conversion;
import java.util.Date;
import java.util.Set; import java.util.Set;
import org.mapstruct.ap.model.common.ConversionContext; import org.mapstruct.ap.model.common.ConversionContext;
@ -26,14 +27,14 @@ import org.mapstruct.ap.model.common.Type;
import static org.mapstruct.ap.util.Collections.asSet; import static org.mapstruct.ap.util.Collections.asSet;
/** /**
* Implementation of {@link org.mapstruct.ap.conversion.ConversionProvider} mapping Joda Types * Conversion between the following Joda types and {@link Date}:
* <ul> * <ul>
* <li>org.joda.time.DateTime</li> * <li>org.joda.time.DateTime</li>
* <li>org.joda.time.LocalDateTime</li> * <li>org.joda.time.LocalDateTime</li>
* <li>org.joda.time.LocalDate</li> * <li>org.joda.time.LocalDate</li>
* </ul> * </ul>
* to java.util.Date by invoking org.joda.time.base.AbstractInstant#toDate(). *
* Backward conversion is done. * @author Timo Eckhardt
*/ */
public class JodaTimeToDateConversion extends SimpleConversion { public class JodaTimeToDateConversion extends SimpleConversion {

View File

@ -18,6 +18,9 @@
*/ */
package org.mapstruct.ap.test.conversion.jodatime; package org.mapstruct.ap.test.conversion.jodatime;
import java.util.Calendar;
import java.util.TimeZone;
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.LocalDate;
@ -29,13 +32,12 @@ import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import java.util.Calendar;
import java.util.TimeZone;
import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Assertions.assertThat;
/** /**
* Tests the conversion between Joda-Time types and String/Date/Calendar.
* *
* @author Timo Eckhardt
*/ */
@RunWith(AnnotationProcessorTestRunner.class) @RunWith(AnnotationProcessorTestRunner.class)
@WithClasses({ Source.class, Target.class, SourceTargetMapper.class }) @WithClasses({ Source.class, Target.class, SourceTargetMapper.class })
@ -199,6 +201,5 @@ public class JodaConversionTest {
Source mappedSource = SourceTargetMapper.INSTANCE.targetToSource( target ); Source mappedSource = SourceTargetMapper.INSTANCE.targetToSource( target );
assertThat( mappedSource ).isNotNull(); assertThat( mappedSource ).isNotNull();
assertThat( mappedSource.getDateTimeForCalendarConversion() ).isEqualTo( dateTimeWithCalendar ); assertThat( mappedSource.getDateTimeForCalendarConversion() ).isEqualTo( dateTimeWithCalendar );
} }
} }

View File

@ -23,9 +23,6 @@ import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime; import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime; import org.joda.time.LocalTime;
/**
*
*/
public class Source { public class Source {
private DateTime dateTime; private DateTime dateTime;

View File

@ -23,9 +23,6 @@ import org.mapstruct.Mapping;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
/**
*
*/
@Mapper @Mapper
public interface SourceTargetMapper { public interface SourceTargetMapper {
@ -39,43 +36,47 @@ public interface SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class ); SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
@Mappings({ @Mapping( source = "dateTime", dateFormat = DATE_TIME_FORMAT ), @Mappings({
@Mapping( source = "localDateTime", dateFormat = LOCAL_DATE_TIME_FORMAT ), @Mapping(source = "dateTime", dateFormat = DATE_TIME_FORMAT),
@Mapping( source = "localDate", dateFormat = LOCAL_DATE_FORMAT ), @Mapping(source = "localDateTime", dateFormat = LOCAL_DATE_TIME_FORMAT),
@Mapping( source = "localTime", dateFormat = LOCAL_TIME_FORMAT ) }) @Mapping(source = "localDate", dateFormat = LOCAL_DATE_FORMAT),
@Mapping(source = "localTime", dateFormat = LOCAL_TIME_FORMAT)
})
Target sourceToTarget(Source source); Target sourceToTarget(Source source);
Target sourceToTargetDefaultMapping(Source source); Target sourceToTargetDefaultMapping(Source source);
@Mapping( source = "dateTime", dateFormat = DATE_TIME_FORMAT ) @Mapping(source = "dateTime", dateFormat = DATE_TIME_FORMAT)
Target sourceToTargetDateTimeMapped(Source source); Target sourceToTargetDateTimeMapped(Source source);
@Mapping( source = "localDateTime", dateFormat = LOCAL_DATE_TIME_FORMAT ) @Mapping(source = "localDateTime", dateFormat = LOCAL_DATE_TIME_FORMAT)
Target sourceToTargetLocalDateTimeMapped(Source source); Target sourceToTargetLocalDateTimeMapped(Source source);
@Mapping( source = "localDate", dateFormat = LOCAL_DATE_FORMAT ) @Mapping(source = "localDate", dateFormat = LOCAL_DATE_FORMAT)
Target sourceToTargetLocalDateMapped(Source source); Target sourceToTargetLocalDateMapped(Source source);
@Mapping( source = "localTime", dateFormat = LOCAL_TIME_FORMAT ) @Mapping(source = "localTime", dateFormat = LOCAL_TIME_FORMAT)
Target sourceToTargetLocalTimeMapped(Source source); Target sourceToTargetLocalTimeMapped(Source source);
@Mappings({ @Mapping( source = "dateTime", dateFormat = DATE_TIME_FORMAT ), @Mappings({
@Mapping( source = "localDateTime", dateFormat = LOCAL_DATE_TIME_FORMAT ), @Mapping(source = "dateTime", dateFormat = DATE_TIME_FORMAT),
@Mapping( source = "localDate", dateFormat = LOCAL_DATE_FORMAT ), @Mapping(source = "localDateTime", dateFormat = LOCAL_DATE_TIME_FORMAT),
@Mapping( source = "localTime", dateFormat = LOCAL_TIME_FORMAT ) }) @Mapping(source = "localDate", dateFormat = LOCAL_DATE_FORMAT),
@Mapping(source = "localTime", dateFormat = LOCAL_TIME_FORMAT)
})
Source targetToSource(Target target); Source targetToSource(Target target);
@Mapping( source = "dateTime", dateFormat = DATE_TIME_FORMAT ) @Mapping(source = "dateTime", dateFormat = DATE_TIME_FORMAT)
Source targetToSourceDateTimeMapped(Target target); Source targetToSourceDateTimeMapped(Target target);
@Mapping( source = "localDateTime", dateFormat = LOCAL_DATE_TIME_FORMAT ) @Mapping(source = "localDateTime", dateFormat = LOCAL_DATE_TIME_FORMAT)
Source targetToSourceLocalDateTimeMapped(Target target); Source targetToSourceLocalDateTimeMapped(Target target);
@Mapping( source = "localDate", dateFormat = LOCAL_DATE_FORMAT ) @Mapping(source = "localDate", dateFormat = LOCAL_DATE_FORMAT)
Source targetToSourceLocalDateMapped(Target target); Source targetToSourceLocalDateMapped(Target target);
@Mapping( source = "localTime", dateFormat = LOCAL_TIME_FORMAT ) @Mapping(source = "localTime", dateFormat = LOCAL_TIME_FORMAT)
Source targetToSourceLocalTimeMapped(Target target); Source targetToSourceLocalTimeMapped(Target target);
Source targetToSourceDefaultMapping(Target target); Source targetToSourceDefaultMapping(Target target);

View File

@ -21,9 +21,6 @@ package org.mapstruct.ap.test.conversion.jodatime;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
/**
*
*/
public class Target { public class Target {
private String dateTime; private String dateTime;