|
| 1 | +/* |
| 2 | + * (C) Copyright Uwe Schindler (Generics Policeman) and others. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package de.thetaphi.forbiddenapis; |
| 18 | + |
| 19 | +import java.util.Comparator; |
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
| 22 | + |
| 23 | +/** Some static utilities for analyzing with ASM, also constants. */ |
| 24 | +final class VersionCompare { |
| 25 | + |
| 26 | + private static final Pattern DOT_SPLITTER_PATTERN = Pattern.compile("\\."); |
| 27 | + |
| 28 | + private VersionCompare() {} |
| 29 | + |
| 30 | + public static int compareVersions(String version1, String version2) { |
| 31 | + final String[] version1Splits = DOT_SPLITTER_PATTERN.split(version1), |
| 32 | + version2Splits = DOT_SPLITTER_PATTERN.split(version2); |
| 33 | + final int maxLengthOfVersionSplits = Math.max(version1Splits.length, version2Splits.length); |
| 34 | + |
| 35 | + for (int i = 0; i < maxLengthOfVersionSplits; i++) { |
| 36 | + final int v1 = i < version1Splits.length ? Integer.parseInt(version1Splits[i]) : 0; |
| 37 | + final int v2 = i < version2Splits.length ? Integer.parseInt(version2Splits[i]) : 0; |
| 38 | + final int compare = Integer.compare(v1, v2); |
| 39 | + if (compare != 0) { |
| 40 | + return compare; |
| 41 | + } |
| 42 | + } |
| 43 | + return 0; |
| 44 | + } |
| 45 | + |
| 46 | + public static Comparator<String> VERSION_COMPARATOR = new Comparator<String>() { |
| 47 | + @Override |
| 48 | + public int compare(String version1, String version2) { |
| 49 | + return compareVersions(version1, version2); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + public static Comparator<String> BUNDLED_SIGNATURES_COMPARATOR = new Comparator<String>() { |
| 54 | + @Override |
| 55 | + public int compare(String bs1, String bs2) { |
| 56 | + final Matcher m1 = Constants.ENDS_WITH_VERSION_PATTERN.matcher(bs1), |
| 57 | + m2 = Constants.ENDS_WITH_VERSION_PATTERN.matcher(bs2); |
| 58 | + if (m1.matches() && m2.matches()) { |
| 59 | + final int prefixCmp = m1.group(1).compareTo(m2.group(1)); |
| 60 | + if (prefixCmp != 0) { |
| 61 | + return prefixCmp; |
| 62 | + } |
| 63 | + return compareVersions(m1.group(2), m2.group(2)); |
| 64 | + } else { |
| 65 | + return bs1.compareTo(bs2); |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | +} |
0 commit comments