All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mapstruct.ap.internal.model.StreamMappingMethod.ftl Maven / Gradle / Ivy

There is a newer version: 1.6.3
Show newest version
<#--

    Copyright MapStruct Authors.

    Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0

-->
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.StreamMappingMethod" -->
<#if overridden>@Override
<#lt>${accessibility.keyword} <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, )<@throws/> {
    <#--TODO does it even make sense to do a callback if the result is a Stream, as they are immutable-->
    <#list beforeMappingReferencesWithoutMappingTarget as callback>
    	<@includeModel object=callback targetBeanName=resultName targetType=resultType/>
    	<#if !callback_has_next>

    	
    
    if ( ${sourceParameter.name} == null ) {
        <#if !mapNullToDefault>
            <#-- returned target type starts to miss-align here with target handed via param, TODO is this right? -->
            return<#if returnType.name != "void"> null;
        <#else>
            <#if resultType.arrayType>
                <#if existingInstanceMapping>
                    <#-- we can't clear an existing array, so we've got to clear by setting values to default -->
                    for (int ${index2Name} = 0; ${index2Name} < ${resultName}.length; ${index2Name}++ ) {
                        ${resultName}[${index2Name}] = ${resultElementType.null};
                    }
                    return<#if returnType.name != "void"> ${resultName};
                <#else>
                    return new <@includeModel object=resultElementType/>[0];
                
            <#elseif resultType.iterableType>
                <#if existingInstanceMapping>
                    ${resultName}.clear();
                    return<#if returnType.name != "void"> ${resultName};
                <#else>
                    return <@includeModel object=iterableCreation useSizeIfPossible=false/>;
                
            <#else>
                <#if existingInstanceMapping>
                    <#-- We cannot update an existing stream so we just return the old one -->
                    return<#if returnType.name != "void"> ${resultName};
                <#else>
                    return Stream.empty();
                
            
        
    }

    <#-- A variable needs to be defined if there are before or after mappings and this is not exisitingInstanceMapping -->
    <#assign needVarDefine = (beforeMappingReferencesWithMappingTarget?has_content || afterMappingReferences?has_content) && !existingInstanceMapping />

    <#if resultType.arrayType>
        <#if needVarDefine>
            <#assign needVarDefine = false />
            <#-- We create a null array which later will be directly assigned from the stream-->
            ${resultElementType}[] ${resultName} = null;
        
    <#elseif resultType.iterableType>
        <#if existingInstanceMapping>
            ${resultName}.clear();
        <#elseif needVarDefine>
            <#assign needVarDefine = false />
            <#-- Use the interface type on the left side, except it is java.lang.Iterable; use the implementation type - if present - on the right side -->
            <@iterableLocalVarDef/> ${resultName} = <@includeModel object=iterableCreation useSizeIfPossible=true/>;
        
    <#else>
        <#-- Streams are immutable so we can't update them -->
        <#if needVarDefine>
            <#assign needVarDefine = false />
            <@iterableLocalVarDef/> ${resultName} = Stream.empty();
        
    

    <#list beforeMappingReferencesWithMappingTarget as callback>
        <@includeModel object=callback targetBeanName=resultName targetType=resultType/>
        <#if !callback_has_next>

        
    

    <#-- If there are no after mappings, no variable was created before i.e. no before mappings
        and this is not an existingInstanceMapping then we can return immediatelly -->
    <#assign canReturnImmediatelly = !afterMappingReferences?has_content && !needVarDefine && !existingInstanceMapping/>

    <#if resultType.arrayType>
        <#if existingInstanceMapping>
        int ${index1Name} = 0;
        for ( <@includeModel object=resultElementType/> ${loopVariableName} : ${sourceParameter.name}.limit( ${resultName}.length )<@streamMapSupplier />.toArray( ${resultElementType}[]::new ) ) {
            if ( ( ${index1Name} >= ${resultName}.length ) ) {
                break;
            }
            ${resultName}[${index1Name}++] = ${loopVariableName};
        }
        <#else>
            <#if canReturnImmediatelly><#if returnType.name != "void">return <#else> <#if needVarDefine>${resultElementType}[] <#else>${resultName} = ${sourceParameter.name}<@streamMapSupplier />
                        .toArray( <@includeModel object=resultElementType/>[]::new );
        
    <#elseif resultType.iterableType>
        <#if existingInstanceMapping || !canReturnImmediatelly>
            ${resultName}.addAll( ${sourceParameter.name}<@streamMapSupplier />
                                    .collect( Collectors.toCollection( <@iterableCollectionSupplier /> ) )
                                );
        <#else>
            <@returnLocalVarDefOrUpdate>
                <#lt>${sourceParameter.name}<@streamMapSupplier />
                    .collect( Collectors.toCollection( <@iterableCollectionSupplier /> ) );
            

        
    <#else>
        <#-- Streams are immutable so we can't update them -->
        <#if !existingInstanceMapping>
            <#--TODO fhr: after the the result is no longer the same instance, how does it affect the
                Before mapping methods. Does it even make sense to have before mapping on a stream? -->
            <#if sourceParameter.type.arrayType>
                <@returnLocalVarDefOrUpdate>Stream.of( ${sourceParameter.name} )<@streamMapSupplier />;
            <#elseif sourceParameter.type.collectionType>
                <@returnLocalVarDefOrUpdate>${sourceParameter.name}.stream()<@streamMapSupplier />;
            <#elseif sourceParameter.type.iterableType>
                <@returnLocalVarDefOrUpdate>StreamSupport.stream( ${sourceParameter.name}.spliterator(), false )<@streamMapSupplier />;
            <#else>
                <@returnLocalVarDefOrUpdate>${sourceParameter.name}<@streamMapSupplier />;
            
        

    

    <#list afterMappingReferences as callback>
    	<#if callback_index = 0>

    	
    	<@includeModel object=callback targetBeanName=resultName targetType=resultType/>
    

    <#if !canReturnImmediatelly && returnType.name != "void">
        return ${resultName};
    
}
<#macro throws>
    <#if (thrownTypes?size > 0)><#lt> throws <@compress single_line=true>
        <#list thrownTypes as exceptionType>
            <@includeModel object=exceptionType/>
            <#if exceptionType_has_next>, <#t>
        
    

<#macro iterableSize>
    <@compress single_line=true>
        <#if sourceParameter.type.arrayType>
           ${sourceParameter.name}.length
        <#else>
           ${sourceParameter.name}.size()
        
    

<#macro iterableLocalVarDef>
    <@compress single_line=true>
        <#if resultType.fullyQualifiedName == "java.lang.Iterable">
            <@includeModel object=resultType.implementationType/>
        <#else>
            <@includeModel object=resultType/>
        
    

<#macro iterableCollectionSupplier>
    <@compress single_line=true>
        <#if resultType.implementationType??>
            <@includeModel object=resultType.implementationType/>
        <#else>
            <@includeModel object=resultType/>::new
    

<#macro streamMapSupplier>
    <@compress>
        <#if !elementAssignment.directAssignment?? || !elementAssignment.directAssignment>
            .map( <@includeModel object=elementAssignment targetBeanName=resultName targetType=resultElementType/> )
        
    

<#macro returnLocalVarDefOrUpdate>
    <#if canReturnImmediatelly><#if returnType.name != "void">return <#elseif needVarDefine><@iterableLocalVarDef/> ${resultName} = <#else>${resultName} = <#nested />





© 2015 - 2024 Weber Informatics LLC | Privacy Policy