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
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ public interface CarMapper {
* Between `java.net.URL` and `String`.
** When converting from a `String`, the value needs to be a valid https://en.wikipedia.org/wiki/URL[URL] otherwise a `MalformedURLException` is thrown.

* Between `java.util.Locale` and `String`.
** When converting from a `Locale`, the resulting `String` will be a well-formed IETF BCP 47 language tag representing the locale. When converting from a `String`, the locale that best represents the language tag will be returned. See https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html#forLanguageTag-java.lang.String-[Locale.forLanguageTag()] and https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html#toLanguageTag--[Locale.toLanguageTag()] for more information.

[[mapping-object-references]]
=== Mapping object references

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Currency;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
Expand Down Expand Up @@ -198,6 +199,7 @@ public Conversions(TypeFactory typeFactory) {
register( Currency.class, String.class, new CurrencyToStringConversion() );

register( UUID.class, String.class, new UUIDToStringConversion() );
register( Locale.class, String.class, new LocaleToStringConversion() );

registerURLConversion();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.internal.conversion;

import java.util.Locale;
import java.util.Set;

import org.mapstruct.ap.internal.model.common.ConversionContext;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.util.Collections;

import static org.mapstruct.ap.internal.conversion.ConversionUtils.locale;

/**
* Conversion between {@link java.util.Locale} and {@link String}.
*
* @author Jason Bodnar
*/
public class LocaleToStringConversion extends SimpleConversion {
@Override
protected String getToExpression(ConversionContext conversionContext) {
return "<SOURCE>.toLanguageTag()";
}

@Override
protected String getFromExpression(ConversionContext conversionContext) {
return locale( conversionContext ) + ".forLanguageTag( <SOURCE> )";
}

@Override
protected Set<Type> getFromConversionImportTypes(final ConversionContext conversionContext) {
return Collections.asSet( conversionContext.getTypeFactory().getType( Locale.class ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.conversion.locale;

import java.util.Locale;

import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;

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

/**
* Tests conversions between {@link Locale} and String.
*
* @author Jason Bodnar
*/
@IssueKey("3172")
@WithClasses({ LocaleSource.class, LocaleTarget.class, LocaleMapper.class })
public class LocaleConversionTest {

@ProcessorTest
public void shouldApplyLocaleConversion() {
LocaleSource source = new LocaleSource();
source.setLocaleA( Locale.getDefault() );

LocaleTarget target = LocaleMapper.INSTANCE.sourceToTarget( source );

assertThat( target ).isNotNull();
assertThat( target.getLocaleA() ).isEqualTo( source.getLocaleA().toLanguageTag() );
}

@ProcessorTest
public void shouldApplyReverseLocaleConversion() {
LocaleTarget target = new LocaleTarget();
target.setLocaleA( Locale.getDefault().toLanguageTag() );

LocaleSource source = LocaleMapper.INSTANCE.targetToSource( target );

assertThat( source ).isNotNull();
assertThat( source.getLocaleA() ).isEqualTo( Locale.forLanguageTag( target.getLocaleA() ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.conversion.locale;

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

@Mapper
public interface LocaleMapper {

LocaleMapper INSTANCE = Mappers.getMapper( LocaleMapper.class );

LocaleTarget sourceToTarget(LocaleSource source);

LocaleSource targetToSource(LocaleTarget target);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.conversion.locale;

import java.util.Locale;

/**
* @author Jason Bodnar
*/
public class LocaleSource {
private Locale localeA;

public Locale getLocaleA() {
return localeA;
}

public void setLocaleA(Locale localeA) {
this.localeA = localeA;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.conversion.locale;

/**
* @author Jason Bodnar
*/
public class LocaleTarget {
private String localeA;

public String getLocaleA() {
return localeA;
}

public void setLocaleA(String localeA) {
this.localeA = localeA;
}
}