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