I wanted to change the system processing depending on the version of the task management service, so I implemented an implementation that compares the two version strings. It is a method to treat the version as a character string, divide it by a period, and then convert each part to Integer and compare.
--For example, you want to branch the process depending on whether the version of a service is 2.5 or higher and less than 2.5. --The version needs to consider three parts separated by periods of x.y.z, such as 2.5.0. --There are some ways to write versions such as 1.3-SNAPSHOT, 1.3a, but this time we will not consider them. --The version that becomes the branch point is fixed, and the character string of the service version is read from the configuration file.
The implementation was written with reference to this site. For more detailed discussions, please refer to those who are interested.
I implemented the hashCode calculation myself. (I don't know if this is okay)
Version.java
package com.example;
import java.util.Arrays;
import java.util.ArrayList;
public class Version implements Comparable<Version> {
private String version;
private String[] parts;
public final String getVersion() {
return this.version;
}
public String[] getParts() {
return this.parts;
}
public Version(String version) {
if(version == null)
throw new IllegalArgumentException("Version can not be null");
if(!version.matches("[0-9]+(\\.[0-9]+)*"))
throw new IllegalArgumentException("Invalid version format");
this.version = version;
this.parts = this.getVersion().split("\\.");
}
@Override public int compareTo(Version that) {
if(that == null)
return 1;
String[] thisParts = this.getParts();
String[] thatParts = that.getParts();
int length = Math.max(thisParts.length, thatParts.length);
for(int i = 0; i < length; i++) {
int thisPart = i < thisParts.length ?
Integer.parseInt(thisParts[i]) : 0;
int thatPart = i < thatParts.length ?
Integer.parseInt(thatParts[i]) : 0;
if(thisPart < thatPart)
return -1;
if(thisPart > thatPart)
return 1;
}
return 0;
}
@Override public boolean equals(Object that) {
if(this == that)
return true;
if(that == null)
return false;
if(this.getClass() != that.getClass())
return false;
return this.compareTo((Version) that) == 0;
}
@Override public final int hashCode() {
ArrayList<Integer> intParts = new ArrayList();
String[] thisParts = this.getParts();
for(int i = 0; i < thisParts.length; i++) {
intParts.add(Integer.parseInt(thisParts[i]));
}
Integer[] list = intParts.toArray(new Integer[intParts.size()]);
return Arrays.hashCode(list);
}
}
I tried to move the main function as follows.
Main.java
package com.example;
import com.example.Version;
public class Main {
public static void main(String[] args) {
Version a = new Version("1.1");
Version b = new Version("1.1.1");
System.out.println(a.compareTo(b)); // return -1 (a<b)
System.out.println(a.equals(b)); // return false
System.out.println(a.hashCode());
System.out.println(b.hashCode());
Version c = new Version("2.0");
Version d = new Version("1.9.9");
System.out.println(c.compareTo(d)); // return 1 (c>d)
System.out.println(c.equals(d)); // return false
System.out.println(c.hashCode());
System.out.println(d.hashCode());
Version e = new Version("2.06");
Version f = new Version("2.060");
System.out.println(e.compareTo(f)); // return -1 (e<f)
System.out.println(e.equals(f)); // return false
Version g = new Version("3.3.0");
Version h = new Version("3.3.0");
System.out.println(g.compareTo(h)); // return 0 (g==h)
System.out.println(g.equals(h)); // return true
System.out.println(g.hashCode());
System.out.println(h.hashCode());
}
}
Output
-1
false
993
30784
1
false
1023
31040
-1
false
0
true
32767
32767
For the time being, I thought it would be okay because it seems to be able to survive this way. Actually, I wanted to be able to use it even if a version such as 1.3-SNAPSHOT, 1.3a is written. .. ..
Recommended Posts