Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions core/src/main/java/org/mapstruct/IterableMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,38 @@
/**
* A format string as processable by {@link SimpleDateFormat} if the annotated method maps from an iterable of
* {@code String} to an iterable {@link Date} or vice-versa. Will be ignored for all other element types.
* <p>
* If the {@link #locale()} is also specified, the format will consider the specified locale when processing
* the date. Otherwise, the system's default locale will be used.
*
* @return A date format string as processable by {@link SimpleDateFormat}.
* @see #locale()
*/
String dateFormat() default "";

/**
* A format string as processable by {@link DecimalFormat} if the annotated method maps from a
* {@link Number} to a {@link String} or vice-versa. Will be ignored for all other element types.
* <p>
* If the {@link #locale()} is also specified, the number format will be applied in the context of the given locale.
* Otherwise, the system's default locale will be used to process the number format.
*
* @return A decimal format string as processable by {@link DecimalFormat}.
* @see #locale()
*/
String numberFormat() default "";

/**
* Specifies the locale to be used when processing {@link #dateFormat()} or {@link #numberFormat()}.
* <p>
* The locale should be a plain tag representing the language, such as "en" for English, "de" for German, etc.
* <p>
* If no locale is specified, the system's default locale will be used.
*
* @return A string representing the locale to be used when formatting dates or numbers.
*/
String locale() default "";

/**
* A qualifier can be specified to aid the selection process of a suitable mapper. This is useful in case multiple
* mappers (hand written of internal) qualify and result in an 'Ambiguous mapping methods found' error.
Expand Down
27 changes: 27 additions & 0 deletions core/src/main/java/org/mapstruct/MapMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,63 @@
/**
* A format string as processable by {@link SimpleDateFormat} if the annotated method maps from a map with key type
* {@code String} to an map with key type {@link Date} or vice-versa. Will be ignored for all other key types.
* <p>
* If the {@link #locale()} is specified, the format will consider the specified locale when processing the date.
* Otherwise, the system's default locale will be used.
*
* @return A date format string as processable by {@link SimpleDateFormat}.
* @see #locale()
*/
String keyDateFormat() default "";

/**
* A format string as processable by {@link SimpleDateFormat} if the annotated method maps from a map with value
* type {@code String} to an map with value type {@link Date} or vice-versa. Will be ignored for all other value
* types.
* <p>
* If the {@link #locale()} is specified, the format will consider the specified locale when processing the date.
* Otherwise, the system's default locale will be used.
*
* @return A date format string as processable by {@link SimpleDateFormat}.
* @see #locale()
*/
String valueDateFormat() default "";

/**
* A format string as processable by {@link DecimalFormat} if the annotated method maps from a
* {@link Number} to a {@link String} or vice-versa. Will be ignored for all other key types.
* <p>
* If the {@link #locale()} is specified, the number format will be applied in the context of the given locale.
* Otherwise, the system's default locale will be used.
*
* @return A decimal format string as processable by {@link DecimalFormat}.
* @see #locale()
*/
String keyNumberFormat() default "";

/**
* A format string as processable by {@link DecimalFormat} if the annotated method maps from a
* {@link Number} to a {@link String} or vice-versa. Will be ignored for all other value types.
* <p>
* If the {@link #locale()} is specified, the number format will be applied in the context of the given locale.
* Otherwise, the system's default locale will be used.
*
* @return A decimal format string as processable by {@link DecimalFormat}.
* @see #locale()
*/
String valueNumberFormat() default "";

/**
* Specifies the locale to be used when processing {@link SimpleDateFormat} or {@link DecimalFormat} for key or
* value mappings in maps. The locale should be a plain tag representing the language, such as "en" for English,
* "de" for German, etc.
* <p>
* If no locale is specified, the system's default locale will be used.
*
* @return A string representing the locale to be used when formatting dates or numbers in maps.
*/
String locale() default "";

/**
* A key value qualifier can be specified to aid the selection process of a suitable mapper. This is useful in
* case multiple mappers (hand written of internal) qualify and result in an 'Ambiguous mapping methods found'
Expand Down
19 changes: 19 additions & 0 deletions core/src/main/java/org/mapstruct/Mapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,38 @@
/**
* A format string as processable by {@link SimpleDateFormat} if the attribute is mapped from {@code String} to
* {@link Date} or vice-versa. Will be ignored for all other attribute types and when mapping enum constants.
* <p>
* If the {@link #locale()} is also specified, the format will consider the specified locale when processing
* the date. Otherwise, the system's default locale will be used.
*
* @return A date format string as processable by {@link SimpleDateFormat}.
* @see #locale()
*/
String dateFormat() default "";

/**
* A format string as processable by {@link DecimalFormat} if the annotated method maps from a
* {@link Number} to a {@link String} or vice-versa. Will be ignored for all other element types.
* <p>
* If the {@link #locale()} is also specified, the number format will be applied in the context of the given locale.
* Otherwise, the system's default locale will be used to process the number format.
*
* @return A decimal format string as processable by {@link DecimalFormat}.
* @see #locale()
*/
String numberFormat() default "";

/**
* Specifies the locale to be used when processing {@link #dateFormat()} or {@link #numberFormat()}.
* <p>
* The locale should be a plain tag representing the language, such as "en" for English, "de" for German, etc.
* <p>
* If no locale is specified, the system's default locale will be used.
*
* @return A string representing the locale to be used when formatting dates or numbers.
*/
String locale() default "";

/**
* A constant {@link String} based on which the specified target property is to be set.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
import org.mapstruct.ap.internal.model.common.ConversionContext;
import org.mapstruct.ap.internal.model.common.Type;

import static org.mapstruct.ap.internal.util.Collections.asSet;
import static org.mapstruct.ap.internal.conversion.ConversionUtils.bigDecimal;
import static org.mapstruct.ap.internal.conversion.ConversionUtils.locale;
import static org.mapstruct.ap.internal.util.Collections.asSet;

/**
* Conversion between {@link BigDecimal} and {@link String}.
Expand Down Expand Up @@ -64,18 +65,31 @@ protected Set<Type> getFromConversionImportTypes(ConversionContext conversionCon
public List<HelperMethod> getRequiredHelperMethods(ConversionContext conversionContext) {
List<HelperMethod> helpers = new ArrayList<>();
if ( conversionContext.getNumberFormat() != null ) {
helpers.add( new CreateDecimalFormat( conversionContext.getTypeFactory() ) );
helpers.add( new CreateDecimalFormat(
conversionContext.getTypeFactory(),
conversionContext.getLocale() != null
) );
}
return helpers;
}

private void appendDecimalFormatter(StringBuilder sb, ConversionContext conversionContext) {
sb.append( "createDecimalFormat( " );
boolean withLocale = conversionContext.getLocale() != null;
sb.append( "createDecimalFormat" );
if ( withLocale ) {
sb.append( "WithLocale" );
}
sb.append( "( " );
if ( conversionContext.getNumberFormat() != null ) {
sb.append( "\"" );
sb.append( conversionContext.getNumberFormat() );
sb.append( "\"" );
}
if ( withLocale ) {
sb.append( ", " ).append( locale( conversionContext ) ).append( ".forLanguageTag( \"" );
sb.append( conversionContext.getLocale() );
sb.append( "\" )" );
}

sb.append( " )" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
import org.mapstruct.ap.internal.model.common.ConversionContext;
import org.mapstruct.ap.internal.model.common.Type;

import static org.mapstruct.ap.internal.util.Collections.asSet;
import static org.mapstruct.ap.internal.conversion.ConversionUtils.locale;
import static org.mapstruct.ap.internal.conversion.ConversionUtils.bigDecimal;
import static org.mapstruct.ap.internal.conversion.ConversionUtils.bigInteger;
import static org.mapstruct.ap.internal.util.Collections.asSet;

/**
* Conversion between {@link BigInteger} and {@link String}.
Expand Down Expand Up @@ -72,18 +73,31 @@ protected Set<Type> getFromConversionImportTypes(ConversionContext conversionCon
public List<HelperMethod> getRequiredHelperMethods(ConversionContext conversionContext) {
List<HelperMethod> helpers = new ArrayList<>();
if ( conversionContext.getNumberFormat() != null ) {
helpers.add( new CreateDecimalFormat( conversionContext.getTypeFactory() ) );
helpers.add( new CreateDecimalFormat(
conversionContext.getTypeFactory(),
conversionContext.getLocale() != null
) );
}
return helpers;
}

private void appendDecimalFormatter(StringBuilder sb, ConversionContext conversionContext) {
sb.append( "createDecimalFormat( " );
boolean withLocale = conversionContext.getLocale() != null;
sb.append( "createDecimalFormat" );
if ( withLocale ) {
sb.append( "WithLocale" );
}
sb.append( "( " );
if ( conversionContext.getNumberFormat() != null ) {
sb.append( "\"" );
sb.append( conversionContext.getNumberFormat() );
sb.append( "\"" );
}
if ( withLocale ) {
sb.append( ", " ).append( locale( conversionContext ) ).append( ".forLanguageTag( \"" );
sb.append( conversionContext.getLocale() );
sb.append( "\" )" );
}

sb.append( " )" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -279,4 +280,15 @@ public static String url(ConversionContext conversionContext) {
return typeReferenceName( conversionContext, URL.class );
}

/**
* Name for {@link java.text.DecimalFormatSymbols}.
*
* @param conversionContext Conversion context
*
* @return Name or fully-qualified name.
*/
public static String decimalFormatSymbols(ConversionContext conversionContext) {
return typeReferenceName( conversionContext, DecimalFormatSymbols.class );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
*/
package org.mapstruct.ap.internal.conversion;

import static org.mapstruct.ap.internal.util.Collections.asSet;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.mapstruct.ap.internal.model.HelperMethod;
Expand All @@ -16,6 +18,8 @@
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.model.source.MappingMethodOptions;

import static org.mapstruct.ap.internal.util.Collections.asSet;

/**
* HelperMethod that creates a {@link java.text.DecimalFormat}
*
Expand All @@ -27,13 +31,30 @@
public class CreateDecimalFormat extends HelperMethod {

private final Parameter parameter;
private final Parameter localeParameter;
private final Type returnType;
private final Set<Type> importTypes;

public CreateDecimalFormat(TypeFactory typeFactory) {
public CreateDecimalFormat(TypeFactory typeFactory, boolean withLocale) {
this.parameter = new Parameter( "numberFormat", typeFactory.getType( String.class ) );
this.localeParameter = withLocale ? new Parameter( "locale", typeFactory.getType( Locale.class ) ) : null;
this.returnType = typeFactory.getType( DecimalFormat.class );
this.importTypes = asSet( parameter.getType(), returnType );
if ( withLocale ) {
this.importTypes = asSet(
parameter.getType(),
returnType,
typeFactory.getType( DecimalFormatSymbols.class ),
typeFactory.getType( Locale.class )
);
}
else {
this.importTypes = asSet( parameter.getType(), returnType );
}
}

@Override
public String getName() {
return localeParameter == null ? "createDecimalFormat" : "createDecimalFormatWithLocale";
}

@Override
Expand All @@ -60,4 +81,12 @@ public MappingMethodOptions getOptions() {
public String describe() {
return null;
}

@Override
public List<Parameter> getParameters() {
if ( localeParameter == null ) {
return super.getParameters();
}
return Arrays.asList( getParameter(), localeParameter );
}
}
Loading