001package org.eclipse.aether.util.artifact;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.Map;
024import static java.util.Objects.requireNonNull;
025
026import org.eclipse.aether.artifact.AbstractArtifact;
027import org.eclipse.aether.artifact.Artifact;
028
029/**
030 * An artifact that delegates to another artifact instance. This class serves as a base for subclasses that want to
031 * carry additional data fields.
032 */
033public abstract class DelegatingArtifact
034    extends AbstractArtifact
035{
036
037    private final Artifact delegate;
038
039    /**
040     * Creates a new artifact instance that delegates to the specified artifact.
041     *
042     * @param delegate The artifact to delegate to, must not be {@code null}.
043     */
044    protected DelegatingArtifact( Artifact delegate )
045    {
046        this.delegate = requireNonNull( delegate, "delegate artifact cannot be null" );
047    }
048
049    /**
050     * Creates a new artifact instance that delegates to the specified artifact. Subclasses should use this hook to
051     * instantiate themselves, taking along any data from the current instance that was added.
052     *
053     * @param delegate The artifact to delegate to, must not be {@code null}.
054     * @return The new delegating artifact, never {@code null}.
055     */
056    protected abstract DelegatingArtifact newInstance( Artifact delegate );
057
058    public String getGroupId()
059    {
060        return delegate.getGroupId();
061    }
062
063    public String getArtifactId()
064    {
065        return delegate.getArtifactId();
066    }
067
068    public String getVersion()
069    {
070        return delegate.getVersion();
071    }
072
073    public Artifact setVersion( String version )
074    {
075        Artifact artifact = delegate.setVersion( version );
076        if ( artifact != delegate )
077        {
078            return newInstance( artifact );
079        }
080        return this;
081    }
082
083    public String getBaseVersion()
084    {
085        return delegate.getBaseVersion();
086    }
087
088    public boolean isSnapshot()
089    {
090        return delegate.isSnapshot();
091    }
092
093    public String getClassifier()
094    {
095        return delegate.getClassifier();
096    }
097
098    public String getExtension()
099    {
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}