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.util.artifact;
20  
21  import java.io.File;
22  import java.nio.file.Path;
23  import java.util.Map;
24  
25  import org.eclipse.aether.artifact.AbstractArtifact;
26  import org.eclipse.aether.artifact.Artifact;
27  
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * An artifact that delegates to another artifact instance. This class serves as a base for subclasses that want to
32   * carry additional data fields.
33   */
34  public abstract class DelegatingArtifact extends AbstractArtifact {
35  
36      private final Artifact delegate;
37  
38      /**
39       * Creates a new artifact instance that delegates to the specified artifact.
40       *
41       * @param delegate The artifact to delegate to, must not be {@code null}.
42       */
43      protected DelegatingArtifact(Artifact delegate) {
44          this.delegate = requireNonNull(delegate, "delegate artifact cannot be null");
45      }
46  
47      /**
48       * Creates a new artifact instance that delegates to the specified artifact. Subclasses should use this hook to
49       * instantiate themselves, taking along any data from the current instance that was added.
50       *
51       * @param delegate The artifact to delegate to, must not be {@code null}.
52       * @return The new delegating artifact, never {@code null}.
53       */
54      protected abstract DelegatingArtifact newInstance(Artifact delegate);
55  
56      @Override
57      public String getGroupId() {
58          return delegate.getGroupId();
59      }
60  
61      @Override
62      public String getArtifactId() {
63          return delegate.getArtifactId();
64      }
65  
66      @Override
67      public String getVersion() {
68          return delegate.getVersion();
69      }
70  
71      @Override
72      public Artifact setVersion(String version) {
73          Artifact artifact = delegate.setVersion(version);
74          if (artifact != delegate) {
75              return newInstance(artifact);
76          }
77          return this;
78      }
79  
80      @Override
81      public String getBaseVersion() {
82          return delegate.getBaseVersion();
83      }
84  
85      @Override
86      public boolean isSnapshot() {
87          return delegate.isSnapshot();
88      }
89  
90      @Override
91      public String getClassifier() {
92          return delegate.getClassifier();
93      }
94  
95      @Override
96      public String getExtension() {
97          return delegate.getExtension();
98      }
99  
100     @Deprecated
101     @Override
102     public File getFile() {
103         return delegate.getFile();
104     }
105 
106     @Override
107     public Path getPath() {
108         return delegate.getPath();
109     }
110 
111     @Deprecated
112     @Override
113     public Artifact setFile(File file) {
114         Artifact artifact = delegate.setFile(file);
115         if (artifact != delegate) {
116             return newInstance(artifact);
117         }
118         return this;
119     }
120 
121     @Override
122     public Artifact setPath(Path path) {
123         Artifact artifact = delegate.setPath(path);
124         if (artifact != delegate) {
125             return newInstance(artifact);
126         }
127         return this;
128     }
129 
130     public String getProperty(String key, String defaultValue) {
131         return delegate.getProperty(key, defaultValue);
132     }
133 
134     public Map<String, String> getProperties() {
135         return delegate.getProperties();
136     }
137 
138     public Artifact setProperties(Map<String, String> properties) {
139         Artifact artifact = delegate.setProperties(properties);
140         if (artifact != delegate) {
141             return newInstance(artifact);
142         }
143         return this;
144     }
145 
146     @Override
147     public boolean equals(Object obj) {
148         if (obj == this) {
149             return true;
150         }
151 
152         if (obj instanceof DelegatingArtifact) {
153             return delegate.equals(((DelegatingArtifact) obj).delegate);
154         }
155 
156         return delegate.equals(obj);
157     }
158 
159     @Override
160     public int hashCode() {
161         return delegate.hashCode();
162     }
163 
164     @Override
165     public String toString() {
166         return delegate.toString();
167     }
168 }