blob: 02f3de83dc630deea4f5a3ed11b96ec41d7af0da [file] [log] [blame]
Tatu Saloranta68bb83d2013-04-19 10:41:15 -07001/* Jackson JSON-processor.
2 *
3 * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi
4 */
5
Tatu Salorantaf15531c2011-12-22 23:00:40 -08006package com.fasterxml.jackson.core;
7
8/**
Tatu Saloranta96e131f2011-12-28 22:50:23 -08009 * 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.
Tatu Salorantaf15531c2011-12-22 23:00:40 -080015 */
16public class Version
Tatu Saloranta32e4e912014-01-26 19:59:28 -080017 implements Comparable<Version>, java.io.Serializable
Tatu Salorantaf15531c2011-12-22 23:00:40 -080018{
Tatu Saloranta76d903a2012-10-06 13:41:11 -070019 private static final long serialVersionUID = 1L;
20
Tatu Saloranta96e131f2011-12-28 22:50:23 -080021 private final static Version UNKNOWN_VERSION = new Version(0, 0, 0, null, null, null);
Cowtowncoderadb9e682015-08-18 12:27:19 -070022
Tatu Salorantaf15531c2011-12-22 23:00:40 -080023 protected final int _majorVersion;
24
25 protected final int _minorVersion;
26
27 protected final int _patchLevel;
28
Tatu Saloranta96e131f2011-12-28 22:50:23 -080029 protected final String _groupId;
Cowtowncoderadb9e682015-08-18 12:27:19 -070030
Tatu Saloranta96e131f2011-12-28 22:50:23 -080031 protected final String _artifactId;
Cowtowncoderadb9e682015-08-18 12:27:19 -070032
Tatu Salorantaf15531c2011-12-22 23:00:40 -080033 /**
34 * Additional information for snapshot versions; null for non-snapshot
35 * (release) versions.
36 */
37 protected final String _snapshotInfo;
Tatu Saloranta96e131f2011-12-28 22:50:23 -080038
39 /**
40 * @deprecated Use variant that takes group and artifact ids
Tatu Saloranta76d903a2012-10-06 13:41:11 -070041 *
42 * @since 2.1
Tatu Saloranta96e131f2011-12-28 22:50:23 -080043 */
44 @Deprecated
45 public Version(int major, int minor, int patchLevel, String snapshotInfo)
46 {
47 this(major, minor, patchLevel, snapshotInfo, null, null);
48 }
Cowtowncoderadb9e682015-08-18 12:27:19 -070049
Tatu Saloranta96e131f2011-12-28 22:50:23 -080050 public Version(int major, int minor, int patchLevel, String snapshotInfo,
51 String groupId, String artifactId)
Tatu Salorantaf15531c2011-12-22 23:00:40 -080052 {
53 _majorVersion = major;
54 _minorVersion = minor;
55 _patchLevel = patchLevel;
56 _snapshotInfo = snapshotInfo;
Tatu Saloranta96e131f2011-12-28 22:50:23 -080057 _groupId = (groupId == null) ? "" : groupId;
58 _artifactId = (artifactId == null) ? "" : artifactId;
Tatu Salorantaf15531c2011-12-22 23:00:40 -080059 }
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
Cowtowncoder22465412015-08-18 12:25:05 -070067 /**
68 * @since 2.7 to replace misspelled {@link #isUknownVersion()}
69 */
70 public boolean isUnknownVersion() { return (this == UNKNOWN_VERSION); }
Tatu Saloranta25e7dd02016-03-09 14:42:30 -080071
Tatu Salorantaf15531c2011-12-22 23:00:40 -080072 public boolean isSnapshot() { return (_snapshotInfo != null && _snapshotInfo.length() > 0); }
Cowtowncoder22465412015-08-18 12:25:05 -070073
74 /**
75 * @deprecated Since 2.7 use correctly spelled method {@link #isUnknownVersion()}
76 */
77 @Deprecated
Cowtowncoderadb9e682015-08-18 12:27:19 -070078 public boolean isUknownVersion() { return isUnknownVersion(); }
79
Tatu Salorantaf15531c2011-12-22 23:00:40 -080080 public int getMajorVersion() { return _majorVersion; }
81 public int getMinorVersion() { return _minorVersion; }
82 public int getPatchLevel() { return _patchLevel; }
83
Tatu Saloranta96e131f2011-12-28 22:50:23 -080084 public String getGroupId() { return _groupId; }
85 public String getArtifactId() { return _artifactId; }
Cowtowncoderadb9e682015-08-18 12:27:19 -070086
Tatu Salorantaa0af4f42013-01-11 10:36:44 -080087 public String toFullString() {
Gonçalo Silvad1d43822014-01-25 17:05:43 +000088 return _groupId + '/' + _artifactId + '/' + toString();
Tatu Salorantaa0af4f42013-01-11 10:36:44 -080089 }
Cowtowncoderadb9e682015-08-18 12:27:19 -070090
Tatu Saloranta32e4e912014-01-26 19:59:28 -080091 @Override public String toString() {
Tatu Salorantaf15531c2011-12-22 23:00:40 -080092 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
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800102 @Override public int hashCode() {
Tatu Saloranta96e131f2011-12-28 22:50:23 -0800103 return _artifactId.hashCode() ^ _groupId.hashCode() + _majorVersion - _minorVersion + _patchLevel;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800104 }
105
106 @Override
107 public boolean equals(Object o)
108 {
109 if (o == this) return true;
110 if (o == null) return false;
111 if (o.getClass() != getClass()) return false;
112 Version other = (Version) o;
113 return (other._majorVersion == _majorVersion)
114 && (other._minorVersion == _minorVersion)
Tatu Saloranta96e131f2011-12-28 22:50:23 -0800115 && (other._patchLevel == _patchLevel)
116 && other._artifactId.equals(_artifactId)
117 && other._groupId.equals(_groupId)
118 ;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800119 }
120
Tatu Saloranta23439272013-04-06 20:44:18 -0700121 @Override
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800122 public int compareTo(Version other)
123 {
Tatu Saloranta96e131f2011-12-28 22:50:23 -0800124 if (other == this) return 0;
125
126 int diff = _groupId.compareTo(other._groupId);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800127 if (diff == 0) {
Tatu Saloranta96e131f2011-12-28 22:50:23 -0800128 diff = _artifactId.compareTo(other._artifactId);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800129 if (diff == 0) {
Tatu Saloranta96e131f2011-12-28 22:50:23 -0800130 diff = _majorVersion - other._majorVersion;
131 if (diff == 0) {
132 diff = _minorVersion - other._minorVersion;
133 if (diff == 0) {
134 diff = _patchLevel - other._patchLevel;
135 }
136 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800137 }
138 }
139 return diff;
140 }
141}