001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.artifact;
020
021import java.io.File;
022import java.nio.file.Path;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.Map;
026import java.util.Objects;
027import java.util.regex.Matcher;
028import java.util.regex.Pattern;
029
030/**
031 * A skeleton class for artifacts.
032 */
033public abstract class AbstractArtifact implements Artifact {
034
035    private static final String SNAPSHOT = "SNAPSHOT";
036
037    private static final Pattern SNAPSHOT_TIMESTAMP = Pattern.compile("^(.*-)?([0-9]{8}\\.[0-9]{6}-[0-9]+)$");
038
039    @Override
040    public boolean isSnapshot() {
041        return isSnapshot(getVersion());
042    }
043
044    private static boolean isSnapshot(String version) {
045        return version.endsWith(SNAPSHOT) || SNAPSHOT_TIMESTAMP.matcher(version).matches();
046    }
047
048    @Override
049    public String getBaseVersion() {
050        return toBaseVersion(getVersion());
051    }
052
053    private static String toBaseVersion(String version) {
054        String baseVersion;
055
056        if (version == null) {
057            baseVersion = null;
058        } else if (version.startsWith("[") || version.startsWith("(")) {
059            baseVersion = version;
060        } else {
061            Matcher m = SNAPSHOT_TIMESTAMP.matcher(version);
062            if (m.matches()) {
063                if (m.group(1) != null) {
064                    baseVersion = m.group(1) + SNAPSHOT;
065                } else {
066                    baseVersion = SNAPSHOT;
067                }
068            } else {
069                baseVersion = version;
070            }
071        }
072
073        return baseVersion;
074    }
075
076    /**
077     * Creates a new artifact with the specified coordinates, properties and file.
078     *
079     * @param version The version of the artifact, may be {@code null}.
080     * @param properties The properties of the artifact, may be {@code null} if none. The method may assume immutability
081     *            of the supplied map, i.e. need not copy it.
082     * @param path The resolved file of the artifact, may be {@code null}.
083     * @return The new artifact instance, never {@code null}.
084     */
085    private Artifact newInstance(String version, Map<String, String> properties, Path path) {
086        return new DefaultArtifact(
087                getGroupId(), getArtifactId(), getClassifier(), getExtension(), version, path, properties);
088    }
089
090    @Override
091    public Artifact setVersion(String version) {
092        String current = getVersion();
093        if (current.equals(version) || (version == null && current.isEmpty())) {
094            return this;
095        }
096        return newInstance(version, getProperties(), getPath());
097    }
098
099    @Deprecated
100    @Override
101    public Artifact setFile(File file) {
102        return setPath(file != null ? file.toPath() : null);
103    }
104
105    @Override
106    public Artifact setPath(Path path) {
107        Path current = getPath();
108        if (Objects.equals(current, path)) {
109            return this;
110        }
111        return newInstance(getVersion(), getProperties(), path);
112    }
113
114    public Artifact setProperties(Map<String, String> properties) {
115        Map<String, String> current = getProperties();
116        if (current.equals(properties) || (properties == null && current.isEmpty())) {
117            return this;
118        }
119        return newInstance(getVersion(), copyProperties(properties), getPath());
120    }
121
122    public String getProperty(String key, String defaultValue) {
123        String value = getProperties().get(key);
124        return (value != null) ? value : defaultValue;
125    }
126
127    /**
128     * Copies the specified artifact properties. This utility method should be used when creating new artifact instances
129     * with caller-supplied properties.
130     *
131     * @param properties The properties to copy, may be {@code null}.
132     * @return The copied and read-only properties, never {@code null}.
133     */
134    protected static Map<String, String> copyProperties(Map<String, String> properties) {
135        if (properties != null && !properties.isEmpty()) {
136            return Collections.unmodifiableMap(new HashMap<>(properties));
137        } else {
138            return Collections.emptyMap();
139        }
140    }
141
142    @Override
143    public String toString() {
144        StringBuilder buffer = new StringBuilder(128);
145        buffer.append(getGroupId());
146        buffer.append(':').append(getArtifactId());
147        buffer.append(':').append(getExtension());
148        if (!getClassifier().isEmpty()) {
149            buffer.append(':').append(getClassifier());
150        }
151        buffer.append(':').append(getVersion());
152        return buffer.toString();
153    }
154
155    /**
156     * Compares this artifact with the specified object.
157     *
158     * @param obj The object to compare this artifact against, may be {@code null}.
159     * @return {@code true} if and only if the specified object is another {@link Artifact} with equal coordinates,
160     *         properties and file, {@code false} otherwise.
161     */
162    @Override
163    public boolean equals(Object obj) {
164        if (obj == this) {
165            return true;
166        } else if (!(obj instanceof Artifact)) {
167            return false;
168        }
169
170        Artifact that = (Artifact) obj;
171
172        return Objects.equals(getArtifactId(), that.getArtifactId())
173                && Objects.equals(getGroupId(), that.getGroupId())
174                && Objects.equals(getVersion(), that.getVersion())
175                && Objects.equals(getExtension(), that.getExtension())
176                && Objects.equals(getClassifier(), that.getClassifier())
177                && Objects.equals(getPath(), that.getPath())
178                && Objects.equals(getProperties(), that.getProperties());
179    }
180
181    /**
182     * Returns a hash code for this artifact.
183     *
184     * @return A hash code for the artifact.
185     */
186    @Override
187    public int hashCode() {
188        int hash = 17;
189        hash = hash * 31 + getGroupId().hashCode();
190        hash = hash * 31 + getArtifactId().hashCode();
191        hash = hash * 31 + getExtension().hashCode();
192        hash = hash * 31 + getClassifier().hashCode();
193        hash = hash * 31 + getVersion().hashCode();
194        hash = hash * 31 + hash(getPath());
195        return hash;
196    }
197
198    private static int hash(Object obj) {
199        return (obj != null) ? obj.hashCode() : 0;
200    }
201}