Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Code to Handle Versions and Their Comparisons [Snippet]

DZone's Guide to

Java Code to Handle Versions and Their Comparisons [Snippet]

Take a look at this quick piece of code to help you manage version strings and their comparisons within your Java projects.

· Java Zone ·
Free Resource

Start coding something amazing with our library of open source Cloud code patterns. Content provided by IBM.

Hello, buddies.

I'm sharing a small but useful piece of code that can help you in handling version strings and comparisons in day-to-day coding.

Often, we struggle while comparing and transcoding versions, which might take some time and can be frustrating as well.

So for your reference and my own, I am keeping this snippet handy.

Check out the full project here, or you may refer to the code shared below.

Happy coding!

package versionCheckDemo;
/**  
 * @author namit  
 * @version 2.0  
 *   
 * re-checking-in for testing develop branch  
 *   
 * */
public class VersionTester {
    public static void main(String[] args) {
        System.out.println("Part 1 - Generalize the version numbers to a standard format - a.b.c");
        String input = "1.1";
        update(input);
        input = "1.1.1";
        update(input);
        input = "1";
        update(input);
        System.out.println("Part 2 - Get the main digit representing the version number ");
        input = "1.0.1";
        findVersion(input);
        input = "54.1";
        findVersion(input);
        input = "100";
        findVersion(input);
        input = "100.c.c";
        findVersion(input);
        input = "100.cc.&@!#$$";
        findVersion(input);
        System.out.println("Part 3 - Comparing the versions, under the format a.b.c ");
        String versionX = "2.3";
        String minVersion = "2.5.5";
        checkVersion(versionX, minVersion);
        versionX = "2.7.9";
        minVersion = "2.5.5";
        checkVersion(versionX, minVersion);
    }
    /**  
     * Method to update the version numbers to a uniform pattern  
     * */
    private static void update(String input) {
        System.out.println("incoming >> " + input);
        int occurence = countMatches(input, '.');
        if (occurence == 0)
            input += ".0.0";
        else if (occurence == 1)
            input += ".0";
        System.out.println("outgoing >> " + input);
        System.out.println("---------------");
    }
    public static int countMatches(final CharSequence str, final char ch) {
        if (null == str) {
            return 0;
        }
        int count = 0;
        char[] arr = ((String) str).toCharArray();
        for (char c: arr) {
            if (c == ch)
                count++;
        }
        // likewise you can go for  
        // org.apache.commons.lang3.StringUtils.StringUtils.countMatches(str, ch)  
        // for saving time of writing the count logic  
        return count;
    }
    /**  
     * Method to find the main version number  
     * */
    private static void findVersion(String input) {
        System.out.println("Incoming >> " + input);
        String inputWithoutDots = input.replace(".", "");
        if (inputWithoutDots.matches("[0-9]+")) {
            int version = 0;
            String[] arr = input.split("\\.");
            version = Integer.valueOf(arr[0]);
            System.out.println("Success! outgoing >> " + version);
        } else {
            System.out.println("error!");
        }
        System.out.println("---------------");
    }
    /**  
     * Method to compare the given version with a minimum accepted version  
     * */
    private static void checkVersion(String versionX, String minVersion) {
        String minVersionFormatted = String.format("%-4s", minVersion.replace(".", "")).replace(' ', '0');
        int minVersionNum = Integer.valueOf(minVersionFormatted);
        String formattedVersion = String.format("%-4s", versionX.replace(".", "")).replace(' ', '0');
        int versionXNum = Integer.parseInt(formattedVersion);
        System.out.println("Min Version - " + minVersion);
        System.out.println("You provided - " + versionX);
        if (versionXNum >= minVersionNum) {
            System.out.println("Version Accepted!");
        } else {
            System.out.println("Version Rejected!");
        }
        System.out.println("---------------");
    }
}


Output:

 Part 1 - Generalize the version numbers to a standard format - a.b.c  
 incoming >> 1.1  
 outgoing >> 1.1.0  
 ---------------  
 incoming >> 1.1.1  
 outgoing >> 1.1.1  
 ---------------  
 incoming >> 1  
 outgoing >> 1.0.0  
 ---------------  
 Part 2 - Get the main digit representing the version number   
 Incoming >> 1.0.1  
 Success! outgoing >> 1  
 ---------------  
 Incoming >> 54.1  
 Success! outgoing >> 54  
 ---------------  
 Incoming >> 100  
 Success! outgoing >> 100  
 ---------------  
 Incoming >> 100.c.c  
 error!  
 ---------------  
 Incoming >> 100.cc.&@!#$$  
 error!  
 ---------------  
 Part 3 - Comparing the versions, under the format a.b.c   
 Min Version - 2.5.5  
 You provided - 2.3  
 Version Rejected!  
 ---------------  
 Min Version - 2.5.5  
 You provided - 2.7.9  
 Version Accepted!  
 ---------------  


Use this tool to look at the contents of GitHub and classify code based on the programming language used.  Content provided by IBM Developer.

Topics:
java ,code ,snippet ,version ,comparison ,string

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}