1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.repository.metadata;
20
21 import org.apache.maven.artifact.Artifact;
22 import org.apache.maven.artifact.ArtifactScopeEnum;
23
24 /**
25 * metadata [dirty] Tree
26 *
27 * @author <a href="oleg@codehaus.org">Oleg Gusakov</a>
28 *
29 */
30 public class MetadataTreeNode {
31 ArtifactMetadata md; // this node
32
33 MetadataTreeNode parent; // papa
34
35 /** default # of children. Used for tree creation optimization only */
36 int nChildren = 8;
37
38 MetadataTreeNode[] children; // of cause
39
40 public int getNChildren() {
41 return nChildren;
42 }
43
44 public void setNChildren(int children) {
45 nChildren = children;
46 }
47
48 // ------------------------------------------------------------------------
49 public MetadataTreeNode() {}
50 // ------------------------------------------------------------------------
51 public MetadataTreeNode(ArtifactMetadata md, MetadataTreeNode parent, boolean resolved, ArtifactScopeEnum scope) {
52 if (md != null) {
53 md.setArtifactScope(ArtifactScopeEnum.checkScope(scope));
54 md.setResolved(resolved);
55 }
56
57 this.md = md;
58 this.parent = parent;
59 }
60 // ------------------------------------------------------------------------
61 public MetadataTreeNode(Artifact af, MetadataTreeNode parent, boolean resolved, ArtifactScopeEnum scope) {
62 this(new ArtifactMetadata(af), parent, resolved, scope);
63 }
64
65 // ------------------------------------------------------------------------
66 public void addChild(int index, MetadataTreeNode kid) {
67 if (kid == null) {
68 return;
69 }
70
71 if (children == null) {
72 children = new MetadataTreeNode[nChildren];
73 }
74
75 children[index % nChildren] = kid;
76 }
77
78 // ------------------------------------------------------------------
79 @Override
80 public String toString() {
81 return md == null ? "no metadata" : md.toString();
82 }
83
84 // ------------------------------------------------------------------
85 public String graphHash() throws MetadataResolutionException {
86 if (md == null) {
87 throw new MetadataResolutionException(
88 "treenode without metadata, parent: " + (parent == null ? "null" : parent.toString()));
89 }
90
91 return md.groupId + ":" + md.artifactId;
92 }
93
94 // ------------------------------------------------------------------------
95 public boolean hasChildren() {
96 return children != null;
97 }
98 // ------------------------------------------------------------------------
99 public ArtifactMetadata getMd() {
100 return md;
101 }
102
103 public void setMd(ArtifactMetadata md) {
104 this.md = md;
105 }
106
107 public MetadataTreeNode getParent() {
108 return parent;
109 }
110
111 public void setParent(MetadataTreeNode parent) {
112 this.parent = parent;
113 }
114
115 public MetadataTreeNode[] getChildren() {
116 return children;
117 }
118
119 public void setChildren(MetadataTreeNode[] children) {
120 this.children = children;
121 }
122 // ------------------------------------------------------------------------
123 // ------------------------------------------------------------------------
124
125 }