View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.artifact;
20  
21  import java.io.File;
22  import java.nio.file.Path;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.Objects;
27  import java.util.regex.Matcher;
28  import java.util.regex.Pattern;
29  
30  /**
31   * A skeleton class for artifacts.
32   */
33  public abstract class AbstractArtifact implements Artifact {
34  
35      private static final String SNAPSHOT = "SNAPSHOT";
36  
37      private static final Pattern SNAPSHOT_TIMESTAMP = Pattern.compile("^(.*-)?([0-9]{8}\\.[0-9]{6}-[0-9]+)$");
38  
39      /**
40       * Constructor for subclasses.
41       */
42      public AbstractArtifact() {}
43  
44      @Override
45      public boolean isSnapshot() {
46          return isSnapshot(getVersion());
47      }
48  
49      private static boolean isSnapshot(String version) {
50          return version.endsWith(SNAPSHOT) || SNAPSHOT_TIMESTAMP.matcher(version).matches();
51      }
52  
53      @Override
54      public String getBaseVersion() {
55          return toBaseVersion(getVersion());
56      }
57  
58      private static String toBaseVersion(String version) {
59          String baseVersion;
60  
61          if (version == null) {
62              baseVersion = null;
63          } else if (version.startsWith("[") || version.startsWith("(")) {
64              baseVersion = version;
65          } else {
66              Matcher m = SNAPSHOT_TIMESTAMP.matcher(version);
67              if (m.matches()) {
68                  if (m.group(1) != null) {
69                      baseVersion = m.group(1) + SNAPSHOT;
70                  } else {
71                      baseVersion = SNAPSHOT;
72                  }
73              } else {
74                  baseVersion = version;
75              }
76          }
77  
78          return baseVersion;
79      }
80  
81      /**
82       * Creates a new artifact with the specified coordinates, properties and file.
83       *
84       * @param version The version of the artifact, may be {@code null}.
85       * @param properties The properties of the artifact, may be {@code null} if none. The method may assume immutability
86       *            of the supplied map, i.e. need not copy it.
87       * @param path The resolved file of the artifact, may be {@code null}.
88       * @return The new artifact instance, never {@code null}.
89       */
90      private Artifact newInstance(String version, Map<String, String> properties, Path path) {
91          return new DefaultArtifact(
92                  getGroupId(), getArtifactId(), getClassifier(), getExtension(), version, path, properties);
93      }
94  
95      @Override
96      public Artifact setVersion(String version) {
97          String current = getVersion();
98          if (current.equals(version) || (version == null && current.isEmpty())) {
99              return this;
100         }
101         return newInstance(version, getProperties(), getPath());
102     }
103 
104     /**
105      * This method should (and in Resolver is) overridden, but is kept just to preserve backward compatibility if
106      * this class is extended somewhere. It delegates to the deprecated {@link #getFile()} on purpose, as that is
107      * the method such a subclass implements.
108      */
109     @SuppressWarnings("deprecation")
110     public Path getPath() {
111         File file = getFile();
112         return file != null ? file.toPath() : null;
113     }
114 
115     @Deprecated
116     @Override
117     public Artifact setFile(File file) {
118         return setPath(file != null ? file.toPath() : null);
119     }
120 
121     @Override
122     public Artifact setPath(Path path) {
123         Path current = getPath();
124         if (Objects.equals(current, path)) {
125             return this;
126         }
127         return newInstance(getVersion(), getProperties(), path);
128     }
129 
130     public Artifact setProperties(Map<String, String> properties) {
131         Map<String, String> current = getProperties();
132         if (current.equals(properties) || (properties == null && current.isEmpty())) {
133             return this;
134         }
135         return newInstance(getVersion(), copyProperties(properties), getPath());
136     }
137 
138     public String getProperty(String key, String defaultValue) {
139         String value = getProperties().get(key);
140         return (value != null) ? value : defaultValue;
141     }
142 
143     /**
144      * Copies the specified artifact properties. This utility method should be used when creating new artifact instances
145      * with caller-supplied properties.
146      *
147      * @param properties The properties to copy, may be {@code null}.
148      * @return The copied and read-only properties, never {@code null}.
149      */
150     protected static Map<String, String> copyProperties(Map<String, String> properties) {
151         if (properties != null && !properties.isEmpty()) {
152             return Collections.unmodifiableMap(new HashMap<>(properties));
153         } else {
154             return Collections.emptyMap();
155         }
156     }
157 
158     @Override
159     public String toString() {
160         StringBuilder buffer = new StringBuilder(128);
161         buffer.append(getGroupId());
162         buffer.append(':').append(getArtifactId());
163         buffer.append(':').append(getExtension());
164         if (!getClassifier().isEmpty()) {
165             buffer.append(':').append(getClassifier());
166         }
167         buffer.append(':').append(getVersion());
168         return buffer.toString();
169     }
170 
171     /**
172      * Compares this artifact with the specified object.
173      *
174      * @param obj The object to compare this artifact against, may be {@code null}.
175      * @return {@code true} if and only if the specified object is another {@link Artifact} with equal coordinates,
176      *         properties and file, {@code false} otherwise.
177      */
178     @Override
179     public boolean equals(Object obj) {
180         if (obj == this) {
181             return true;
182         } else if (!(obj instanceof Artifact)) {
183             return false;
184         }
185 
186         Artifact that = (Artifact) obj;
187 
188         return Objects.equals(getArtifactId(), that.getArtifactId())
189                 && Objects.equals(getGroupId(), that.getGroupId())
190                 && Objects.equals(getVersion(), that.getVersion())
191                 && Objects.equals(getExtension(), that.getExtension())
192                 && Objects.equals(getClassifier(), that.getClassifier())
193                 && Objects.equals(getPath(), that.getPath())
194                 && Objects.equals(getProperties(), that.getProperties());
195     }
196 
197     /**
198      * Returns a hash code for this artifact.
199      *
200      * @return A hash code for the artifact.
201      */
202     @Override
203     public int hashCode() {
204         int hash = 17;
205         hash = hash * 31 + getGroupId().hashCode();
206         hash = hash * 31 + getArtifactId().hashCode();
207         hash = hash * 31 + getExtension().hashCode();
208         hash = hash * 31 + getClassifier().hashCode();
209         hash = hash * 31 + getVersion().hashCode();
210         hash = hash * 31 + hash(getPath());
211         return hash;
212     }
213 
214     private static int hash(Object obj) {
215         return (obj != null) ? obj.hashCode() : 0;
216     }
217 }