001package org.apache.maven.repository.metadata;
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 java.util.ArrayList;
023import java.util.Iterator;
024import java.util.List;
025
026import org.apache.maven.artifact.ArtifactScopeEnum;
027
028/**
029 * classpath container that is aware of the classpath scope
030 *
031 * @author <a href="oleg@codehaus.org">Oleg Gusakov</a>
032 *
033 */
034public class ClasspathContainer
035implements Iterable<ArtifactMetadata>
036{
037    private List<ArtifactMetadata> classpath;
038
039    private ArtifactScopeEnum scope;
040
041    // -------------------------------------------------------------------------------------------
042    public ClasspathContainer( ArtifactScopeEnum scope )
043    {
044        this.scope = ArtifactScopeEnum.checkScope( scope );
045    }
046
047    // -------------------------------------------------------------------------------------------
048    public ClasspathContainer( List<ArtifactMetadata> classpath, ArtifactScopeEnum scope )
049    {
050        this( scope );
051        this.classpath = classpath;
052    }
053
054    // -------------------------------------------------------------------------------------------
055    public Iterator<ArtifactMetadata> iterator()
056    {
057        return classpath == null ? null : classpath.iterator();
058    }
059
060    // -------------------------------------------------------------------------------------------
061    public ClasspathContainer add( ArtifactMetadata md )
062    {
063        if ( classpath == null )
064        {
065            classpath = new ArrayList<ArtifactMetadata>( 16 );
066        }
067
068        classpath.add( md );
069
070        return this;
071    }
072
073    // -------------------------------------------------------------------------------------------
074    public List<ArtifactMetadata> getClasspath()
075    {
076        return classpath;
077    }
078
079    // -------------------------------------------------------------------------------------------
080    public MetadataTreeNode getClasspathAsTree()
081        throws MetadataResolutionException
082    {
083        if ( classpath == null || classpath.size() < 1 )
084        {
085            return null;
086        }
087
088        MetadataTreeNode tree = null;
089        MetadataTreeNode parent = null;
090        MetadataTreeNode node = null;
091
092        for ( ArtifactMetadata md : classpath )
093        {
094            node = new MetadataTreeNode( md, parent, md.isResolved(), md.getArtifactScope() );
095            if ( tree == null )
096            {
097                tree = node;
098            }
099
100            if ( parent != null )
101            {
102                parent.setNChildren( 1 );
103                parent.addChild( 0, node );
104            }
105
106            parent = node;
107
108        }
109        return tree;
110    }
111
112    public void setClasspath( List<ArtifactMetadata> classpath )
113    {
114        this.classpath = classpath;
115    }
116
117    public ArtifactScopeEnum getScope()
118    {
119        return scope;
120    }
121
122    public void setScope( ArtifactScopeEnum scope )
123    {
124        this.scope = scope;
125    }
126
127    // -------------------------------------------------------------------------------------------
128    @Override
129    public String toString()
130    {
131        StringBuilder sb = new StringBuilder( 256 );
132        sb.append( "[scope=" + scope.getScope() );
133        if ( classpath != null )
134        {
135            for ( ArtifactMetadata md : classpath )
136            {
137                sb.append( ": " + md.toString() + '{' + md.getArtifactUri() + '}' );
138            }
139        }
140        sb.append( ']' );
141        return sb.toString();
142    }
143    // -------------------------------------------------------------------------------------------
144    // -------------------------------------------------------------------------------------------
145}