Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.time.format.DateTimeFormatter;
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 org.mapstruct.ap.internal.util.Strings;
/**
*
* Base type for mapping Java 8 time types to String and vice versa.
*
*
* In general each type comes with a "parse" method to convert a string to this particular type.
* For formatting a dedicated instance of {@link java.time.format.DateTimeFormatter} is used.
*
*
* If no date format for mapping is specified predefined ISO* formatters from
* {@link java.time.format.DateTimeFormatter} are used.
*
*
* An overview of date and time types shipped with Java 8 can be found at
* http://docs.oracle.com/javase/tutorial/datetime/iso/index.html.
*
*/
public abstract class AbstractJavaTimeToStringConversion extends SimpleConversion {
@Override
protected String getToExpression(ConversionContext conversionContext) {
return dateTimeFormatter( conversionContext ) + ".format( )";
}
private String dateTimeFormatter(ConversionContext conversionContext) {
if ( !Strings.isEmpty( conversionContext.getDateFormat() ) ) {
return ConversionUtils.dateTimeFormatter( conversionContext )
+ ".ofPattern( \"" + conversionContext.getDateFormat()
+ "\" )";
}
else {
return ConversionUtils.dateTimeFormatter( conversionContext ) + "." + defaultFormatterSuffix();
}
}
protected abstract String defaultFormatterSuffix();
@Override
protected String getFromExpression(ConversionContext conversionContext) {
// See http://docs.oracle.com/javase/tutorial/datetime/iso/format.html for how to parse Dates
return new StringBuilder().append( conversionContext.getTargetType().createReferenceName() )
.append( ".parse( " )
.append( parametersListForParsing( conversionContext ) )
.append( " )" ).toString();
}
private String parametersListForParsing(ConversionContext conversionContext) {
// See http://docs.oracle.com/javase/tutorial/datetime/iso/format.html for how to format Dates
StringBuilder parameterBuilder = new StringBuilder( "" );
if ( !Strings.isEmpty( conversionContext.getDateFormat() ) ) {
parameterBuilder.append( ", " );
parameterBuilder.append( dateTimeFormatter( conversionContext ) );
}
return parameterBuilder.toString();
}
@Override
protected Set getToConversionImportTypes(ConversionContext conversionContext) {
return Collections.asSet(
conversionContext.getTypeFactory().getType( DateTimeFormatter.class )
);
}
@Override
protected Set getFromConversionImportTypes(ConversionContext conversionContext) {
if ( !Strings.isEmpty( conversionContext.getDateFormat() ) ) {
return Collections.asSet(
conversionContext.getTargetType(),
conversionContext.getTypeFactory().getType( DateTimeFormatter.class )
);
}
return Collections.asSet( conversionContext.getTargetType() );
}
}