001    /**
002     *
003     * Copyright 2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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: 430508 $ $Date: 2006-08-10 12:56:47 -0700 (Thu, 10 Aug 2006) $
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);
036        }
037    
038        public Artifact(String groupId, String artifactId, Version version, String type) {
039            if (artifactId == null) throw new NullPointerException("artifactId is null");
040            this.groupId = groupId;
041            this.artifactId = artifactId;
042            this.version = version;
043            this.type = type;
044        }
045    
046        public static Artifact create(String id) {
047            String[] parts = id.split("/", -1);
048            if (parts.length != 4) {
049                throw new IllegalArgumentException("Invalid id: " + id);
050            }
051            for (int i = 0; i < parts.length; i++) {
052                if (parts[i].equals("")) {
053                    parts[i] = null;
054                }
055            }
056            return new Artifact(parts[0], parts[1], parts[2], parts[3]);
057        }
058    
059        public String getGroupId() {
060            return groupId;
061        }
062    
063        public String getArtifactId() {
064            return artifactId;
065        }
066    
067        public Version getVersion() {
068            return version;
069        }
070    
071        public String getType() {
072            return type;
073        }
074    
075        public boolean isResolved() {
076            return groupId != null && artifactId != null && version != null && type != null;
077        }
078    
079        public int compareTo(Object object) {
080            Artifact artifact = (Artifact) object;
081    
082            int i = safeCompare(groupId, artifact.groupId);
083            if (i != 0) return i;
084    
085            i = safeCompare(artifactId, artifact.artifactId);
086            if (i != 0) return i;
087    
088            i = safeCompare(version, artifact.version);
089            if (i != 0) return i;
090    
091            i = safeCompare(type, artifact.type);
092            return i;
093        }
094    
095        private static int GREATER = 1;
096        private static int LESS = -1;
097    
098        private static int safeCompare(Comparable left, Comparable right) {
099            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    }