KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > maven > ModuleName


1 package hudson.maven;
2
3 import org.apache.maven.project.MavenProject;
4 import org.apache.maven.model.Plugin;
5 import org.apache.maven.model.Extension;
6 import org.apache.maven.model.Dependency;
7 import org.apache.maven.model.ReportPlugin;
8
9 import java.io.Serializable JavaDoc;
10
11 /**
12  * Version independent name of a Maven project.
13  *
14  * @author Kohsuke Kawaguchi
15  */

16 public class ModuleName implements Comparable JavaDoc<ModuleName>, Serializable JavaDoc {
17     public final String JavaDoc groupId;
18     public final String JavaDoc artifactId;
19
20     public ModuleName(String JavaDoc groupId, String JavaDoc artifactId) {
21         this.groupId = groupId;
22         this.artifactId = artifactId;
23     }
24
25     public ModuleName(MavenProject project) {
26         this(project.getGroupId(),project.getArtifactId());
27     }
28
29     public ModuleName(Plugin plugin) {
30         this(plugin.getGroupId(),plugin.getArtifactId());
31     }
32
33     public ModuleName(ReportPlugin plugin) {
34         this(plugin.getGroupId(),plugin.getArtifactId());
35     }
36
37     public ModuleName(Extension ext) {
38         this(ext.getGroupId(),ext.getArtifactId());
39     }
40
41     public ModuleName(Dependency dep) {
42         this(dep.getGroupId(),dep.getArtifactId());
43     }
44
45     /**
46      * Returns the "groupId:artifactId" form.
47      */

48     public String JavaDoc toString() {
49         return groupId+':'+artifactId;
50     }
51
52     /**
53      * Returns the "groupId$artifactId" form,
54      * which is safe for the use as a file name, unlike {@link #toString()}.
55      */

56     public String JavaDoc toFileSystemName() {
57         return groupId+'$'+artifactId;
58     }
59
60     public static ModuleName fromFileSystemName(String JavaDoc n) {
61         int idx = n.indexOf('$');
62         if(idx<0) throw new IllegalArgumentException JavaDoc(n);
63         return new ModuleName(n.substring(0,idx),n.substring(idx+1));
64     }
65
66     public static ModuleName fromString(String JavaDoc n) {
67         int idx = n.indexOf(':');
68         if(idx<0) throw new IllegalArgumentException JavaDoc(n);
69         return new ModuleName(n.substring(0,idx),n.substring(idx+1));
70     }
71
72     /**
73      * Checks if the given name is valid module name string format
74      * created by {@link #toString()}.
75      */

76     public static boolean isValid(String JavaDoc n) {
77         return n.indexOf(':')>0;
78     }
79
80     public boolean equals(Object JavaDoc o) {
81         if (this == o) return true;
82         if (o == null || getClass() != o.getClass()) return false;
83
84         ModuleName that = (ModuleName) o;
85
86         return artifactId.equals(that.artifactId)
87             && groupId.equals(that.groupId);
88
89     }
90
91     public int hashCode() {
92         int result;
93         result = groupId.hashCode();
94         result = 31 * result + artifactId.hashCode();
95         return result;
96     }
97
98     public int compareTo(ModuleName that) {
99         int r = this.groupId.compareTo(that.groupId);
100         if(r!=0) return r;
101         return this.artifactId.compareTo(that.artifactId);
102     }
103
104     private static final long serialVersionUID = 1L;
105 }
106
Popular Tags