Tatu Saloranta | 68bb83d | 2013-04-19 10:41:15 -0700 | [diff] [blame] | 1 | /* Jackson JSON-processor. |
| 2 | * |
| 3 | * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi |
| 4 | */ |
| 5 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 6 | package com.fasterxml.jackson.core; |
| 7 | |
| 8 | /** |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 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. |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 15 | */ |
| 16 | public class Version |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 17 | implements Comparable<Version>, java.io.Serializable |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 18 | { |
Tatu Saloranta | 76d903a | 2012-10-06 13:41:11 -0700 | [diff] [blame] | 19 | private static final long serialVersionUID = 1L; |
| 20 | |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 21 | private final static Version UNKNOWN_VERSION = new Version(0, 0, 0, null, null, null); |
Cowtowncoder | adb9e68 | 2015-08-18 12:27:19 -0700 | [diff] [blame] | 22 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 23 | protected final int _majorVersion; |
| 24 | |
| 25 | protected final int _minorVersion; |
| 26 | |
| 27 | protected final int _patchLevel; |
| 28 | |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 29 | protected final String _groupId; |
Cowtowncoder | adb9e68 | 2015-08-18 12:27:19 -0700 | [diff] [blame] | 30 | |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 31 | protected final String _artifactId; |
Cowtowncoder | adb9e68 | 2015-08-18 12:27:19 -0700 | [diff] [blame] | 32 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Additional information for snapshot versions; null for non-snapshot |
| 35 | * (release) versions. |
| 36 | */ |
| 37 | protected final String _snapshotInfo; |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * @deprecated Use variant that takes group and artifact ids |
Tatu Saloranta | 76d903a | 2012-10-06 13:41:11 -0700 | [diff] [blame] | 41 | * |
| 42 | * @since 2.1 |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 43 | */ |
| 44 | @Deprecated |
| 45 | public Version(int major, int minor, int patchLevel, String snapshotInfo) |
| 46 | { |
| 47 | this(major, minor, patchLevel, snapshotInfo, null, null); |
| 48 | } |
Cowtowncoder | adb9e68 | 2015-08-18 12:27:19 -0700 | [diff] [blame] | 49 | |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 50 | public Version(int major, int minor, int patchLevel, String snapshotInfo, |
| 51 | String groupId, String artifactId) |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 52 | { |
| 53 | _majorVersion = major; |
| 54 | _minorVersion = minor; |
| 55 | _patchLevel = patchLevel; |
| 56 | _snapshotInfo = snapshotInfo; |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 57 | _groupId = (groupId == null) ? "" : groupId; |
| 58 | _artifactId = (artifactId == null) ? "" : artifactId; |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 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 | |
Cowtowncoder | 2246541 | 2015-08-18 12:25:05 -0700 | [diff] [blame] | 67 | /** |
| 68 | * @since 2.7 to replace misspelled {@link #isUknownVersion()} |
| 69 | */ |
| 70 | public boolean isUnknownVersion() { return (this == UNKNOWN_VERSION); } |
Tatu Saloranta | 25e7dd0 | 2016-03-09 14:42:30 -0800 | [diff] [blame] | 71 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 72 | public boolean isSnapshot() { return (_snapshotInfo != null && _snapshotInfo.length() > 0); } |
Cowtowncoder | 2246541 | 2015-08-18 12:25:05 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * @deprecated Since 2.7 use correctly spelled method {@link #isUnknownVersion()} |
| 76 | */ |
| 77 | @Deprecated |
Cowtowncoder | adb9e68 | 2015-08-18 12:27:19 -0700 | [diff] [blame] | 78 | public boolean isUknownVersion() { return isUnknownVersion(); } |
| 79 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 80 | public int getMajorVersion() { return _majorVersion; } |
| 81 | public int getMinorVersion() { return _minorVersion; } |
| 82 | public int getPatchLevel() { return _patchLevel; } |
| 83 | |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 84 | public String getGroupId() { return _groupId; } |
| 85 | public String getArtifactId() { return _artifactId; } |
Cowtowncoder | adb9e68 | 2015-08-18 12:27:19 -0700 | [diff] [blame] | 86 | |
Tatu Saloranta | a0af4f4 | 2013-01-11 10:36:44 -0800 | [diff] [blame] | 87 | public String toFullString() { |
Gonçalo Silva | d1d4382 | 2014-01-25 17:05:43 +0000 | [diff] [blame] | 88 | return _groupId + '/' + _artifactId + '/' + toString(); |
Tatu Saloranta | a0af4f4 | 2013-01-11 10:36:44 -0800 | [diff] [blame] | 89 | } |
Cowtowncoder | adb9e68 | 2015-08-18 12:27:19 -0700 | [diff] [blame] | 90 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 91 | @Override public String toString() { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 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 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 102 | @Override public int hashCode() { |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 103 | return _artifactId.hashCode() ^ _groupId.hashCode() + _majorVersion - _minorVersion + _patchLevel; |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 104 | } |
| 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 Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 115 | && (other._patchLevel == _patchLevel) |
| 116 | && other._artifactId.equals(_artifactId) |
| 117 | && other._groupId.equals(_groupId) |
| 118 | ; |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Tatu Saloranta | 2343927 | 2013-04-06 20:44:18 -0700 | [diff] [blame] | 121 | @Override |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 122 | public int compareTo(Version other) |
| 123 | { |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 124 | if (other == this) return 0; |
| 125 | |
| 126 | int diff = _groupId.compareTo(other._groupId); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 127 | if (diff == 0) { |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 128 | diff = _artifactId.compareTo(other._artifactId); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 129 | if (diff == 0) { |
Tatu Saloranta | 96e131f | 2011-12-28 22:50:23 -0800 | [diff] [blame] | 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 | } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | return diff; |
| 140 | } |
| 141 | } |