@@ -10,6 +10,7 @@ public class Version implements Comparable<Version> {
1010
1111 @ Nullable private final String originalString ;
1212 @ Nonnull private final List <Integer > subversionNumbers = new ArrayList <>();
13+ @ Nonnull private final List <Integer > subversionNumbersWithoutTrailingZeros = new ArrayList <>();
1314 @ Nonnull private String suffix = "" ;
1415
1516 /**
@@ -122,8 +123,9 @@ public boolean isHigherThan(String otherVersion) {
122123 * @see #isHigherThan(String otherVersion)
123124 */
124125 public boolean isHigherThan (Version otherVersion ) {
125- int subversionsResult =
126- VersionComparator .compareSubversionNumbers (subversionNumbers , otherVersion .subversionNumbers );
126+ int subversionsResult = VersionComparator .compareSubversionNumbers (
127+ subversionNumbersWithoutTrailingZeros ,
128+ otherVersion .subversionNumbersWithoutTrailingZeros );
127129 if (subversionsResult != 0 ) {
128130 return subversionsResult > 0 ;
129131 }
@@ -151,8 +153,10 @@ public boolean isLowerThan(String otherVersion) {
151153 * @see #isLowerThan(String otherVersion)
152154 */
153155 public boolean isLowerThan (Version otherVersion ) {
154- int subversionsResult =
155- VersionComparator .compareSubversionNumbers (subversionNumbers , otherVersion .subversionNumbers );
156+ int subversionsResult = VersionComparator .compareSubversionNumbers (
157+ subversionNumbersWithoutTrailingZeros ,
158+ otherVersion .subversionNumbersWithoutTrailingZeros
159+ );
156160 if (subversionsResult != 0 ) {
157161 return subversionsResult < 0 ;
158162 }
@@ -180,8 +184,10 @@ public boolean isEqual(String otherVersion) {
180184 * @see #isEqual(String otherVersion)
181185 */
182186 public boolean isEqual (Version otherVersion ) {
183- return VersionComparator .compareSubversionNumbers (subversionNumbers , otherVersion .subversionNumbers ) == 0
184- && VersionComparator .compareSuffix (suffix , otherVersion .suffix ) == 0 ;
187+ return VersionComparator .compareSubversionNumbers (
188+ subversionNumbersWithoutTrailingZeros ,
189+ otherVersion .subversionNumbersWithoutTrailingZeros
190+ ) == 0 && VersionComparator .compareSuffix (suffix , otherVersion .suffix ) == 0 ;
185191 }
186192
187193 /**
@@ -231,8 +237,10 @@ public boolean isAtLeast(String otherVersion, boolean ignoreSuffix) {
231237 * @see #isAtLeast(String otherVersion, boolean ignoreSuffix)
232238 */
233239 public boolean isAtLeast (Version otherVersion , boolean ignoreSuffix ) {
234- int subversionsResult =
235- VersionComparator .compareSubversionNumbers (subversionNumbers , otherVersion .subversionNumbers );
240+ int subversionsResult = VersionComparator .compareSubversionNumbers (
241+ subversionNumbersWithoutTrailingZeros ,
242+ otherVersion .subversionNumbersWithoutTrailingZeros
243+ );
236244 if (subversionsResult == 0 && !ignoreSuffix ) {
237245 return VersionComparator .compareSuffix (suffix , otherVersion .suffix ) >= 0 ;
238246 }
@@ -267,20 +275,38 @@ private void initVersion() {
267275 }
268276 }
269277 }
278+ subversionNumbersWithoutTrailingZeros .addAll (subversionNumbers );
279+ while (subversionNumbersWithoutTrailingZeros .lastIndexOf (0 ) >= 0 ) {
280+ subversionNumbersWithoutTrailingZeros .remove (subversionNumbersWithoutTrailingZeros .lastIndexOf (0 ));
281+ }
270282 if (suffixSb != null ) suffix = suffixSb .toString ();
271283 }
272284 }
273285
274286 @ Override
275- public int compareTo (@ Nonnull Version version ) {
276- if (this .isEqual (version )) return 0 ;
277- else if (this .isLowerThan (version )) return -1 ;
278- else return 1 ;
287+ final public int compareTo (@ Nonnull Version version ) {
288+ if (this .isEqual (version )) return 0 ;
289+ else if (this .isLowerThan (version )) return -1 ;
290+ else return 1 ;
279291 }
280292
281293 @ Override
282- public boolean equals (Object o ) {
283- if (o instanceof Version && this .isEqual ((Version )o )) return true ;
284- else return super .equals (o );
294+ final public boolean equals (Object o ) {
295+ if (o instanceof Version && this .isEqual ((Version ) o )) return true ;
296+ else return super .equals (o );
297+ }
298+
299+ @ Override
300+ final public int hashCode () {
301+ final int prime = 31 ;
302+ int hash = 1 ;
303+ hash = prime * hash + subversionNumbersWithoutTrailingZeros .hashCode ();
304+ if (suffix .isEmpty ()) return hash ;
305+
306+ int releaseQualifier = VersionComparator .qualifierToNumber (suffix );
307+ int releaseQualifierVersion = VersionComparator .preReleaseVersion (suffix , releaseQualifier );
308+ hash = prime * hash + releaseQualifier ;
309+ hash = prime * hash + releaseQualifierVersion ;
310+ return hash ;
285311 }
286312}
0 commit comments