|
| 1 | +package com.nuix.javaenginesimple; |
| 2 | + |
| 3 | +import java.util.regex.Pattern; |
| 4 | + |
| 5 | +/*** |
| 6 | + * Assists in representing a Nuix version in object form to assist with comparing two versions. This allows for things such as |
| 7 | + * only executing chunks of code if the version meets a requirement.<br> |
| 8 | + * Ruby example: |
| 9 | + * <pre> |
| 10 | + * {@code |
| 11 | + * current_version = NuixVersion.new(NUIX_VERSION) |
| 12 | + * if current_version.isLessThan("7.8.0.10") |
| 13 | + * puts "Sorry your version of Nuix is below the minimum required version of 7.8.0.10" |
| 14 | + * exit 1 |
| 15 | + * end |
| 16 | + * } |
| 17 | + * </pre> |
| 18 | + * @author Jason Wells |
| 19 | + * |
| 20 | + */ |
| 21 | +public class NuixVersion implements Comparable<NuixVersion> { |
| 22 | + private static Pattern previewVersionInfoRemovalPattern = Pattern.compile("[^0-9\\.].*$"); |
| 23 | + |
| 24 | + private int major = 0; |
| 25 | + private int minor = 0; |
| 26 | + private int bugfix = 0; |
| 27 | + private int build = 0; |
| 28 | + |
| 29 | + /*** |
| 30 | + * Creates a new instance defaulting to version 0.0.0 |
| 31 | + */ |
| 32 | + public NuixVersion(){ |
| 33 | + this(0,0,0,0); |
| 34 | + } |
| 35 | + |
| 36 | + /*** |
| 37 | + * Creates a new instance using the provided major version: major.0.0.0 |
| 38 | + * @param majorVersion The major version number |
| 39 | + */ |
| 40 | + public NuixVersion(int majorVersion){ |
| 41 | + this(majorVersion,0,0,0); |
| 42 | + } |
| 43 | + /*** |
| 44 | + * Creates a new instance using the provided major and minor versions: major.minor.0.0 |
| 45 | + * @param majorVersion The major version number |
| 46 | + * @param minorVersion The minor version number |
| 47 | + */ |
| 48 | + public NuixVersion(int majorVersion, int minorVersion){ |
| 49 | + this(majorVersion,minorVersion,0,0); |
| 50 | + } |
| 51 | + /*** |
| 52 | + * Creates a new instance using the provided major, minor and bugfix versions: major.minor.bugfix.0 |
| 53 | + * @param majorVersion The major version number |
| 54 | + * @param minorVersion The minor version number |
| 55 | + * @param bugfixVersion The bugfix version number |
| 56 | + */ |
| 57 | + public NuixVersion(int majorVersion, int minorVersion, int bugfixVersion){ |
| 58 | + this(majorVersion,minorVersion,bugfixVersion,0); |
| 59 | + } |
| 60 | + |
| 61 | + /*** |
| 62 | + * Creates a new instance using the provided major, minor, bugfix and build versions: major.minor.bugfix.build |
| 63 | + * @param majorVersion The major version number |
| 64 | + * @param minorVersion The minor version number |
| 65 | + * @param bugfixVersion The bugfix version number |
| 66 | + * @param buildVersion The build version number |
| 67 | + */ |
| 68 | + public NuixVersion(int majorVersion, int minorVersion, int bugfixVersion, int buildVersion){ |
| 69 | + major = majorVersion; |
| 70 | + minor = minorVersion; |
| 71 | + bugfix = bugfixVersion; |
| 72 | + build = buildVersion; |
| 73 | + } |
| 74 | + |
| 75 | + /*** |
| 76 | + * Parses a version string into a NuixVersion instance. Supports values such as: 6, 6.2, 6.2.0, 6.2.1-preview6, 7.8.0.10 <br> |
| 77 | + * When providing a version string such as "6.2.1-preview6", "-preview6" will be trimmed off before parsing. |
| 78 | + * @param versionString The version string to parse. |
| 79 | + * @return A NuixVersion instance representing the supplied version string, if there is an error parsing the provided value will return |
| 80 | + * an instance representing 100.0.0 |
| 81 | + */ |
| 82 | + public static NuixVersion parse(String versionString){ |
| 83 | + try { |
| 84 | + String[] versionParts = NuixVersion.previewVersionInfoRemovalPattern.matcher(versionString.trim()).replaceAll("").split("\\."); |
| 85 | + int[] versionPartInts = new int[versionParts.length]; |
| 86 | + for(int i=0;i<versionParts.length;i++){ |
| 87 | + versionPartInts[i] = Integer.parseInt(versionParts[i]); |
| 88 | + } |
| 89 | + switch(versionParts.length){ |
| 90 | + case 1: |
| 91 | + return new NuixVersion(versionPartInts[0]); |
| 92 | + case 2: |
| 93 | + return new NuixVersion(versionPartInts[0],versionPartInts[1]); |
| 94 | + case 3: |
| 95 | + return new NuixVersion(versionPartInts[0],versionPartInts[1],versionPartInts[2]); |
| 96 | + case 4: |
| 97 | + return new NuixVersion(versionPartInts[0],versionPartInts[1],versionPartInts[2],versionPartInts[3]); |
| 98 | + default: |
| 99 | + return new NuixVersion(); |
| 100 | + } |
| 101 | + }catch(Exception exc){ |
| 102 | + System.out.println("Error while parsing version: "+versionString); |
| 103 | + System.out.println("Pretending version is 100.0.0.0"); |
| 104 | + return new NuixVersion(100,0,0,0); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /*** |
| 109 | + * Gets the determined major portion of this version instance (X.0.0.0) |
| 110 | + * @return The determined major portion of version |
| 111 | + */ |
| 112 | + public int getMajor() { |
| 113 | + return major; |
| 114 | + } |
| 115 | + |
| 116 | + /*** |
| 117 | + * Sets the major portion of this version instance (X.0.0.0) |
| 118 | + * @param major The major version value |
| 119 | + */ |
| 120 | + public void setMajor(int major) { |
| 121 | + this.major = major; |
| 122 | + } |
| 123 | + |
| 124 | + /*** |
| 125 | + * Gets the determined minor portion of this version instance (0.X.0.0) |
| 126 | + * @return The determined minor portion of version |
| 127 | + */ |
| 128 | + public int getMinor() { |
| 129 | + return minor; |
| 130 | + } |
| 131 | + |
| 132 | + /*** |
| 133 | + * Sets the minor portion of this version instance (0.X.0.0) |
| 134 | + * @param minor The minor version value |
| 135 | + */ |
| 136 | + public void setMinor(int minor) { |
| 137 | + this.minor = minor; |
| 138 | + } |
| 139 | + |
| 140 | + /*** |
| 141 | + * Gets the determined bugfix portion of this version instance (0.0.x.0) |
| 142 | + * @return The determined bugfix portion of version |
| 143 | + */ |
| 144 | + public int getBugfix() { |
| 145 | + return bugfix; |
| 146 | + } |
| 147 | + |
| 148 | + /*** |
| 149 | + * Sets the determined bugfix portion of this version instance (0.0.x.0) |
| 150 | + * @param bugfix The determined bugfix portion of version |
| 151 | + */ |
| 152 | + public void setBugfix(int bugfix) { |
| 153 | + this.bugfix = bugfix; |
| 154 | + } |
| 155 | + |
| 156 | + /*** |
| 157 | + * Gets the determined build portion of this version instance (0.0.0.x) |
| 158 | + * @return The determined build portion of version |
| 159 | + */ |
| 160 | + public int getBuild() { |
| 161 | + return build; |
| 162 | + } |
| 163 | + |
| 164 | + /*** |
| 165 | + * Sets the build portion of this version instance (0.0.0.x) |
| 166 | + * @param build The build version value |
| 167 | + */ |
| 168 | + public void setBuild(int build) { |
| 169 | + this.build = build; |
| 170 | + } |
| 171 | + |
| 172 | + /*** |
| 173 | + * Determines whether another instance's version is less than this instance |
| 174 | + * @param other The other instance to compare against |
| 175 | + * @return True if the other instance is a lower version, false otherwise |
| 176 | + */ |
| 177 | + public boolean isLessThan(NuixVersion other){ |
| 178 | + return this.compareTo(other) < 0; |
| 179 | + } |
| 180 | + /*** |
| 181 | + * Determines whether another instance's version is greater than or equal to this instance |
| 182 | + * @param other The other instance to compare against |
| 183 | + * @return True if the other instance is greater than or equal to this instance, false otherwise |
| 184 | + */ |
| 185 | + public boolean isAtLeast(NuixVersion other){ |
| 186 | + return this.compareTo(other) >= 0; |
| 187 | + } |
| 188 | + /*** |
| 189 | + * Determines whether another instance's version is greater than this instance |
| 190 | + * @param other The other instance to compare against |
| 191 | + * @return True if the other instance is greater than this instance, false otherwise |
| 192 | + */ |
| 193 | + public boolean isGreaterThan(NuixVersion other){ |
| 194 | + return this.compareTo(other) > 0; |
| 195 | + } |
| 196 | + /*** |
| 197 | + * Determines whether another instance's version is greater than this instance |
| 198 | + * @param other The other instance to compare against |
| 199 | + * @return True if the other instance is greater than this instance, false otherwise |
| 200 | + */ |
| 201 | + public boolean isGreaterThan(String other){ |
| 202 | + return this.compareTo(NuixVersion.parse(other)) > 0; |
| 203 | + } |
| 204 | + /*** |
| 205 | + * Determines whether another instance's version is equal to this instance |
| 206 | + * @param other The other instance to compare against |
| 207 | + * @return True if the other instance is greater than this instance, false otherwise |
| 208 | + */ |
| 209 | + public boolean isEqualTo(NuixVersion other){ |
| 210 | + return this.compareTo(other) == 0; |
| 211 | + } |
| 212 | + /*** |
| 213 | + * Determines whether another instance's version is equal to this instance |
| 214 | + * @param other The other instance to compare against |
| 215 | + * @return True if the other instance is equal to this instance (major, minor and release are the same), false otherwise |
| 216 | + */ |
| 217 | + public boolean isEqualTo(String other){ |
| 218 | + return this.compareTo(NuixVersion.parse(other)) == 0; |
| 219 | + } |
| 220 | + /*** |
| 221 | + * Determines whether another instance's version is less than this instance |
| 222 | + * @param other A version string to compare against |
| 223 | + * @return True if the other instance is a lower version, false otherwise |
| 224 | + */ |
| 225 | + public boolean isLessThan(String other){ |
| 226 | + return isLessThan(parse(other)); |
| 227 | + } |
| 228 | + /*** |
| 229 | + * Determines whether another instance's version is greater than or equal to this instance |
| 230 | + * @param other A version string to compare against |
| 231 | + * @return True if the other instance is greater than or equal to this instance, false otherwise |
| 232 | + */ |
| 233 | + public boolean isAtLeast(String other){ |
| 234 | + return isAtLeast(parse(other)); |
| 235 | + } |
| 236 | + |
| 237 | + /*** |
| 238 | + * Provides comparison logic when comparing two instances. |
| 239 | + */ |
| 240 | + @Override |
| 241 | + public int compareTo(NuixVersion other) { |
| 242 | + if(this.major == other.major){ |
| 243 | + if(this.minor == other.minor){ |
| 244 | + if(this.bugfix == other.bugfix) { |
| 245 | + return Integer.compare(this.build, other.build); |
| 246 | + } else { |
| 247 | + return Integer.compare(this.bugfix, other.bugfix); |
| 248 | + } |
| 249 | + } else{ |
| 250 | + return Integer.compare(this.minor, other.minor); |
| 251 | + } |
| 252 | + } else{ |
| 253 | + return Integer.compare(this.major, other.major); |
| 254 | + } |
| 255 | + |
| 256 | + } |
| 257 | + |
| 258 | + /*** |
| 259 | + * Converts this instance back to a version string from its components, such as: "7.8.0.10" |
| 260 | + */ |
| 261 | + @Override |
| 262 | + public String toString(){ |
| 263 | + return Integer.toString(this.major) + "." + |
| 264 | + Integer.toString(this.minor) + "." + |
| 265 | + Integer.toString(this.bugfix) + "." + |
| 266 | + Integer.toString(this.build); |
| 267 | + } |
| 268 | +} |
0 commit comments