View Javadoc
1   package org.eclipse.aether.util.artifact;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Map;
24  import static java.util.Objects.requireNonNull;
25  
26  import org.eclipse.aether.artifact.AbstractArtifact;
27  import org.eclipse.aether.artifact.Artifact;
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
34      extends AbstractArtifact
35  {
36  
37      private final Artifact delegate;
38  
39      /**
40       * Creates a new artifact instance that delegates to the specified artifact.
41       *
42       * @param delegate The artifact to delegate to, must not be {@code null}.
43       */
44      protected DelegatingArtifact( Artifact delegate )
45      {
46          this.delegate = requireNonNull( delegate, "delegate artifact cannot be null" );
47      }
48  
49      /**
50       * Creates a new artifact instance that delegates to the specified artifact. Subclasses should use this hook to
51       * instantiate themselves, taking along any data from the current instance that was added.
52       *
53       * @param delegate The artifact to delegate to, must not be {@code null}.
54       * @return The new delegating artifact, never {@code null}.
55       */
56      protected abstract DelegatingArtifact newInstance( Artifact delegate );
57  
58      public String getGroupId()
59      {
60          return delegate.getGroupId();
61      }
62  
63      public String getArtifactId()
64      {
65          return delegate.getArtifactId();
66      }
67  
68      public String getVersion()
69      {
70          return delegate.getVersion();
71      }
72  
73      public Artifact setVersion( String version )
74      {
75          Artifact artifact = delegate.setVersion( version );
76          if ( artifact != delegate )
77          {
78              return newInstance( artifact );
79          }
80          return this;
81      }
82  
83      public String getBaseVersion()
84      {
85          return delegate.getBaseVersion();
86      }
87  
88      public boolean isSnapshot()
89      {
90          return delegate.isSnapshot();
91      }
92  
93      public String getClassifier()
94      {
95          return delegate.getClassifier();
96      }
97  
98      public String getExtension()
99      {
100         return delegate.getExtension();
101     }
102 
103     public File getFile()
104     {
105         return delegate.getFile();
106     }
107 
108     public Artifact setFile( File file )
109     {
110         Artifact artifact = delegate.setFile( file );
111         if ( artifact != delegate )
112         {
113             return newInstance( artifact );
114         }
115         return this;
116     }
117 
118     public String getProperty( String key, String defaultValue )
119     {
120         return delegate.getProperty( key, defaultValue );
121     }
122 
123     public Map<String, String> getProperties()
124     {
125         return delegate.getProperties();
126     }
127 
128     public Artifact setProperties( Map<String, String> properties )
129     {
130         Artifact artifact = delegate.setProperties( properties );
131         if ( artifact != delegate )
132         {
133             return newInstance( artifact );
134         }
135         return this;
136     }
137 
138     @Override
139     public boolean equals( Object obj )
140     {
141         if ( obj == this )
142         {
143             return true;
144         }
145 
146         if ( obj instanceof DelegatingArtifact )
147         {
148             return delegate.equals( ( (DelegatingArtifact) obj ).delegate );
149         }
150 
151         return delegate.equals( obj );
152     }
153 
154     @Override
155     public int hashCode()
156     {
157         return delegate.hashCode();
158     }
159 
160     @Override
161     public String toString()
162     {
163         return delegate.toString();
164     }
165 
166 }