org.mapstruct.ap.internal.conversion.PrimitiveToStringConversion Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapstruct-processor Show documentation
Show all versions of mapstruct-processor Show documentation
An annotation processor for generating type-safe bean mappers
/*
* 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 primitive types such as {@code byte} or {@code long} and
* {@link String}.
*
* @author Gunnar Morling
*/
public class PrimitiveToStringConversion extends AbstractNumberToStringConversion {
private final Class> sourceType;
private final Class> wrapperType;
public PrimitiveToStringConversion(Class> sourceType) {
super( NativeTypes.isNumber( sourceType ) );
if ( !sourceType.isPrimitive() ) {
throw new IllegalArgumentException( sourceType + " is no primitive type." );
}
this.sourceType = sourceType;
this.wrapperType = NativeTypes.getWrapperType( sourceType );
}
@Override
public String getToExpression(ConversionContext conversionContext) {
if ( requiresDecimalFormat( conversionContext ) ) {
StringBuilder sb = new StringBuilder();
appendDecimalFormatter( sb, conversionContext );
sb.append( ".format(