Skip to content

Commit

Permalink
Fixes version comparison for balloted versions
Browse files Browse the repository at this point in the history
Fixes #168
  • Loading branch information
qligier committed Dec 28, 2023
1 parent 5b4dc52 commit f4c9aff
Showing 1 changed file with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*-
* #%L
* HAPI FHIR JPA Server
* %%
* Copyright (C) 2014 - 2023 Smile CDR, Inc.
* %%
* 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.
* #L%
*/
package ca.uhn.fhir.jpa.packages;

import java.math.BigDecimal;
import java.util.Comparator;

import static org.apache.commons.lang3.StringUtils.isNumeric;

public class PackageVersionComparator implements Comparator<String> {
public static final PackageVersionComparator INSTANCE = new PackageVersionComparator();

@Override
public int compare(String o1, String o2) {

String[] o1parts = o1.split("\\.");
String[] o2parts = o2.split("\\.");

for (int i = 0; i < o1parts.length && i < o2parts.length; i++) {
String i1part = o1parts[i];
String i2part = o2parts[i];

if (isNumeric(i1part)) {
if (isNumeric(i2part)) {
int cmp = new BigDecimal(i1part).compareTo(new BigDecimal(i2part));
if (cmp != 0) {
return cmp;
}
}
}

// MATCHBOX FIX for https://github.com/ahdis/matchbox/issues/168
if (i == o1parts.length - 1 || i == o2parts.length - 1) {
if (o1.endsWith("-ballot")) {
return -1;
} else if (o2.endsWith("-ballot")) {
return 1;
}
}

int cmp = i1part.compareTo(i2part);
if (cmp != 0) {
return cmp;
}
}


return o1parts.length - o2parts.length;
}

public static boolean isEquivalent(String theSpec, String thePackageVersion) {
String[] o1parts = theSpec.split("\\.");
String[] o2parts = thePackageVersion.split("\\.");

for (int i = 0; i < o1parts.length; i++) {
if (!o1parts[i].equals("x")) {
if (!o1parts[i].equals(o2parts[i])) {
return false;
}
}
}

return true;
}
}

0 comments on commit f4c9aff

Please sign in to comment.