/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.registry.driver;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;
/**
* DriverUtils
*/
public class DriverUtils
{
public static boolean isBetaVersion(@NotNull String versionInfo) {
return versionInfo.contains("beta") || versionInfo.contains("alpha");
}
@Nullable
public static String findLatestVersion(@NotNull Collection<String> allVersions) {
String latest = null;
for (String version : allVersions) {
if (isBetaVersion(version)) {
continue;
}
if (latest == null || compareVersions(version, latest) > 0) {
latest = version;
}
}
if (latest == null) {
// Now use beta versions too
for (String version : allVersions) {
if (latest == null || compareVersions(version, latest) > 0) {
latest = version;
}
}
}
return latest;
}
public static int compareVersions(@NotNull String v1, @NotNull String v2) {
StringTokenizer st1 = new StringTokenizer(v1, ".-_");
StringTokenizer st2 = new StringTokenizer(v2, ".-_");
while (st1.hasMoreTokens() && st2.hasMoreTokens()) {
String t1 = st1.nextToken();
String t2 = st2.nextToken();
try {
int cmp = Integer.parseInt(t1) - Integer.parseInt(t2);
if (cmp != 0) {
return cmp;
}
} catch (NumberFormatException e) {
// Non-numeric versions - use lexicographical compare
int cmp = t1.compareTo(t2);
if (cmp != 0) {
return cmp;
}
}
}
if (st1.hasMoreTokens()) {
return 1;
} else if (st2.hasMoreTokens()) {
return -1;
} else {
return 0;
}
}
}