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; 21 22 /** 23 * @version $Rev: 674386 $ $Date: 2008-07-06 19:50:41 -0700 (Sun, 06 Jul 2008) $ 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, true); 36 } 37 38 public Artifact(String groupId, String artifactId, Version version, String type) { 39 this(groupId, artifactId, version, type, true); 40 } 41 42 private Artifact(String groupId, String artifactId, Version version, String type, boolean requireArtifactId) { 43 if (requireArtifactId && artifactId == null) throw new NullPointerException("artifactId is null: groupId: " + groupId + ", version: " + version + ", type: " + type); 44 this.groupId = groupId; 45 this.artifactId = artifactId; 46 this.version = version; 47 this.type = type; 48 } 49 50 public static Artifact create(String id) { 51 return create(id, true); 52 } 53 54 public static Artifact createPartial(String id) { 55 return create(id, false); 56 } 57 58 private static Artifact create(String id, boolean requireArtifactId) { 59 String[] parts = id.split("/", -1); 60 if (parts.length != 4) { 61 throw new IllegalArgumentException("id must be in the form [groupId]/[artifactId]/[version]/[type] : " + id); 62 } 63 for (int i = 0; i < parts.length; i++) { 64 if (parts[i].equals("")) { 65 parts[i] = null; 66 } 67 } 68 return new Artifact(parts[0], parts[1], parts[2] == null ? null : new Version(parts[2]), parts[3], requireArtifactId); 69 } 70 71 public String getGroupId() { 72 return groupId; 73 } 74 75 public String getArtifactId() { 76 return artifactId; 77 } 78 79 public Version getVersion() { 80 return version; 81 } 82 83 public String getType() { 84 return type; 85 } 86 87 public boolean isResolved() { 88 return groupId != null && artifactId != null && version != null && type != null; 89 } 90 91 public int compareTo(Object object) { 92 Artifact artifact = (Artifact) object; 93 94 int i = safeCompare(groupId, artifact.groupId); 95 if (i != 0) return i; 96 97 i = safeCompare(artifactId, artifact.artifactId); 98 if (i != 0) return i; 99 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 }