|
| 1 | +package datadog.environment; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * This class represents a Java version according the String Naming Convention. |
| 8 | + * |
| 9 | + * @see <a href="https://www.oracle.com/java/technologies/javase/versioning-naming.html">String |
| 10 | + * Naming Convention</a> |
| 11 | + */ |
| 12 | +final class JavaVersion { |
| 13 | + final int major; |
| 14 | + final int minor; |
| 15 | + final int update; |
| 16 | + |
| 17 | + JavaVersion(int major, int minor, int update) { |
| 18 | + this.major = major; |
| 19 | + this.minor = minor; |
| 20 | + this.update = update; |
| 21 | + } |
| 22 | + |
| 23 | + static JavaVersion getRuntimeVersion() { |
| 24 | + return parseJavaVersion(SystemProperties.getOrDefault("java.version", "")); |
| 25 | + } |
| 26 | + |
| 27 | + static JavaVersion parseJavaVersion(String javaVersion) { |
| 28 | + // Remove pre-release part, usually -ea |
| 29 | + final int indexOfDash = javaVersion.indexOf('-'); |
| 30 | + if (indexOfDash >= 0) { |
| 31 | + javaVersion = javaVersion.substring(0, indexOfDash); |
| 32 | + } |
| 33 | + |
| 34 | + int major = 0; |
| 35 | + int minor = 0; |
| 36 | + int update = 0; |
| 37 | + |
| 38 | + try { |
| 39 | + List<Integer> nums = splitDigits(javaVersion); |
| 40 | + major = nums.get(0); |
| 41 | + |
| 42 | + // for java 1.6/1.7/1.8 |
| 43 | + if (major == 1) { |
| 44 | + major = nums.get(1); |
| 45 | + minor = nums.get(2); |
| 46 | + update = nums.get(3); |
| 47 | + } else { |
| 48 | + minor = nums.get(1); |
| 49 | + update = nums.get(2); |
| 50 | + } |
| 51 | + } catch (NumberFormatException | IndexOutOfBoundsException e) { |
| 52 | + // unable to parse version string - do nothing |
| 53 | + } |
| 54 | + return new JavaVersion(major, minor, update); |
| 55 | + } |
| 56 | + |
| 57 | + /* The method splits java version string by digits. Delimiters are: dot, underscore and plus */ |
| 58 | + private static List<Integer> splitDigits(String str) { |
| 59 | + List<Integer> results = new ArrayList<>(); |
| 60 | + |
| 61 | + int len = str.length(); |
| 62 | + int value = 0; |
| 63 | + for (int i = 0; i < len; i++) { |
| 64 | + char ch = str.charAt(i); |
| 65 | + if (ch >= '0' && ch <= '9') { |
| 66 | + value = value * 10 + (ch - '0'); |
| 67 | + } else if (ch == '.' || ch == '_' || ch == '+') { |
| 68 | + results.add(value); |
| 69 | + value = 0; |
| 70 | + } else { |
| 71 | + throw new NumberFormatException(); |
| 72 | + } |
| 73 | + } |
| 74 | + results.add(value); |
| 75 | + return results; |
| 76 | + } |
| 77 | + |
| 78 | + public boolean is(int major) { |
| 79 | + return this.major == major; |
| 80 | + } |
| 81 | + |
| 82 | + public boolean is(int major, int minor) { |
| 83 | + return this.major == major && this.minor == minor; |
| 84 | + } |
| 85 | + |
| 86 | + public boolean is(int major, int minor, int update) { |
| 87 | + return this.major == major && this.minor == minor && this.update == update; |
| 88 | + } |
| 89 | + |
| 90 | + public boolean isAtLeast(int major, int minor, int update) { |
| 91 | + return isAtLeast(this.major, this.minor, this.update, major, minor, update); |
| 92 | + } |
| 93 | + |
| 94 | + public boolean isBetween( |
| 95 | + int fromMajor, int fromMinor, int fromUpdate, int toMajor, int toMinor, int toUpdate) { |
| 96 | + return isAtLeast(toMajor, toMinor, toUpdate, fromMajor, fromMinor, fromUpdate) |
| 97 | + && isAtLeast(fromMajor, fromMinor, fromUpdate) |
| 98 | + && !isAtLeast(toMajor, toMinor, toUpdate); |
| 99 | + } |
| 100 | + |
| 101 | + private static boolean isAtLeast( |
| 102 | + int major, int minor, int update, int atLeastMajor, int atLeastMinor, int atLeastUpdate) { |
| 103 | + return (major > atLeastMajor) |
| 104 | + || (major == atLeastMajor && minor > atLeastMinor) |
| 105 | + || (major == atLeastMajor && minor == atLeastMinor && update >= atLeastUpdate); |
| 106 | + } |
| 107 | +} |
0 commit comments