forked from hapifhir/hapi-fhir-jpaserver-starter
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes version comparison for balloted versions
Fixes #168
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
matchbox-server/src/main/java/ca/uhn/fhir/jpa/packages/PackageVersionComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |