KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > repository > Artifact


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.kernel.repository;
19
20 import java.io.Serializable JavaDoc;
21
22 /**
23  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
24  */

25 public class Artifact implements Comparable JavaDoc, Serializable JavaDoc {
26     private static final long serialVersionUID = -3459638899709893444L;
27     public static final String JavaDoc DEFAULT_GROUP_ID = "default";
28
29     private final String JavaDoc groupId;
30     private final String JavaDoc artifactId;
31     private final Version version;
32     private final String JavaDoc type;
33
34     public Artifact(String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version, String JavaDoc type) {
35         this(groupId, artifactId, version == null ? null : new Version(version), type);
36     }
37
38     public Artifact(String JavaDoc groupId, String JavaDoc artifactId, Version version, String JavaDoc type) {
39         if (artifactId == null) throw new NullPointerException JavaDoc("artifactId is null");
40         this.groupId = groupId;
41         this.artifactId = artifactId;
42         this.version = version;
43         this.type = type;
44     }
45
46     public static Artifact create(String JavaDoc id) {
47         String JavaDoc[] parts = id.split("/", -1);
48         if (parts.length != 4) {
49             throw new IllegalArgumentException JavaDoc("Invalid id: " + id);
50         }
51         for (int i = 0; i < parts.length; i++) {
52             if (parts[i].equals("")) {
53                 parts[i] = null;
54             }
55         }
56         return new Artifact(parts[0], parts[1], parts[2], parts[3]);
57     }
58
59     public String JavaDoc getGroupId() {
60         return groupId;
61     }
62
63     public String JavaDoc getArtifactId() {
64         return artifactId;
65     }
66
67     public Version getVersion() {
68         return version;
69     }
70
71     public String JavaDoc getType() {
72         return type;
73     }
74
75     public boolean isResolved() {
76         return groupId != null && artifactId != null && version != null && type != null;
77     }
78
79     public int compareTo(Object JavaDoc object) {
80         Artifact artifact = (Artifact) object;
81
82         int i = safeCompare(groupId, artifact.groupId);
83         if (i != 0) return i;
84
85         i = safeCompare(artifactId, artifact.artifactId);
86         if (i != 0) return i;
87
88         i = safeCompare(version, artifact.version);
89         if (i != 0) return i;
90
91         i = safeCompare(type, artifact.type);
92         return i;
93     }
94
95     private static int GREATER = 1;
96     private static int LESS = -1;
97
98     private static int safeCompare(Comparable JavaDoc left, Comparable JavaDoc right) {
99         if (left == null) {
100             if (right != null) return LESS;
101             return 0;
102         }
103         if (right == null) return GREATER;
104         return left.compareTo(right);
105     }
106
107     public boolean equals(Object JavaDoc o) {
108         if (this == o) return true;
109         if (o == null || getClass() != o.getClass()) return false;
110
111         final Artifact artifact = (Artifact) o;
112
113         if (!artifactId.equals(artifact.artifactId)) {
114             return false;
115         }
116
117         if (groupId != null ? !groupId.equals(artifact.groupId) : artifact.groupId != null) {
118             return false;
119         }
120
121         if (type != null ? !type.equals(artifact.type) : artifact.type != null) {
122             return false;
123         }
124
125         return !(version != null ? !version.equals(artifact.version) : artifact.version != null);
126
127     }
128
129     public int hashCode() {
130         int result;
131         result = (groupId != null ? groupId.hashCode() : 0);
132         result = 29 * result + artifactId.hashCode();
133         result = 29 * result + (version != null ? version.hashCode() : 0);
134         result = 29 * result + (type != null ? type.hashCode() : 0);
135         return result;
136     }
137
138     public String JavaDoc toString() {
139         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
140
141         if (groupId != null) {
142             buffer.append(groupId);
143         }
144         buffer.append("/");
145
146         buffer.append(artifactId);
147         buffer.append("/");
148
149         if (version != null) {
150             buffer.append(version);
151         }
152         buffer.append("/");
153
154         if (type != null) {
155             buffer.append(type);
156         }
157         return buffer.toString();
158     }
159
160     /**
161      * see if this artifact matches the other artifact (which is more specific than this one)
162      *
163      * @param otherArtifact the more specific artifact we are comparing with
164      * @return whether the other artifact is consistent with everything specified in this artifact.
165      */

166     public boolean matches(Artifact otherArtifact) {
167         if (groupId != null && !groupId.equals(otherArtifact.groupId)) {
168             return false;
169         }
170         if (artifactId != null && !artifactId.equals(otherArtifact.artifactId)) {
171             return false;
172         }
173         if (version != null && !version.equals(otherArtifact.version)) {
174             return false;
175         }
176         return (type == null || type.equals(otherArtifact.type));
177     }
178 }
179
Popular Tags