001/*- 002 * #%L 003 * HAPI FHIR JPA Server 004 * %% 005 * Copyright (C) 2014 - 2024 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.jpa.packages; 021 022import java.math.BigDecimal; 023import java.util.Comparator; 024 025import static org.apache.commons.lang3.StringUtils.isNumeric; 026 027public class PackageVersionComparator implements Comparator<String> { 028 public static final PackageVersionComparator INSTANCE = new PackageVersionComparator(); 029 030 @Override 031 public int compare(String o1, String o2) { 032 033 String[] o1parts = o1.split("\\."); 034 String[] o2parts = o2.split("\\."); 035 036 for (int i = 0; i < o1parts.length && i < o2parts.length; i++) { 037 String i1part = o1parts[i]; 038 String i2part = o2parts[i]; 039 040 if (isNumeric(i1part)) { 041 if (isNumeric(i2part)) { 042 int cmp = new BigDecimal(i1part).compareTo(new BigDecimal(i2part)); 043 if (cmp != 0) { 044 return cmp; 045 } 046 } 047 } 048 049 int cmp = i1part.compareTo(i2part); 050 if (cmp != 0) { 051 return cmp; 052 } 053 } 054 055 return o1parts.length - o2parts.length; 056 } 057 058 public static boolean isEquivalent(String theSpec, String thePackageVersion) { 059 String[] o1parts = theSpec.split("\\."); 060 String[] o2parts = thePackageVersion.split("\\."); 061 062 for (int i = 0; i < o1parts.length; i++) { 063 if (!o1parts[i].equals("x")) { 064 if (!o1parts[i].equals(o2parts[i])) { 065 return false; 066 } 067 } 068 } 069 070 return true; 071 } 072}