001/*
002 * Extra Bnd Repository Plugins
003 * Copyright (C) 2019-2021  Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package de.mnl.osgi.bnd.maven;
020
021import aQute.maven.api.Archive;
022import aQute.maven.api.Revision;
023import aQute.maven.provider.MavenBackingRepository;
024
025/**
026 * A revision with a reference to the maven repository
027 * in which it was found.
028 * <P>
029 * This class resorts to a delegation pattern as second best solution,
030 * because {@link Revision} has only an invisible constructor and can 
031 * therefore not be extended. The drawback is that the information
032 * about the repository doesn't propagate, i.e. you cannot retrieve
033 * it from an ordinary {@link Archive} related to this 
034 * {@link BoundRevision}. If you need the information, you have to use 
035 * a {@link BoundArchive} instead. 
036 * <P>
037 * @see <a href="https://github.com/bndtools/bnd/issues/3058">Related 
038 * bnd issue</a>
039 */
040public class BoundRevision implements Comparable<BoundRevision> {
041
042    private final MavenBackingRepository mavenBackingRepository;
043    private final Revision unbound;
044
045    /**
046     * Instantiates a new bound revision.
047     *
048     * @param mavenBackingRepository the maven backing repository
049     * @param revision the revision
050     */
051    public BoundRevision(MavenBackingRepository mavenBackingRepository,
052            Revision revision) {
053        this.mavenBackingRepository = mavenBackingRepository;
054        this.unbound = revision;
055    }
056
057    /**
058     * Gets the maven backing repository.
059     *
060     * @return the mavenBackingRepository
061     */
062    public final MavenBackingRepository mavenBackingRepository() {
063        return mavenBackingRepository;
064    }
065
066    /**
067     * Gets the revision.
068     *
069     * @return the revision
070     */
071    public Revision unbound() {
072        return unbound;
073    }
074
075    /**
076     * Returns the group id.
077     *
078     * @return the id
079     */
080    public String groupId() {
081        return unbound.group;
082    }
083
084    /**
085     * Returns the artifact id.
086     *
087     * @return the id
088     */
089    public String artifactId() {
090        return unbound.artifact;
091    }
092
093    /**
094     * Returns the version.
095     *
096     * @return the version
097     */
098    public MavenVersion version() {
099        return MavenVersion.from(unbound.version);
100    }
101
102    /**
103     * Checks if is snapshot.
104     *
105     * @return true, if is snapshot
106     * @see aQute.maven.api.Revision#isSnapshot()
107     */
108    public boolean isSnapshot() {
109        return unbound.isSnapshot();
110    }
111
112    /**
113     * Get a bound archive from this revision.
114     *
115     * @param extension the archive's extension (or {@code null} for "jar")
116     * @param classifier the archive's classifier (or {@code null} for "")
117     * @return the archive
118     * @see aQute.maven.api.Revision#archive(java.lang.String, java.lang.String)
119     */
120    public BoundArchive archive(String extension, String classifier) {
121        return new BoundArchive(mavenBackingRepository, unbound, null,
122            extension, classifier);
123    }
124
125    /**
126     * Get the default archive (extension "jar", no classifier) 
127     * for this revision.
128     *
129     * @return the archive
130     * @see aQute.maven.api.Revision#archive(java.lang.String, java.lang.String)
131     */
132    public BoundArchive archive() {
133        return archive(null, null);
134    }
135
136    /**
137     * Get an archive from this revision.
138     *
139     * @param version the version
140     * @param extension the archive's extension (or {@code null} for "jar")
141     * @param classifier the archive's classifier (or {@code null} for "")
142     * @return the archive
143     */
144    public BoundArchive archive(MavenVersion version, String extension,
145            String classifier) {
146        return new BoundArchive(mavenBackingRepository, unbound, version,
147            extension, classifier);
148    }
149
150    /**
151     * Returns the Metadata.
152     *
153     * @return the string
154     * @see aQute.maven.api.Revision#metadata()
155     */
156    public String metadata() {
157        return unbound.metadata();
158    }
159
160    /**
161     * Returns the metadata for the given id.
162     *
163     * @param id the id
164     * @return the string
165     * @see aQute.maven.api.Revision#metadata(java.lang.String)
166     */
167    @SuppressWarnings("PMD.ShortVariable")
168    public String metadata(String id) {
169        return unbound.metadata(id);
170    }
171
172    /**
173     * Returns the revision with the origin repository attached in
174     * square brackets.
175     * 
176     * @return the string representation
177     * @see aQute.maven.api.Revision#toString()
178     */
179    public String toString() {
180        return unbound.toString() + "[" + mavenBackingRepository.toString()
181            + "]";
182    }
183
184    /**
185     * Return the pom archive.
186     *
187     * @return the archive
188     * @see aQute.maven.api.Revision#pomArchive()
189     */
190    public Archive pomArchive() {
191        return unbound.pomArchive();
192    }
193
194    /**
195     * Gets the pom archive.
196     *
197     * @return the pom archive
198     * @see aQute.maven.api.Revision#getPomArchive()
199     */
200    public Archive getPomArchive() {
201        return unbound.getPomArchive();
202    }
203
204    /**
205     * Compare to other revision. Ignores repository.
206     *
207     * @param other the other
208     * @return the int
209     * @see aQute.maven.api.Revision#compareTo(aQute.maven.api.Revision)
210     */
211    public int compareTo(Revision other) {
212        return unbound.compareTo(other);
213    }
214
215    /**
216     * Compare to other revision. Ignores repository.
217     *
218     * @param other the other
219     * @return the int
220     * @see aQute.maven.api.Revision#compareTo(aQute.maven.api.Revision)
221     */
222    public int compareTo(BoundRevision other) {
223        return unbound.compareTo(other.unbound());
224    }
225
226    /**
227     * Hash code. Ignores repository.
228     *
229     * @return the int
230     * @see aQute.maven.api.Revision#hashCode()
231     */
232    public int hashCode() {
233        return unbound.hashCode();
234    }
235
236    /**
237     * Equals. Ignores respository.
238     *
239     * @param obj the obj
240     * @return true, if successful
241     * @see aQute.maven.api.Revision#equals(java.lang.Object)
242     */
243    public boolean equals(Object obj) {
244        if (obj instanceof BoundRevision) {
245            return unbound.equals(((BoundRevision) obj).unbound);
246        }
247        return unbound.equals(obj);
248    }
249
250}