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