001    package 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    
022    import org.apache.maven.artifact.Artifact;
023    import org.apache.maven.artifact.ArtifactScopeEnum;
024    
025    /**
026     * metadata [dirty] Tree
027     *
028     * @author <a href="oleg@codehaus.org">Oleg Gusakov</a>
029     *
030     */
031    public class MetadataTreeNode
032    {
033        ArtifactMetadata md; // this node
034    
035        MetadataTreeNode parent; // papa
036    
037        /** default # of children. Used for tree creation optimization only */
038        int nChildren = 8;
039    
040        MetadataTreeNode[] children; // of cause
041    
042        public int getNChildren()
043        {
044            return nChildren;
045        }
046    
047        public void setNChildren( int children )
048        {
049            nChildren = children;
050        }
051    
052        //------------------------------------------------------------------------
053        public MetadataTreeNode()
054        {
055        }
056        //------------------------------------------------------------------------
057        public MetadataTreeNode( ArtifactMetadata md, MetadataTreeNode parent, boolean resolved, ArtifactScopeEnum scope )
058        {
059            if ( md != null )
060            {
061                md.setArtifactScope( ArtifactScopeEnum.checkScope( scope ) );
062                md.setResolved( resolved );
063            }
064    
065            this.md = md;
066            this.parent = parent;
067        }
068        //------------------------------------------------------------------------
069        public MetadataTreeNode( Artifact af, MetadataTreeNode parent, boolean resolved, ArtifactScopeEnum scope )
070        {
071            this( new ArtifactMetadata( af ), parent, resolved, scope );
072        }
073    
074        // ------------------------------------------------------------------------
075        public void addChild( int index, MetadataTreeNode kid )
076        {
077            if ( kid == null )
078            {
079                return;
080            }
081    
082            if ( children == null )
083            {
084                children = new MetadataTreeNode[nChildren];
085            }
086    
087            children[index % nChildren] = kid;
088        }
089    
090        //------------------------------------------------------------------
091        @Override
092        public String toString()
093        {
094            return md == null ? "no metadata" : md.toString();
095        }
096    
097        //------------------------------------------------------------------
098        public String graphHash()
099            throws MetadataResolutionException
100        {
101            if ( md == null )
102            {
103                throw new MetadataResolutionException( "treenode without metadata, parent: "
104                    + ( parent == null ? "null" : parent.toString() ) );
105            }
106    
107            return md.groupId + ":" + md.artifactId;
108        }
109    
110        //------------------------------------------------------------------------
111        public boolean hasChildren()
112        {
113            return children != null;
114        }
115        //------------------------------------------------------------------------
116        public ArtifactMetadata getMd()
117        {
118            return md;
119        }
120    
121        public void setMd( ArtifactMetadata md )
122        {
123            this.md = md;
124        }
125    
126        public MetadataTreeNode getParent()
127        {
128            return parent;
129        }
130    
131        public void setParent( MetadataTreeNode parent )
132        {
133            this.parent = parent;
134        }
135    
136        public MetadataTreeNode[] getChildren()
137        {
138            return children;
139        }
140    
141        public void setChildren( MetadataTreeNode[] children )
142        {
143            this.children = children;
144        }
145        //------------------------------------------------------------------------
146        //------------------------------------------------------------------------
147    
148    }