View Javadoc

1   /**
2    *
3    * Copyright 2006 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
21  
22  /**
23   * @version $Rev: 430508 $ $Date: 2006-08-10 12:56:47 -0700 (Thu, 10 Aug 2006) $
24   */
25  public class Artifact implements Comparable, Serializable {
26      private static final long serialVersionUID = -3459638899709893444L;
27      public static final String DEFAULT_GROUP_ID = "default";
28  
29      private final String groupId;
30      private final String artifactId;
31      private final Version version;
32      private final String type;
33  
34      public Artifact(String groupId, String artifactId, String version, String type) {
35          this(groupId, artifactId, version == null ? null : new Version(version), type);
36      }
37  
38      public Artifact(String groupId, String artifactId, Version version, String type) {
39          if (artifactId == null) throw new NullPointerException("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 id) {
47          String[] parts = id.split("/", -1);
48          if (parts.length != 4) {
49              throw new IllegalArgumentException("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 getGroupId() {
60          return groupId;
61      }
62  
63      public String getArtifactId() {
64          return artifactId;
65      }
66  
67      public Version getVersion() {
68          return version;
69      }
70  
71      public String 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 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 left, Comparable 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 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 toString() {
139         StringBuffer buffer = new StringBuffer();
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 }