View Javadoc

1   /*
2    ProgramVersionImpl.java
3    Creation date : 8/06/2010
4    Copyright © Benjamin Croizet (graffity2199@yahoo.fr)
5   
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    or GNU Lesser General Public License as published by the
9    Free Software Foundation; either version 3 of the License,
10   or (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received copies of the GNU General Public License
18   and GNU Lesser General Public License along with this program;
19   if not, write to the Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21   http://www.fsf.org/licensing/licenses/gpl.html
22   http://www.gnu.org/licenses/lgpl.html
23   */
24  
25  package net.sourceforge.plantumldependency.commoncli.program.version.impl;
26  
27  import static java.util.logging.Level.SEVERE;
28  import static java.util.logging.Logger.getLogger;
29  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.DOT_CHAR;
30  import static net.sourceforge.plantumldependency.common.constants.CommonConstants.HASHCODE_PRIME_NUMBER1;
31  import static net.sourceforge.plantumldependency.common.constants.CommonConstants.HASHCODE_PRIME_NUMBER2;
32  import static net.sourceforge.plantumldependency.common.constants.log.ErrorConstants.UNEXPECTED_ERROR;
33  import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
34  import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.AFTER;
35  import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.BEFORE;
36  import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.EQUAL;
37  import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
38  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.COMPILATION_TIME_NULL_ERROR;
39  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.NOT_POSITIVE_VERSION_NUMBER_ERROR;
40  
41  import java.util.Date;
42  import java.util.logging.Logger;
43  
44  import net.sourceforge.plantumldependency.commoncli.program.version.ProgramVersion;
45  
46  /**
47   * The default {@link ProgramVersion} implementation.
48   *
49   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
50   * @since 1.3.0
51   * @version 1.3.0
52   */
53  public class ProgramVersionImpl implements ProgramVersion {
54  
55      /** Serial version UID. */
56      private static final long serialVersionUID = 3475034667983466083L;
57  
58      /** The class logger. */
59      private static final transient Logger LOGGER = getLogger(ProgramVersionImpl.class.getName());
60  
61      /** The program major version number. */
62      private int majorVersionNumber;
63  
64      /** The program minor version number. */
65      private int minorVersionNumber;
66  
67      /** The program revision version number. */
68      private int revisionVersionNumber;
69  
70      /** The program compilation time. */
71      private Date compilationTime;
72  
73      /** The boolean telling if the program version is a snapshot. */
74      private boolean snapshot;
75  
76      /**
77       * Medium constructor.
78       *
79       * @param majorVersion
80       *            the program major version number, must be positive.
81       * @param minorVersion
82       *            the program minor version number, must be positive.
83       * @param revisionVersion
84       *            the program revision version number, must be positive.
85       * @since 1.3.0
86       */
87      public ProgramVersionImpl(final int majorVersion, final int minorVersion, final int revisionVersion) {
88          this(majorVersion, minorVersion, revisionVersion, new Date(), false);
89      }
90  
91      /**
92       * Medium constructor.
93       *
94       * @param majorVersion
95       *            the program major version number, must be positive.
96       * @param minorVersion
97       *            the program minor version number, must be positive.
98       * @param revisionVersion
99       *            the program revision version number, must be positive.
100      * @param compilation
101      *            the program compilation time, mustn't be <code>null</code>.
102      * @since 1.3.0
103      */
104     public ProgramVersionImpl(final int majorVersion, final int minorVersion, final int revisionVersion,
105             final Date compilation) {
106         this(majorVersion, minorVersion, revisionVersion, compilation, false);
107     }
108 
109     /**
110      * Full constructor.
111      *
112      * @param majorVersion
113      *            the program major version number, must be positive.
114      * @param minorVersion
115      *            the program minor version number, must be positive.
116      * @param revisionVersion
117      *            the program revision version number, must be positive.
118      * @param compilation
119      *            the program compilation time, mustn't be <code>null</code>.
120      * @param isSnapshot
121      *            the boolean telling if the program version is a snapshot
122      * @since 1.3.0
123      */
124     public ProgramVersionImpl(final int majorVersion, final int minorVersion, final int revisionVersion,
125             final Date compilation, final boolean isSnapshot) {
126         setCompilationTime(compilation);
127         setMajorVersionNumber(majorVersion);
128         setMinorVersionNumber(minorVersion);
129         setRevisionVersionNumber(revisionVersion);
130         setSnapshot(isSnapshot);
131     }
132 
133     /**
134      * {@inheritDoc}
135      *
136      * @since 1.3.0
137      */
138     @Override
139     public int compareTo(final ProgramVersion o) {
140         if (this == o) {
141             return EQUAL.getResult();
142         }
143         int comparison = getMajorVersionNumber() < o.getMajorVersionNumber() ? BEFORE.getResult()
144                 : getMajorVersionNumber() == o.getMajorVersionNumber() ? EQUAL.getResult() : AFTER.getResult();
145         if (comparison == EQUAL.getResult()) {
146             comparison = getMinorVersionNumber() < o.getMinorVersionNumber() ? BEFORE.getResult()
147                     : getMinorVersionNumber() == o.getMinorVersionNumber() ? EQUAL.getResult() : AFTER.getResult();
148             if (comparison == EQUAL.getResult()) {
149                 comparison = getRevisionVersionNumber() < o.getRevisionVersionNumber() ? BEFORE.getResult()
150                         : getRevisionVersionNumber() == o.getRevisionVersionNumber() ? EQUAL.getResult() : AFTER
151                                 .getResult();
152                 if (comparison == EQUAL.getResult()) {
153                     comparison = getCompilationTime().compareTo(o.getCompilationTime());
154                     if (comparison == EQUAL.getResult()) {
155                         return isSnapshot() == o.isSnapshot() ? EQUAL.getResult() : isSnapshot() ? AFTER.getResult()
156                                 : BEFORE.getResult();
157                     }
158                     return comparison;
159 
160                 }
161                 return comparison;
162 
163             }
164             return comparison;
165 
166         }
167         return comparison;
168 
169     }
170 
171     /**
172      * {@inheritDoc}
173      *
174      * @since 1.3.0
175      */
176     @Override
177     public ProgramVersion deepClone() {
178         ProgramVersionImpl p = null;
179 
180         try {
181             p = (ProgramVersionImpl) super.clone();
182             p.compilationTime = (Date) getCompilationTime().clone();
183         } catch (final CloneNotSupportedException cnse) {
184             LOGGER.log(SEVERE, UNEXPECTED_ERROR, cnse);
185         }
186 
187         return p;
188     }
189 
190     /**
191      * {@inheritDoc}
192      *
193      * @since 1.3.0
194      */
195     @Override
196     public boolean equals(final Object obj) {
197         if (this == obj) {
198             return true;
199         }
200         if (obj == null) {
201             return false;
202         }
203         if (getClass() != obj.getClass()) {
204             return false;
205         }
206         final ProgramVersionImpl other = (ProgramVersionImpl) obj;
207         if (compilationTime == null) {
208             if (other.compilationTime != null) {
209                 return false;
210             }
211         } else if (!compilationTime.equals(other.compilationTime)) {
212             return false;
213         }
214         if (majorVersionNumber != other.majorVersionNumber) {
215             return false;
216         }
217         if (minorVersionNumber != other.minorVersionNumber) {
218             return false;
219         }
220         if (revisionVersionNumber != other.revisionVersionNumber) {
221             return false;
222         }
223         if (snapshot != other.snapshot) {
224             return false;
225         }
226         return true;
227     }
228 
229     /**
230      * {@inheritDoc}
231      *
232      * @since 1.3.0
233      */
234     @Override
235     public Date getCompilationTime() {
236         return new Date(compilationTime.getTime());
237     }
238 
239     /**
240      * {@inheritDoc}
241      *
242      * @since 1.3.0
243      */
244     @Override
245     public String getFullVersionNumber() {
246         return getMajorVersionNumber() + DOT_CHAR + getMinorVersionNumber() + DOT_CHAR + getRevisionVersionNumber();
247     }
248 
249     /**
250      * {@inheritDoc}
251      *
252      * @since 1.3.0
253      */
254     @Override
255     public int getMajorVersionNumber() {
256         return majorVersionNumber;
257     }
258 
259     /**
260      * {@inheritDoc}
261      *
262      * @since 1.3.0
263      */
264     @Override
265     public int getMinorVersionNumber() {
266         return minorVersionNumber;
267     }
268 
269     /**
270      * {@inheritDoc}
271      *
272      * @since 1.3.0
273      */
274     @Override
275     public int getRevisionVersionNumber() {
276         return revisionVersionNumber;
277     }
278 
279     /**
280      * {@inheritDoc}
281      *
282      * @since 1.3.0
283      */
284     @Override
285     public int hashCode() {
286         final int prime = 31;
287         int result = 1;
288         result = prime * result + ((compilationTime == null) ? 0 : compilationTime.hashCode());
289         result = prime * result + majorVersionNumber;
290         result = prime * result + minorVersionNumber;
291         result = prime * result + revisionVersionNumber;
292         result = prime * result + (snapshot ? HASHCODE_PRIME_NUMBER1 : HASHCODE_PRIME_NUMBER2);
293         return result;
294     }
295 
296     /**
297      * {@inheritDoc}
298      *
299      * @since 1.3.0
300      */
301     @Override
302     public boolean isSnapshot() {
303         return snapshot;
304     }
305 
306     /**
307      * Sets the value of <code>compilationTime</code>.
308      *
309      * @param value
310      *            the <code>compilationTime</code> to set, mustn't be <code>null</code>.
311      * @see #getCompilationTime()
312      * @since 1.3.0
313      */
314     private void setCompilationTime(final Date value) {
315         checkNull(value, COMPILATION_TIME_NULL_ERROR);
316 
317         compilationTime = value;
318     }
319 
320     /**
321      * Sets the value of <code>majorVersionNumber</code>.
322      *
323      * @param value
324      *            the <code>majorVersionNumber</code> to set, can be <code>null</code>.
325      * @see #getMajorVersionNumber()
326      * @since 1.3.0
327      */
328     private void setMajorVersionNumber(final int value) {
329         if (value < 0) {
330             throw new IllegalArgumentException(buildLogString(NOT_POSITIVE_VERSION_NUMBER_ERROR, value));
331         }
332         majorVersionNumber = value;
333     }
334 
335     /**
336      * Sets the value of <code>minorVersionNumber</code>.
337      *
338      * @param value
339      *            the <code>minorVersionNumber</code> to set, can be <code>null</code>.
340      * @see #getMinorVersionNumber()
341      * @since 1.3.0
342      */
343     private void setMinorVersionNumber(final int value) {
344         if (value < 0) {
345             throw new IllegalArgumentException(buildLogString(NOT_POSITIVE_VERSION_NUMBER_ERROR, value));
346         }
347         minorVersionNumber = value;
348     }
349 
350     /**
351      * Sets the value of <code>revisionVersionNumber</code>.
352      *
353      * @param value
354      *            the <code>revisionVersionNumber</code> to set, can be <code>null</code>.
355      * @see #getRevisionVersionNumber()
356      * @since 1.3.0
357      */
358     private void setRevisionVersionNumber(final int value) {
359         if (value < 0) {
360             throw new IllegalArgumentException(buildLogString(NOT_POSITIVE_VERSION_NUMBER_ERROR, value));
361         }
362         revisionVersionNumber = value;
363     }
364 
365     /**
366      * Sets the value of <code>snapshot</code>.
367      *
368      * @param value
369      *            the <code>snapshot</code> to set, can be <code>null</code>.
370      * @see #isSnapshot()
371      * @since 1.3.0
372      */
373     private void setSnapshot(final boolean value) {
374         snapshot = value;
375     }
376 
377     /**
378      * {@inheritDoc}
379      *
380      * @since 1.3.0
381      */
382     @Override
383     public String toString() {
384         return getClass().getSimpleName() + " [compilationTime=" + compilationTime + ", majorVersionNumber="
385                 + majorVersionNumber + ", minorVersionNumber=" + minorVersionNumber + ", revisionVersionNumber="
386                 + revisionVersionNumber + ", snapshot=" + snapshot + "]";
387     }
388 }