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;
/**
* Base class for conversions between Joda-Time types and String.
*
* @author Timo Eckhardt
*/
public abstract class AbstractJodaTypeToStringConversion extends SimpleConversion {
@ -83,11 +85,17 @@ public abstract class AbstractJodaTypeToStringConversion extends SimpleConversio
return conversionString.toString();
}
public String defaultDateFormatPattern() {
private String defaultDateFormatPattern() {
return "DateTimeFormat.patternForStyle( \"" + formatStyle() + "\", Locale.getDefault() )";
}
/**
* Returns the default format style to be applied if non is given explicitly.
*/
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();
}

View File

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

View File

@ -18,6 +18,7 @@
*/
package org.mapstruct.ap.conversion;
import java.util.Calendar;
import java.util.Locale;
import java.util.Set;
@ -27,9 +28,11 @@ import org.mapstruct.ap.model.common.Type;
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
protected String getToExpression(ConversionContext conversionContext) {

View File

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

View File

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

View File

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

View File

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

View File

@ -19,7 +19,9 @@
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 {

View File

@ -18,6 +18,7 @@
*/
package org.mapstruct.ap.conversion;
import java.util.Date;
import java.util.Set;
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;
/**
* Implementation of {@link org.mapstruct.ap.conversion.ConversionProvider} mapping Joda Types
* Conversion between the following Joda types and {@link Date}:
* <ul>
* <li>org.joda.time.DateTime</li>
* <li>org.joda.time.LocalDateTime</li>
* <li>org.joda.time.LocalDate</li>
* </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 {

View File

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

View File

@ -23,9 +23,6 @@ import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
*
*/
@Mapper
public interface SourceTargetMapper {
@ -39,10 +36,12 @@ public interface SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
@Mappings({ @Mapping( source = "dateTime", dateFormat = DATE_TIME_FORMAT ),
@Mappings({
@Mapping(source = "dateTime", 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 ) })
@Mapping(source = "localTime", dateFormat = LOCAL_TIME_FORMAT)
})
Target sourceToTarget(Source source);
Target sourceToTargetDefaultMapping(Source source);
@ -60,10 +59,12 @@ public interface SourceTargetMapper {
Target sourceToTargetLocalTimeMapped(Source source);
@Mappings({ @Mapping( source = "dateTime", dateFormat = DATE_TIME_FORMAT ),
@Mappings({
@Mapping(source = "dateTime", 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 ) })
@Mapping(source = "localTime", dateFormat = LOCAL_TIME_FORMAT)
})
Source targetToSource(Target target);
@Mapping(source = "dateTime", dateFormat = DATE_TIME_FORMAT)

View File

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