org.mapstruct.ap.internal.conversion.WrapperToStringConversion Maven / Gradle / Ivy
/*
* 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.text.DecimalFormat;
import java.util.Collections;
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.NativeTypes;
import org.mapstruct.ap.internal.util.Strings;
import static org.mapstruct.ap.internal.conversion.ConversionUtils.decimalFormat;
/**
* Conversion between wrapper types such as {@link Integer} and {@link String}.
*
* @author Gunnar Morling
*/
public class WrapperToStringConversion extends AbstractNumberToStringConversion {
private final Class> sourceType;
private final Class> primitiveType;
public WrapperToStringConversion(Class> sourceType) {
super( NativeTypes.isNumber( sourceType ) );
if ( sourceType.isPrimitive() ) {
throw new IllegalArgumentException( sourceType + " is no wrapper type." );
}
this.sourceType = sourceType;
this.primitiveType = NativeTypes.getPrimitiveType( sourceType );
}
@Override
public String getToExpression(ConversionContext conversionContext) {
if ( requiresDecimalFormat( conversionContext ) ) {
StringBuilder sb = new StringBuilder();
appendDecimalFormatter( sb, conversionContext );
sb.append( ".format(