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

org.apache.maven.graph.effective.EProjectRelationships Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
package org.apache.maven.graph.effective;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.maven.graph.common.DependencyScope;
import org.apache.maven.graph.common.ref.ArtifactRef;
import org.apache.maven.graph.common.ref.ProjectRef;
import org.apache.maven.graph.common.ref.ProjectVersionRef;
import org.apache.maven.graph.effective.ref.EGraphFacts;
import org.apache.maven.graph.effective.ref.EProjectKey;
import org.apache.maven.graph.effective.rel.DependencyRelationship;
import org.apache.maven.graph.effective.rel.ExtensionRelationship;
import org.apache.maven.graph.effective.rel.ParentRelationship;
import org.apache.maven.graph.effective.rel.PluginDependencyRelationship;
import org.apache.maven.graph.effective.rel.PluginRelationship;
import org.apache.maven.graph.effective.rel.ProjectRelationship;

public class EProjectRelationships
    implements EProjectRelationshipCollection, KeyedProjectRelationshipCollection, Serializable
{

    private static final long serialVersionUID = 1L;

    private final EProjectKey key;

    private final List dependencies;

    private final List managedDependencies;

    private final List plugins;

    private final List managedPlugins;

    private final List extensions;

    private final ParentRelationship parent;

    private final Map> pluginDependencies;

    public EProjectRelationships( final EProjectKey key, final ParentRelationship parent,
                                  final List dependencies,
                                  final List plugins,
                                  final List managedDependencies,
                                  final List managedPlugins,
                                  final List extensions,
                                  final Map> pluginDependencies )
    {
        this.key = key;
        this.parent = parent;
        this.dependencies = Collections.unmodifiableList( dependencies );
        this.plugins = Collections.unmodifiableList( plugins );
        this.managedDependencies = Collections.unmodifiableList( managedDependencies );
        this.managedPlugins = Collections.unmodifiableList( managedPlugins );
        this.extensions = Collections.unmodifiableList( extensions );

        final HashMap> pdrels =
            new HashMap>();

        for ( final Map.Entry> entry : pluginDependencies.entrySet() )
        {
            pdrels.put( entry.getKey(), Collections.unmodifiableList( entry.getValue() ) );
        }

        this.pluginDependencies = Collections.unmodifiableMap( pdrels );
    }

    public EProjectKey getKey()
    {
        return key;
    }

    public final EGraphFacts getFacts()
    {
        return key.getFacts();
    }

    public final ProjectVersionRef getProjectRef()
    {
        return key.getProject();
    }

    public final List getDependencies()
    {
        return dependencies;
    }

    public final List getManagedDependencies()
    {
        return managedDependencies;
    }

    public final List getPlugins()
    {
        return plugins;
    }

    public final List getManagedPlugins()
    {
        return managedPlugins;
    }

    public final List getExtensions()
    {
        return extensions;
    }

    public final ParentRelationship getParent()
    {
        return parent;
    }

    public final Map> getPluginDependencies()
    {
        return pluginDependencies;
    }

    public final List getPluginDependencies( final ProjectVersionRef plugin,
                                                                           final boolean managed )
    {
        final PluginRelationship pr = new PluginRelationship( getProjectRef(), plugin, 0, managed );
        return pluginDependencies.get( pr );
    }

    public Set> getAllRelationships()
    {
        final Set> result = new HashSet>();
        if ( parent != null )
        {
            result.add( parent );
        }

        result.addAll( dependencies );
        result.addAll( managedDependencies );
        result.addAll( plugins );
        result.addAll( managedPlugins );
        result.addAll( extensions );

        for ( final List pluginRels : pluginDependencies.values() )
        {
            result.addAll( pluginRels );
        }

        return result;
    }

    public static final class Builder
    {
        private final EProjectKey key;

        private final List dependencies = new ArrayList();

        private final List managedDependencies = new ArrayList();

        private final List plugins = new ArrayList();

        private final List managedPlugins = new ArrayList();

        private final List extensions = new ArrayList();

        private ParentRelationship parent;

        private final Map> pluginDependencies =
            new HashMap>();

        public Builder( final ProjectVersionRef projectRef, final String... activeProfiles )
        {
            this.key = new EProjectKey( projectRef, new EGraphFacts( activeProfiles ) );
        }

        public Builder( final EProjectKey key )
        {
            this.key = key;
        }

        public EProjectRelationships build()
        {
            return new EProjectRelationships( key, parent, dependencies, plugins, managedDependencies, managedPlugins,
                                              extensions, pluginDependencies );
        }

        public Builder withParent( final ProjectVersionRef parent )
        {
            this.parent = new ParentRelationship( key.getProject(), parent );
            return this;
        }

        public Builder withParent( final ParentRelationship parent )
        {
            this.parent = adjustDeclaring( parent );

            return this;
        }

        @SuppressWarnings( "unchecked" )
        private > C adjustDeclaring( final C rel )
        {
            if ( !key.getProject()
                     .equals( rel.getDeclaring() ) )
            {
                return (C) rel.cloneFor( key.getProject() );
            }

            return rel;
        }

        public Builder withDependencies( final DependencyRelationship... deps )
        {
            return withDependencies( Arrays.asList( deps ) );
        }

        public Builder withDependencies( final Collection deps )
        {
            for ( DependencyRelationship dep : deps )
            {
                dep = adjustDeclaring( dep );
                if ( dep.isManaged() )
                {
                    if ( !managedDependencies.contains( dep ) )
                    {
                        managedDependencies.add( dep );
                    }
                }
                else
                {
                    if ( !dependencies.contains( dep ) )
                    {
                        dependencies.add( dep );
                    }
                }
            }

            return this;
        }

        public Builder withPlugins( final PluginRelationship... plugins )
        {
            return withPlugins( Arrays.asList( plugins ) );
        }

        public Builder withPlugins( final Collection plugins )
        {
            for ( PluginRelationship plugin : plugins )
            {
                plugin = adjustDeclaring( plugin );
                if ( plugin.isManaged() )
                {
                    if ( !managedPlugins.contains( plugin ) )
                    {
                        managedPlugins.add( plugin );
                    }
                }
                else
                {
                    if ( !this.plugins.contains( plugin ) )
                    {
                        this.plugins.add( plugin );
                    }
                }
            }

            return this;
        }

        public Builder withPluginDependencies( final PluginDependencyRelationship... pluginDeps )
        {
            return withPluginDependencies( Arrays.asList( pluginDeps ) );
        }

        public Builder withPluginDependencies( final Collection pluginDeps )
        {
            for ( PluginDependencyRelationship rel : pluginDeps )
            {
                rel = adjustDeclaring( rel );
                final ProjectRef pluginRef = rel.getPlugin();

                PluginRelationship pr = null;
                if ( rel.isManaged() )
                {
                    for ( final PluginRelationship pluginRel : managedPlugins )
                    {
                        if ( pluginRef.equals( pluginRel.getTarget() ) )
                        {
                            pr = pluginRel;
                            break;
                        }
                    }
                }
                else
                {
                    for ( final PluginRelationship pluginRel : plugins )
                    {
                        if ( pluginRef.equals( pluginRel.getTarget() ) )
                        {
                            pr = pluginRel;
                            break;
                        }
                    }
                }

                if ( pr == null )
                {
                    throw new IllegalArgumentException(
                                                        "Orphaned plugin-level dependency found: "
                                                            + rel
                                                            + ". Make sure you load plugin relationships BEFORE attempting to load plugin-dependency-relationships." );
                }

                List pdrs = pluginDependencies.get( pr );
                if ( pdrs == null )
                {
                    pdrs = new ArrayList();
                    pluginDependencies.put( pr, pdrs );
                }

                if ( !pdrs.contains( rel ) )
                {
                    pdrs.add( rel );
                }
            }

            return this;
        }

        public Builder withExtensions( final ExtensionRelationship... exts )
        {
            return withExtensions( Arrays.asList( exts ) );
        }

        public Builder withExtensions( final Collection exts )
        {
            for ( final ExtensionRelationship ext : exts )
            {
                if ( !extensions.contains( ext ) )
                {
                    extensions.add( adjustDeclaring( ext ) );
                }
            }

            return this;
        }

        public Builder withRelationships( final Collection> relationships )
        {
            final Set pluginDepRels = new HashSet();
            for ( ProjectRelationship rel : relationships )
            {
                rel = adjustDeclaring( rel );
                switch ( rel.getType() )
                {
                    case DEPENDENCY:
                    {
                        final DependencyRelationship dr = (DependencyRelationship) rel;
                        withDependencies( dr );

                        break;
                    }
                    case PLUGIN:
                    {
                        final PluginRelationship pr = (PluginRelationship) rel;
                        withPlugins( pr );

                        break;
                    }
                    case EXTENSION:
                    {
                        withExtensions( (ExtensionRelationship) rel );
                        break;
                    }
                    case PLUGIN_DEP:
                    {
                        // load all plugin relationships first.
                        pluginDepRels.add( (PluginDependencyRelationship) rel );
                        break;
                    }
                    case PARENT:
                    {
                        withParent( (ParentRelationship) rel );
                        break;
                    }
                }
            }

            withPluginDependencies( pluginDepRels );

            return this;
        }

        public int getNextPluginIndex( final boolean managed )
        {
            return managed ? managedPlugins.size() : plugins.size();
        }

        public int getNextPluginDependencyIndex( final ProjectVersionRef plugin, final boolean managed )
        {
            final List list =
                pluginDependencies.get( new PluginRelationship( key.getProject(), plugin, 0, managed ) );
            return list == null ? 0 : list.size();
        }

        public int getNextDependencyIndex( final boolean managed )
        {
            return managed ? managedDependencies.size() : dependencies.size();
        }

        public int getNextExtensionIndex()
        {
            return extensions.size();
        }

        public Builder withDependency( final ProjectVersionRef ref, final String type, final String classifier,
                                       final DependencyScope scope, final boolean managed )
        {
            withDependencies( new DependencyRelationship( key.getProject(), new ArtifactRef( ref, type, classifier,
                                                                                             false ), scope,
                                                          getNextDependencyIndex( managed ), managed ) );

            return this;
        }

        public Builder withPlugin( final ProjectVersionRef ref, final boolean managed )
        {
            withPlugins( new PluginRelationship( key.getProject(), ref, getNextPluginIndex( managed ), managed ) );

            return this;
        }

        public Builder withExtension( final ProjectVersionRef ref )
        {
            withExtensions( new ExtensionRelationship( key.getProject(), ref, getNextExtensionIndex() ) );
            return this;
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy