View Javadoc

1   /*
2    * Copyright 2008-2010 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package de.smartics.maven.issues.util;
18  
19  import org.apache.commons.lang.builder.ToStringBuilder;
20  import org.apache.maven.artifact.versioning.ArtifactVersion;
21  
22  /**
23   * A simple model for report references.
24   *
25   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
26   * @version $Revision:591 $
27   */
28  public class ReportReference implements Comparable<ReportReference>
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    // --- members --------------------------------------------------------------
35  
36    /**
37     * The label to display.
38     */
39    private final String label;
40  
41    /**
42     * The URL to the references report.
43     */
44    private final String url;
45  
46    /**
47     * The representative for the versions in this report. Since reports may
48     * contain a range of versions this representative is the first version to be
49     * reported about. It is assumed that all reports are disjunct, report over at
50     * least on version and all versions are consecutive. In addition to that, if
51     * the version reference is prior to another version, all versions in the
52     * report referenced by this instance are also prior.
53     * <p>
54     * The ordering of report references is determined by this representative.
55     * </p>
56     */
57    private final ArtifactVersion versionRepresentative;
58  
59    // ****************************** Initializer *******************************
60  
61    // ****************************** Constructors ******************************
62  
63    /**
64     * Default constructor.
65     *
66     * @param label the label to display.
67     * @param url the URL to the references report.
68     * @param versionRepresentative the representative for the versions in this
69     *          report.
70     * @throws NullPointerException if any parameter is <code>null</code>.
71     */
72    public ReportReference(final String label, final String url,
73        final ArtifactVersion versionRepresentative) throws NullPointerException
74    {
75      checkPrecondition(label, url, versionRepresentative);
76      this.label = label;
77      this.url = url + ".html";
78      this.versionRepresentative = versionRepresentative;
79    }
80  
81    // ****************************** Inner Classes *****************************
82  
83    // ********************************* Methods ********************************
84  
85    // --- init -----------------------------------------------------------------
86  
87    /**
88     * Checks if each given value is not <code>null</code>.
89     *
90     * @param label value to check to be not <code>null</code>.
91     * @param url value to check to be not <code>null</code>.
92     * @param versionRepresentative value to check to be not <code>null</code>.
93     * @throws NullPointerException if any parameter is <code>null</code>.
94     */
95    private void checkPrecondition(final String label, final String url,
96        final ArtifactVersion versionRepresentative) throws NullPointerException
97    {
98      if (label == null)
99      {
100       throw new NullPointerException("Label must not be 'null'.");
101     }
102     if (url == null)
103     {
104       throw new NullPointerException("URL must not be 'null'.");
105     }
106     if (versionRepresentative == null)
107     {
108       throw new NullPointerException(
109           "Version representative must not be 'null' because it is responsible for sorting.");
110     }
111   }
112 
113   // --- get&set --------------------------------------------------------------
114 
115   /**
116    * Returns the label to display.
117    *
118    * @return the label to display.
119    */
120   public String getLabel()
121   {
122     return label;
123   }
124 
125   /**
126    * Returns the URL to the references report.
127    *
128    * @return the URL to the references report.
129    */
130   public String getUrl()
131   {
132     return url;
133   }
134 
135   /**
136    * Returns the representative for the versions in this report. Since reports
137    * may contain a range of versions this representative is the first version to
138    * be reported about. It is assumed that all reports are disjunct, report over
139    * at least on version and all versions are consecutive. In addition to that,
140    * if the version reference is prior to another version, all versions in the
141    * report referenced by this instance are also prior.
142    * <p>
143    * The ordering of report references is determined by this representative.
144    * </p>
145    *
146    * @return the representative for the versions in this report.
147    */
148   public ArtifactVersion getVersionRepresentative()
149   {
150     return versionRepresentative;
151   }
152 
153   // --- business -------------------------------------------------------------
154 
155   // --- object basics --------------------------------------------------------
156 
157   /**
158    * Returns the hash code of the object.
159    *
160    * @return the hash code.
161    */
162   @Override
163   public int hashCode()
164   {
165     return versionRepresentative.toString().hashCode();
166   }
167 
168   /**
169    * Returns <code>true</code> if the given object is semantically equal to the
170    * given object, <code>false</code> otherwise.
171    *
172    * @param object the instance to compare to.
173    * @return <code>true</code> if the given object is semantically equal to the
174    *         given object, <code>false</code> otherwise.
175    */
176   @Override
177   public boolean equals(final Object object)
178   {
179     if (this == object)
180     {
181       return true;
182     }
183     else if (object == null || getClass() != object.getClass())
184     {
185       return false;
186     }
187 
188     final ReportReference other = (ReportReference) object;
189 
190     return (url.equals(other.url) && versionRepresentative.toString().equals(
191         other.versionRepresentative.toString()));
192   }
193 
194   /**
195    * {@inheritDoc}
196    *
197    * @see java.lang.Comparable#compareTo(java.lang.Object)
198    */
199   public int compareTo(final ReportReference o)
200   {
201     int compare = url.compareTo(o.url);
202     if (compare == 0)
203     {
204       compare =
205           InverseVersionComparator.INSTANCE.compare(versionRepresentative,
206               o.versionRepresentative);
207     }
208     return compare;
209   }
210 
211   /**
212    * {@inheritDoc}
213    *
214    * @see java.lang.Object#toString()
215    */
216   @Override
217   public String toString()
218   {
219     return ToStringBuilder.reflectionToString(this);
220   }
221 }