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

org.mapstruct.ap.internal.model.StreamMappingMethod 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
 */
package org.mapstruct.ap.internal.model;

import org.mapstruct.ap.internal.model.assignment.Java8FunctionWrapper;
import org.mapstruct.ap.internal.model.common.Assignment;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.model.source.SelectionParameters;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static org.mapstruct.ap.internal.util.Collections.first;

/**
 * A {@link MappingMethod} implemented by a {@link Mapper} class which maps one iterable or array type to Stream.
 * The collection elements are mapped either by a {@link TypeConversion} or another mapping method.
 *
 * @author Filip Hrisafov
 */
public class StreamMappingMethod extends ContainerMappingMethod {

    private final Set helperImports;

    public static class Builder extends ContainerMappingMethodBuilder {

        public Builder() {
            super( Builder.class, "stream element" );
        }

        @Override
        protected Type getElementType(Type parameterType) {
            return StreamMappingMethod.getElementType( parameterType );
        }

        @Override
        protected Assignment getWrapper(Assignment assignment, Method method) {
            return new Java8FunctionWrapper( assignment );
        }

        @Override
        protected StreamMappingMethod instantiateMappingMethod(Method method, Collection existingVariables,
            Assignment assignment, MethodReference factoryMethod, boolean mapNullToDefault, String loopVariableName,
            List beforeMappingMethods,
            List afterMappingMethods, SelectionParameters selectionParameters) {

            Set helperImports = new HashSet<>();
            if ( method.getResultType().isIterableType() ) {
                helperImports.add( ctx.getTypeFactory().getType( Collectors.class ) );
            }

            Type sourceParameterType = first( method.getSourceParameters() ).getType();
            if ( !sourceParameterType.isCollectionType() && !sourceParameterType.isArrayType() &&
                sourceParameterType.isIterableType() ) {
                helperImports.add( ctx.getTypeFactory().getType( StreamSupport.class ) );
            }
            return new StreamMappingMethod(
                method,
                getMethodAnnotations(),
                existingVariables,
                assignment,
                factoryMethod,
                mapNullToDefault,
                loopVariableName,
                beforeMappingMethods,
                afterMappingMethods,
                selectionParameters,
                helperImports
            );
        }
    }
    //CHECKSTYLE:OFF
    private StreamMappingMethod(Method method, List annotations,
                                Collection existingVariables, Assignment parameterAssignment,
                                MethodReference factoryMethod, boolean mapNullToDefault, String loopVariableName,
                                List beforeMappingReferences,
                                List afterMappingReferences,
        SelectionParameters selectionParameters, Set helperImports) {
        super(
            method,
            annotations,
            existingVariables,
            parameterAssignment,
            factoryMethod,
            mapNullToDefault,
            loopVariableName,
            beforeMappingReferences,
            afterMappingReferences,
            selectionParameters
        );
        //CHECKSTYLE:ON
        this.helperImports = helperImports;
    }

    @Override
    public Set getImportTypes() {
        Set types = super.getImportTypes();

        types.addAll( helperImports );

        return types;
    }

    public Type getSourceElementType() {
        return getElementType( getSourceParameter().getType() );
    }

    public Type getResultElementType() {
        return getElementType( getResultType() );
    }

    private static Type getElementType(Type parameterType) {
        if ( parameterType.isArrayType() ) {
            return parameterType.getComponentType();
        }
        else if ( parameterType.isIterableType() ) {
            return first( parameterType.determineTypeArguments( Iterable.class ) ).getTypeBound();
        }
        else if ( parameterType.isStreamType() ) {
            return first( parameterType.determineTypeArguments( Stream.class ) ).getTypeBound();
        }

        throw new IllegalArgumentException( "Could not get the element type" );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy