View Javadoc

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