All source projects

MrUltimateLibrary Source

Standalone source project / 68 files

MrUltimateLibrary is the backbone of all my plugins; it offers features that facilitate plugin creation, maintenance, and updates.

java

MrUltimateLibrary/src/main/java/eu/mrneznamy/mrultimatelibrary/utils/MrLibVersionComparator.java

JAVA · Read-only source

package eu.mrneznamy.mrultimatelibrary.utils;

import java.math.BigInteger;

public final class MrLibVersionComparator {
    private MrLibVersionComparator() { }

    public static int compare(String first, String second) {
        if (first == null || second == null) return 0;
        try {
            String[] a = normalize(first).split("\\+", 2);
            String[] b = normalize(second).split("\\+", 2);
            String[] aParts = a[0].split("-", 2);
            String[] bParts = b[0].split("-", 2);
            String[] aCore = aParts[0].split("\\.");
            String[] bCore = bParts[0].split("\\.");
            int length = Math.max(aCore.length, bCore.length);
            for (int i = 0; i < length; i++) {
                BigInteger aNumber = i < aCore.length ? new BigInteger(aCore[i]) : BigInteger.ZERO;
                BigInteger bNumber = i < bCore.length ? new BigInteger(bCore[i]) : BigInteger.ZERO;
                int compared = aNumber.compareTo(bNumber);
                if (compared != 0) return compared;
            }

            boolean aPreRelease = aParts.length > 1;
            boolean bPreRelease = bParts.length > 1;
            if (aPreRelease != bPreRelease) return aPreRelease ? -1 : 1;
            if (!aPreRelease) return 0;

            String[] aIdentifiers = aParts[1].split("[.-]");
            String[] bIdentifiers = bParts[1].split("[.-]");
            length = Math.max(aIdentifiers.length, bIdentifiers.length);
            for (int i = 0; i < length; i++) {
                if (i >= aIdentifiers.length) return -1;
                if (i >= bIdentifiers.length) return 1;
                String left = aIdentifiers[i];
                String right = bIdentifiers[i];
                boolean leftNumeric = left.matches("\\d+");
                boolean rightNumeric = right.matches("\\d+");
                if (leftNumeric && rightNumeric) {
                    int compared = new BigInteger(left).compareTo(new BigInteger(right));
                    if (compared != 0) return compared;
                } else if (leftNumeric != rightNumeric) {
                    return leftNumeric ? -1 : 1;
                } else {
                    int compared = left.compareToIgnoreCase(right);
                    if (compared != 0) return compared;
                }
            }
            return 0;
        } catch (RuntimeException ignored) {
            return 0;
        }
    }

    private static String normalize(String version) {
        String normalized = version.trim();
        if (normalized.startsWith("v") || normalized.startsWith("V")) normalized = normalized.substring(1);
        return normalized;
    }
}
62 linesGNU GENERAL PUBLIC LICENSE · Version 3, 29 June 2007 · UTF-8 · Read only