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.util.Map;
023
024import org.eclipse.aether.artifact.AbstractArtifact;
025import org.eclipse.aether.artifact.Artifact;
026
027import static java.util.Objects.requireNonNull;
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 extends AbstractArtifact {
034
035    private final Artifact delegate;
036
037    /**
038     * Creates a new artifact instance that delegates to the specified artifact.
039     *
040     * @param delegate The artifact to delegate to, must not be {@code null}.
041     */
042    protected DelegatingArtifact(Artifact delegate) {
043        this.delegate = requireNonNull(delegate, "delegate artifact cannot be null");
044    }
045
046    /**
047     * Creates a new artifact instance that delegates to the specified artifact. Subclasses should use this hook to
048     * instantiate themselves, taking along any data from the current instance that was added.
049     *
050     * @param delegate The artifact to delegate to, must not be {@code null}.
051     * @return The new delegating artifact, never {@code null}.
052     */
053    protected abstract DelegatingArtifact newInstance(Artifact delegate);
054
055    public String getGroupId() {
056        return delegate.getGroupId();
057    }
058
059    public String getArtifactId() {
060        return delegate.getArtifactId();
061    }
062
063    public String getVersion() {
064        return delegate.getVersion();
065    }
066
067    public Artifact setVersion(String version) {
068        Artifact artifact = delegate.setVersion(version);
069        if (artifact != delegate) {
070            return newInstance(artifact);
071        }
072        return this;
073    }
074
075    public String getBaseVersion() {
076        return delegate.getBaseVersion();
077    }
078
079    public boolean isSnapshot() {
080        return delegate.isSnapshot();
081    }
082
083    public String getClassifier() {
084        return delegate.getClassifier();
085    }
086
087    public String getExtension() {
088        return delegate.getExtension();
089    }
090
091    public File getFile() {
092        return delegate.getFile();
093    }
094
095    public Artifact setFile(File file) {
096        Artifact artifact = delegate.setFile(file);
097        if (artifact != delegate) {
098            return newInstance(artifact);
099        }
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}