001package org.apache.maven.lifecycle.internal;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.project.MavenProject;
023
024import java.util.Arrays;
025import java.util.Collection;
026import java.util.Collections;
027import java.util.TreeSet;
028
029/**
030 * Context of dependency artifacts for a particular project.
031 * 
032 * @since 3.0
033 * @author Benjamin Bentmann
034 * @author Kristian Rosenvold (class extract only)
035 *         <p/>
036 *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
037 */
038// TODO: From a concurrency perspective, this class is not good. The combination of mutable/immutable state is not nice
039public class DependencyContext
040{
041
042    private static final Collection<?> UNRESOLVED = Arrays.asList();
043
044    private final MavenProject project;
045
046    private final Collection<String> scopesToCollectForCurrentProject;
047
048    private final Collection<String> scopesToResolveForCurrentProject;
049
050    private final Collection<String> scopesToCollectForAggregatedProjects;
051
052    private final Collection<String> scopesToResolveForAggregatedProjects;
053
054    private volatile Collection<?> lastDependencyArtifacts = UNRESOLVED;
055
056    private volatile int lastDependencyArtifactCount = -1;
057
058    public DependencyContext( MavenProject project, Collection<String> scopesToCollect,
059                              Collection<String> scopesToResolve )
060    {
061        this.project = project;
062        scopesToCollectForCurrentProject = scopesToCollect;
063        scopesToResolveForCurrentProject = scopesToResolve;
064        scopesToCollectForAggregatedProjects = Collections.synchronizedSet( new TreeSet<String>() );
065        scopesToResolveForAggregatedProjects = Collections.synchronizedSet( new TreeSet<String>() );
066    }
067
068    public MavenProject getProject()
069    {
070        return project;
071    }
072
073    public Collection<String> getScopesToCollectForCurrentProject()
074    {
075        return scopesToCollectForCurrentProject;
076    }
077
078    public Collection<String> getScopesToResolveForCurrentProject()
079    {
080        return scopesToResolveForCurrentProject;
081    }
082
083    public Collection<String> getScopesToCollectForAggregatedProjects()
084    {
085        return scopesToCollectForAggregatedProjects;
086    }
087
088    public Collection<String> getScopesToResolveForAggregatedProjects()
089    {
090        return scopesToResolveForAggregatedProjects;
091    }
092
093    public boolean isResolutionRequiredForCurrentProject()
094    {
095        return lastDependencyArtifacts != project.getDependencyArtifacts() || ( lastDependencyArtifacts != null
096            && lastDependencyArtifactCount != lastDependencyArtifacts.size() );
097    }
098
099    public boolean isResolutionRequiredForAggregatedProjects( Collection<String> scopesToCollect,
100                                                              Collection<String> scopesToResolve )
101    {
102        boolean required =
103            scopesToCollectForAggregatedProjects.addAll( scopesToCollect )
104                || scopesToResolveForAggregatedProjects.addAll( scopesToResolve );
105        return required;
106    }
107
108    public void synchronizeWithProjectState()
109    {
110        lastDependencyArtifacts = project.getDependencyArtifacts();
111        lastDependencyArtifactCount = ( lastDependencyArtifacts != null ) ? lastDependencyArtifacts.size() : 0;
112    }
113
114}