001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.kernel.repository;
019
020 import java.io.Serializable;
021
022 /**
023 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
024 */
025 public class Artifact implements Comparable, Serializable {
026 private static final long serialVersionUID = -3459638899709893444L;
027 public static final String DEFAULT_GROUP_ID = "default";
028
029 private final String groupId;
030 private final String artifactId;
031 private final Version version;
032 private final String type;
033
034 public Artifact(String groupId, String artifactId, String version, String type) {
035 this(groupId, artifactId, version == null ? null : new Version(version), type, true);
036 }
037
038 public Artifact(String groupId, String artifactId, Version version, String type) {
039 this(groupId, artifactId, version, type, true);
040 }
041
042 private Artifact(String groupId, String artifactId, Version version, String type, boolean requireArtifactId) {
043 if (requireArtifactId && artifactId == null) throw new NullPointerException("artifactId is null: groupId: " + groupId + ", version: " + version + ", type: " + type);
044 this.groupId = groupId;
045 this.artifactId = artifactId;
046 this.version = version;
047 this.type = type;
048 }
049
050 public static Artifact create(String id) {
051 return create(id, true);
052 }
053
054 public static Artifact createPartial(String id) {
055 return create(id, false);
056 }
057
058 private static Artifact create(String id, boolean requireArtifactId) {
059 String[] parts = id.split("/", -1);
060 if (parts.length != 4) {
061 throw new IllegalArgumentException("id must be in the form [groupId]/[artifactId]/[version]/[type] : " + id);
062 }
063 for (int i = 0; i < parts.length; i++) {
064 if (parts[i].equals("")) {
065 parts[i] = null;
066 }
067 }
068 return new Artifact(parts[0], parts[1], parts[2] == null ? null : new Version(parts[2]), parts[3], requireArtifactId);
069 }
070
071 public String getGroupId() {
072 return groupId;
073 }
074
075 public String getArtifactId() {
076 return artifactId;
077 }
078
079 public Version getVersion() {
080 return version;
081 }
082
083 public String getType() {
084 return type;
085 }
086
087 public boolean isResolved() {
088 return groupId != null && artifactId != null && version != null && type != null;
089 }
090
091 public int compareTo(Object object) {
092 Artifact artifact = (Artifact) object;
093
094 int i = safeCompare(groupId, artifact.groupId);
095 if (i != 0) return i;
096
097 i = safeCompare(artifactId, artifact.artifactId);
098 if (i != 0) return i;
099
100 i = safeCompare(version, artifact.version);
101 if (i != 0) return i;
102
103 i = safeCompare(type, artifact.type);
104 return i;
105 }
106
107 private static int GREATER = 1;
108 private static int LESS = -1;
109
110 private static <T extends Comparable<T>> int safeCompare(T left, T right) {
111 if (left == null) {
112 if (right != null) return LESS;
113 return 0;
114 }
115 if (right == null) return GREATER;
116 return left.compareTo(right);
117 }
118
119 public boolean equals(Object o) {
120 if (this == o) return true;
121 if (o == null || getClass() != o.getClass()) return false;
122
123 final Artifact artifact = (Artifact) o;
124
125 if (artifactId != null? !artifactId.equals(artifact.artifactId) : artifact.artifactId != null) {
126 return false;
127 }
128
129 if (groupId != null ? !groupId.equals(artifact.groupId) : artifact.groupId != null) {
130 return false;
131 }
132
133 if (type != null ? !type.equals(artifact.type) : artifact.type != null) {
134 return false;
135 }
136
137 return !(version != null ? !version.equals(artifact.version) : artifact.version != null);
138
139 }
140
141 public int hashCode() {
142 int result;
143 result = (groupId != null ? groupId.hashCode() : 0);
144 result = 29 * result + (artifactId != null? artifactId.hashCode() : 0);
145 result = 29 * result + (version != null ? version.hashCode() : 0);
146 result = 29 * result + (type != null ? type.hashCode() : 0);
147 return result;
148 }
149
150 public String toString() {
151 StringBuffer buffer = new StringBuffer();
152
153 if (groupId != null) {
154 buffer.append(groupId);
155 }
156 buffer.append("/");
157
158 if (artifactId != null) {
159 buffer.append(artifactId);
160 }
161 buffer.append("/");
162
163 if (version != null) {
164 buffer.append(version);
165 }
166 buffer.append("/");
167
168 if (type != null) {
169 buffer.append(type);
170 }
171 return buffer.toString();
172 }
173
174 /**
175 * see if this artifact matches the other artifact (which is more specific than this one)
176 *
177 * @param otherArtifact the more specific artifact we are comparing with
178 * @return whether the other artifact is consistent with everything specified in this artifact.
179 */
180 public boolean matches(Artifact otherArtifact) {
181 if (groupId != null && !groupId.equals(otherArtifact.groupId)) {
182 return false;
183 }
184 if (artifactId != null && !artifactId.equals(otherArtifact.artifactId)) {
185 return false;
186 }
187 if (version != null && !version.equals(otherArtifact.version)) {
188 return false;
189 }
190 return (type == null || type.equals(otherArtifact.type));
191 }
192 }