KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > savant > Dependencies


1 /*
2  * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.savant;
8
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17
18
19 /**
20  * <p>
21  * This class is the object that stores all the information
22  * about dependencies. Usually these objects are scoped to a
23  * single project.
24  * </p>
25  *
26  * @author Brian Pontarelli
27  */

28 public class Dependencies {
29     private Map JavaDoc projects = new HashMap JavaDoc();
30     private List JavaDoc artifactGroups = new ArrayList JavaDoc();
31     private List JavaDoc artifacts = new ArrayList JavaDoc();
32
33
34
35     /**
36      * Fetches the local project for the given ID.
37      *
38      * @param id The id of the project to lookup
39      */

40     public LocalProject getProject(String JavaDoc id) {
41         return (LocalProject) projects.get(id);
42     }
43
44     /**
45      * Adds the given project to the list of local projects. These might be built
46      * during the course of mediation if artifacts are produced from these projects.
47      * This also calls the {@link LocalProject#validate()} method.
48      *
49      * @param project The project to add
50      */

51     public void addProject(LocalProject project) throws SavantException {
52         project.validate();
53         projects.put(project.getID(), project);
54     }
55
56     /**
57      * Removes the given project to the list of local projects.
58      *
59      * @param project The project to remove
60      */

61     public void removeProject(LocalProject project) {
62         projects.remove(project.getID());
63     }
64
65     /**
66      * Adds the given artifact group to the list of artifact groups to mediate
67      * over. This group is validated by calling the {@link ArtifactGroup#validate()}
68      * method.
69      *
70      * @param group The group to add
71      */

72     public void addArtifactGroup(ArtifactGroup group) throws SavantException {
73         group.validate();
74         this.artifactGroups.add(group);
75     }
76
77     /**
78      * Removes the given artifact group to the list of artifact groups in this
79      * dependency.
80      *
81      * @param group The group to remove
82      */

83     public void removeArtifactGroup(ArtifactGroup group) {
84         this.artifactGroups.remove(group);
85     }
86
87     /**
88      * Adds the given artifact to the list of artifact to mediate over. This
89      * artifact is validated by calling the {@link Artifact#validate()}
90      * method.
91      *
92      * @param artifact The artifact to add
93      */

94     public void addArtifact(Artifact artifact) throws SavantException {
95         artifact.validate();
96         this.artifacts.add(artifact);
97     }
98
99     /**
100      * Removes the given artifact to the list of artifact in this dependency.
101      *
102      * @param artifact The artifact to remove
103      */

104     public void removeArtifact(Artifact artifact) {
105         this.artifacts.add(artifact);
106     }
107
108     /**
109      * Returns the artifact group list. This is list immutable.
110      */

111     public List JavaDoc getArtifactGroups() {
112         return Collections.unmodifiableList(artifactGroups);
113     }
114
115     /**
116      * Returns the artifact list. This List is immutable.
117      */

118     public List JavaDoc getArtifacts() {
119         return artifacts;
120     }
121
122     /**
123      * Returns the Set of artifacts contained here or in ArtifactGroups within.
124      * This never returns null.
125      */

126     public Set JavaDoc getAllArtifacts() {
127         Set JavaDoc set = new HashSet JavaDoc();
128         for (int i = 0; i < artifactGroups.size(); i++) {
129             ArtifactGroup group = (ArtifactGroup) artifactGroups.get(i);
130             addGroup(set, group);
131         }
132
133         set.addAll(artifacts);
134
135         return set;
136     }
137
138     /**
139      * Recursive method that adds the artifacts from an ArtifacGroup and recurses
140      * into each sub-group.
141      *
142      * @param set The Set to store the artifacts in.
143      * @param group The group to add the artifacts from.
144      */

145     protected void addGroup(Set JavaDoc set, ArtifactGroup group) {
146         set.addAll(group.getArtifacts());
147
148         List JavaDoc groups = group.getArtifactGroups();
149         for (int i = 0; i < groups.size(); i++) {
150             addGroup(set, (ArtifactGroup) groups.get(i));
151
152         }
153     }
154 }
Popular Tags