001/* 002 * Copyright (C) 2015-2021 KeepSafe Software 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.getkeepsafe.dexcount; 017 018import org.jetbrains.annotations.NotNull; 019 020import java.util.regex.Matcher; 021import java.util.regex.Pattern; 022 023/** 024 * Represents a Gradle version number. 025 * 026 * Despite its obviousness and the seemingly-public package "org.gradle.util", 027 * the built-in [org.gradle.util.GradleVersion] is in fact an internal class, and 028 * I really can't be bothered trying to fix things whenever Gradle decides to break 029 * their customers yet again by repackaging an obviously-useful utility. 030 * 031 * Android tools define [com.android.ide.common.repository.GradleVersion], but I 032 * can't find anything about in what version that class was introduced or whether 033 * it's a stable API. 034 * 035 * So, here we are, reinventing the wheel yet again. 036 * 037 * No, I'm not still bitter about StyledTextOutput, why do you ask? 038 */ 039public class GradleVersion implements Comparable<GradleVersion> { 040 private static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+)\\.(\\d+).*$"); 041 042 private final int major; 043 private final int minor; 044 045 public GradleVersion(int major, int minor) { 046 this.major = major; 047 this.minor = minor; 048 } 049 050 public static GradleVersion parse(String versionString) { 051 Matcher matcher = VERSION_PATTERN.matcher(versionString); 052 if (!matcher.matches()) { 053 throw new IllegalArgumentException("Invalid Gradle version: " + versionString); 054 } 055 String majorString = matcher.group(1); 056 String minorString = matcher.group(2); 057 return new GradleVersion(Integer.parseInt(majorString), Integer.parseInt(minorString)); 058 } 059 060 @Override 061 public int compareTo(@NotNull GradleVersion other) { 062 int majorCmp = Integer.compare(major, other.major); 063 if (majorCmp != 0) { 064 return majorCmp; 065 } 066 return Integer.compare(minor, other.minor); 067 } 068 069 @Override 070 public String toString() { 071 return "" + major + "." + minor; 072 } 073 074 @Override 075 public boolean equals(Object o) { 076 if (this == o) return true; 077 if (o == null || getClass() != o.getClass()) return false; 078 079 GradleVersion that = (GradleVersion) o; 080 081 if (major != that.major) return false; 082 return minor == that.minor; 083 } 084 085 @Override 086 public int hashCode() { 087 int result = major; 088 result = 31 * result + minor; 089 return result; 090 } 091}