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