001/*
002 * Extra Bnd Repository Plugins
003 * Copyright (C) 2019  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 an archive from this revision.
114     *
115     * @param extension the extension
116     * @param classifier the classifier
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(this, (MavenVersion) null, extension,
122            classifier);
123    }
124
125    /**
126     * Get an archive from this revision.
127     *
128     * @param version the version
129     * @param extension the extension
130     * @param classifier the classifier
131     * @return the archive
132     */
133    public BoundArchive archive(MavenVersion version, String extension,
134            String classifier) {
135        return new BoundArchive(this, version, extension, classifier);
136    }
137
138    /**
139     * Returns the Metadata.
140     *
141     * @return the string
142     * @see aQute.maven.api.Revision#metadata()
143     */
144    public String metadata() {
145        return unbound.metadata();
146    }
147
148    /**
149     * Returns the metadata for the given id.
150     *
151     * @param id the id
152     * @return the string
153     * @see aQute.maven.api.Revision#metadata(java.lang.String)
154     */
155    @SuppressWarnings("PMD.ShortVariable")
156    public String metadata(String id) {
157        return unbound.metadata(id);
158    }
159
160    /**
161     * Returns the revision with the origin repository attached in
162     * square brackets.
163     * 
164     * @return the string representation
165     * @see aQute.maven.api.Revision#toString()
166     */
167    public String toString() {
168        return unbound.toString() + "[" + mavenBackingRepository.toString()
169            + "]";
170    }
171
172    /**
173     * Return the pom archive.
174     *
175     * @return the archive
176     * @see aQute.maven.api.Revision#pomArchive()
177     */
178    public Archive pomArchive() {
179        return unbound.pomArchive();
180    }
181
182    /**
183     * Gets the pom archive.
184     *
185     * @return the pom archive
186     * @see aQute.maven.api.Revision#getPomArchive()
187     */
188    public Archive getPomArchive() {
189        return unbound.getPomArchive();
190    }
191
192    /**
193     * Compare to other revision. Ignores repository.
194     *
195     * @param other the other
196     * @return the int
197     * @see aQute.maven.api.Revision#compareTo(aQute.maven.api.Revision)
198     */
199    public int compareTo(Revision other) {
200        return unbound.compareTo(other);
201    }
202
203    /**
204     * Compare to other revision. Ignores repository.
205     *
206     * @param other the other
207     * @return the int
208     * @see aQute.maven.api.Revision#compareTo(aQute.maven.api.Revision)
209     */
210    public int compareTo(BoundRevision other) {
211        return unbound.compareTo(other.unbound());
212    }
213
214    /**
215     * Hash code. Ignores repository.
216     *
217     * @return the int
218     * @see aQute.maven.api.Revision#hashCode()
219     */
220    public int hashCode() {
221        return unbound.hashCode();
222    }
223
224    /**
225     * Equals. Ignores respository.
226     *
227     * @param obj the obj
228     * @return true, if successful
229     * @see aQute.maven.api.Revision#equals(java.lang.Object)
230     */
231    public boolean equals(Object obj) {
232        if (obj instanceof BoundRevision) {
233            return unbound.equals(((BoundRevision) obj).unbound);
234        }
235        return unbound.equals(obj);
236    }
237
238}