1 /* Jackson JSON-processor.
2  *
3  * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi
4  */

5
6 package com.fasterxml.jackson.core;
7
8 /**
9  * Object that encapsulates versioning information of a component.
10  * Version information includes not just version number but also
11  * optionally group and artifact ids of the component being versioned.
12  *<p>
13  * Note that optional group and artifact id properties are new with Jackson 2.0:
14  * if provided, they should align with Maven artifact information.
15  */

16 public class Version
17     implements Comparable<Version>, java.io.Serializable
18 {
19     private static final long serialVersionUID = 1L;
20
21     private final static Version UNKNOWN_VERSION = new Version(0, 0, 0, nullnullnull);
22
23     protected final int _majorVersion;
24
25     protected final int _minorVersion;
26
27     protected final int _patchLevel;
28
29     protected final String _groupId;
30
31     protected final String _artifactId;
32
33     /**
34      * Additional information for snapshot versions; null for non-snapshot
35      * (release) versions.
36      */

37     protected final String _snapshotInfo;
38
39     /**
40      * @deprecated Use variant that takes group and artifact ids
41      * 
42      * @since 2.1
43      */

44     @Deprecated
45     public Version(int major, int minor, int patchLevel, String snapshotInfo)
46     {
47         this(major, minor, patchLevel, snapshotInfo, nullnull);
48     }
49
50     public Version(int major, int minor, int patchLevel, String snapshotInfo,
51             String groupId, String artifactId)
52     {
53         _majorVersion = major;
54         _minorVersion = minor;
55         _patchLevel = patchLevel;
56         _snapshotInfo = snapshotInfo;
57         _groupId = (groupId == null) ? "" : groupId;
58         _artifactId = (artifactId == null) ? "" : artifactId;
59     }
60
61     /**
62      * Method returns canonical "not known" version, which is used as version
63      * in cases where actual version information is not known (instead of null).
64      */

65     public static Version unknownVersion() { return UNKNOWN_VERSION; }
66
67     /**
68      * @since 2.7 to replace misspelled {@link #isUknownVersion()}
69      */

70     public boolean isUnknownVersion() { return (this == UNKNOWN_VERSION); }
71
72     public boolean isSnapshot() { return (_snapshotInfo != null && _snapshotInfo.length() > 0); }
73
74     /**
75      * @deprecated Since 2.7 use correctly spelled method {@link #isUnknownVersion()}
76      */

77     @Deprecated
78     public boolean isUknownVersion() { return isUnknownVersion(); }
79
80     public int getMajorVersion() { return _majorVersion; }
81     public int getMinorVersion() { return _minorVersion; }
82     public int getPatchLevel() { return _patchLevel; }
83
84     public String getGroupId() { return _groupId; }
85     public String getArtifactId() { return _artifactId; }
86
87     public String toFullString() {
88         return _groupId + '/' + _artifactId + '/' + toString();
89     }
90
91     @Override public String toString() {
92         StringBuilder sb = new StringBuilder();
93         sb.append(_majorVersion).append('.');
94         sb.append(_minorVersion).append('.');
95         sb.append(_patchLevel);
96         if (isSnapshot()) {
97             sb.append('-').append(_snapshotInfo);
98         }
99         return sb.toString();
100     }
101
102     @Override public int hashCode() {
103         return _artifactId.hashCode() ^ _groupId.hashCode() + _majorVersion - _minorVersion + _patchLevel;
104     }
105
106     @Override
107     public boolean equals(Object o)
108     {
109         if (o == thisreturn true;
110         if (o == nullreturn false;
111         if (o.getClass() != getClass()) return false;
112         Version other = (Version) o;
113         return (other._majorVersion == _majorVersion)
114             && (other._minorVersion == _minorVersion)
115             && (other._patchLevel == _patchLevel)
116             && other._artifactId.equals(_artifactId)
117             && other._groupId.equals(_groupId)
118             ;
119     }
120
121     @Override
122     public int compareTo(Version other)
123     {
124         if (other == thisreturn 0;
125         
126         int diff = _groupId.compareTo(other._groupId);
127         if (diff == 0) {
128             diff = _artifactId.compareTo(other._artifactId);
129             if (diff == 0) {
130                 diff = _majorVersion - other._majorVersion;
131                 if (diff == 0) {
132                     diff = _minorVersion - other._minorVersion;
133                     if (diff == 0) {
134                         diff = _patchLevel - other._patchLevel;
135                     }
136                 }
137             }
138         }
139         return diff;
140     }
141 }
142