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.apache.maven.repository.internal;
20  
21  import java.io.File;
22  import java.util.Map;
23  import java.util.Objects;
24  
25  import org.eclipse.aether.artifact.AbstractArtifact;
26  import org.eclipse.aether.artifact.Artifact;
27  
28  /**
29   * @author Benjamin Bentmann
30   */
31  public final class RelocatedArtifact extends AbstractArtifact {
32  
33      private final Artifact artifact;
34  
35      private final String groupId;
36  
37      private final String artifactId;
38  
39      private final String version;
40  
41      private final String message;
42  
43      RelocatedArtifact(Artifact artifact, String groupId, String artifactId, String version, String message) {
44          this.artifact = Objects.requireNonNull(artifact, "artifact cannot be null");
45          // TODO Use StringUtils here
46          this.groupId = (groupId != null && groupId.length() > 0) ? groupId : null;
47          this.artifactId = (artifactId != null && artifactId.length() > 0) ? artifactId : null;
48          this.version = (version != null && version.length() > 0) ? version : null;
49          this.message = (message != null && message.length() > 0) ? message : null;
50      }
51  
52      public String getGroupId() {
53          if (groupId != null) {
54              return groupId;
55          } else {
56              return artifact.getGroupId();
57          }
58      }
59  
60      public String getArtifactId() {
61          if (artifactId != null) {
62              return artifactId;
63          } else {
64              return artifact.getArtifactId();
65          }
66      }
67  
68      public String getVersion() {
69          if (version != null) {
70              return version;
71          } else {
72              return artifact.getVersion();
73          }
74      }
75  
76      // Revise these three methods when MRESOLVER-233 is delivered
77      @Override
78      public Artifact setVersion(String version) {
79          String current = getVersion();
80          if (current.equals(version) || (version == null && current.length() <= 0)) {
81              return this;
82          }
83          return new RelocatedArtifact(artifact, groupId, artifactId, version, message);
84      }
85  
86      @Override
87      public Artifact setFile(File file) {
88          File current = getFile();
89          if (Objects.equals(current, file)) {
90              return this;
91          }
92          return new RelocatedArtifact(artifact.setFile(file), groupId, artifactId, version, message);
93      }
94  
95      @Override
96      public Artifact setProperties(Map<String, String> properties) {
97          Map<String, String> current = getProperties();
98          if (current.equals(properties) || (properties == null && current.isEmpty())) {
99              return this;
100         }
101         return new RelocatedArtifact(artifact.setProperties(properties), groupId, artifactId, version, message);
102     }
103 
104     public String getClassifier() {
105         return artifact.getClassifier();
106     }
107 
108     public String getExtension() {
109         return artifact.getExtension();
110     }
111 
112     public File getFile() {
113         return artifact.getFile();
114     }
115 
116     public String getProperty(String key, String defaultValue) {
117         return artifact.getProperty(key, defaultValue);
118     }
119 
120     public Map<String, String> getProperties() {
121         return artifact.getProperties();
122     }
123 
124     public String getMessage() {
125         return message;
126     }
127 }