123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- package org.apache.archiva.common.utils;
-
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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.
- */
-
- import org.apache.commons.lang.ArrayUtils;
- import org.apache.commons.lang.StringUtils;
- import org.apache.commons.lang.math.NumberUtils;
-
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
-
- /**
- * VersionComparator - compare the parts of two version strings.
- * <p>
- * Technique.
- * </p>
- * <p>
- * * Split the version strings into parts by splitting on <code>"-._"</code> first, then breaking apart words from numbers.
- * </p>
- * <code>
- * "1.0" = "1", "0"
- * "1.0-alpha-1" = "1", "0", "alpha", "1"
- * "2.0-rc2" = "2", "0", "rc", "2"
- * "1.3-m2" = "1", "3", "m", "3"
- * </code>
- * <p>
- * compare each part individually, and when they do not match, perform the following test.
- * </p>
- * <p>
- * Numbers are calculated per normal comparison rules.
- * Words that are part of the "special word list" will be treated as their index within that heirarchy.
- * Words that cannot be identified as special, are treated using normal case-insensitive comparison rules.
- * </p>
- *
- */
- public class VersionComparator
- implements Comparator<String>
- {
- private static final Comparator<String> INSTANCE = new VersionComparator();
-
- private final List<String> specialWords;
-
- public VersionComparator()
- {
- specialWords = new ArrayList<>( 23 );
-
- // ids that refer to LATEST
- specialWords.add( "final" );
- specialWords.add( "release" );
- specialWords.add( "current" );
- specialWords.add( "latest" );
- specialWords.add( "g" );
- specialWords.add( "gold" );
- specialWords.add( "fcs" );
-
- // ids that are for a release cycle.
- specialWords.add( "a" );
- specialWords.add( "alpha" );
- specialWords.add( "b" );
- specialWords.add( "beta" );
- specialWords.add( "pre" );
- specialWords.add( "rc" );
- specialWords.add( "m" );
- specialWords.add( "milestone" );
-
- // ids that are for dev / debug cycles.
- specialWords.add( "dev" );
- specialWords.add( "test" );
- specialWords.add( "debug" );
- specialWords.add( "unofficial" );
- specialWords.add( "nightly" );
- specialWords.add( "incubating" );
- specialWords.add( "incubator" );
- specialWords.add( "snapshot" );
- }
-
- public static Comparator<String> getInstance()
- {
- return INSTANCE;
- }
-
- @Override
- public int compare( String o1, String o2 )
- {
- if ( o1 == null && o2 == null )
- {
- return 0;
- }
-
- if ( o1 == null )
- {
- return 1;
- }
-
- if ( o2 == null )
- {
- return -1;
- }
-
- String[] parts1 = toParts( o1 );
- String[] parts2 = toParts( o2 );
-
- int diff;
- int partLen = Math.max( parts1.length, parts2.length );
- for ( int i = 0; i < partLen; i++ )
- {
- diff = comparePart( safePart( parts1, i ), safePart( parts2, i ) );
- if ( diff != 0 )
- {
- return diff;
- }
- }
-
- diff = parts2.length - parts1.length;
-
- if ( diff != 0 )
- {
- return diff;
- }
-
- return o1.compareToIgnoreCase( o2 );
- }
-
- private String safePart( String[] parts, int idx )
- {
- if ( idx < parts.length )
- {
- return parts[idx];
- }
-
- return "0";
- }
-
- private int comparePart( String s1, String s2 )
- {
- boolean is1Num = NumberUtils.isNumber( s1 );
- boolean is2Num = NumberUtils.isNumber( s2 );
-
- // (Special Case) Test for numbers both first.
- if ( is1Num && is2Num )
- {
- int i1 = NumberUtils.toInt( s1 );
- int i2 = NumberUtils.toInt( s2 );
-
- return i1 - i2;
- }
-
- // Test for text both next.
- if ( !is1Num && !is2Num )
- {
- int idx1 = specialWords.indexOf( s1.toLowerCase() );
- int idx2 = specialWords.indexOf( s2.toLowerCase() );
-
- // Only operate perform index based operation, if both strings
- // are found in the specialWords index.
- if ( idx1 >= 0 && idx2 >= 0 )
- {
- return idx1 - idx2;
- }
- }
-
- // Comparing text to num
- if ( !is1Num && is2Num )
- {
- return -1;
- }
-
- // Comparing num to text
- if ( is1Num && !is2Num )
- {
- return 1;
- }
-
- // Return comparison of strings themselves.
- return s1.compareToIgnoreCase( s2 );
- }
-
- public static String[] toParts( String version )
- {
- if ( StringUtils.isBlank( version ) )
- {
- return ArrayUtils.EMPTY_STRING_ARRAY;
- }
-
- int modeOther = 0;
- int modeDigit = 1;
- int modeText = 2;
-
- List<String> parts = new ArrayList<>();
- int len = version.length();
- int i = 0;
- int start = 0;
- int mode = modeOther;
-
- while ( i < len )
- {
- char c = version.charAt( i );
-
- if ( Character.isDigit( c ) )
- {
- if ( mode != modeDigit )
- {
- if ( mode != modeOther )
- {
- parts.add( version.substring( start, i ) );
- }
- mode = modeDigit;
- start = i;
- }
- }
- else if ( Character.isLetter( c ) )
- {
- if ( mode != modeText )
- {
- if ( mode != modeOther )
- {
- parts.add( version.substring( start, i ) );
- }
- mode = modeText;
- start = i;
- }
- }
- else
- {
- // Other.
- if ( mode != modeOther )
- {
- parts.add( version.substring( start, i ) );
- mode = modeOther;
- }
- }
-
- i++;
- }
-
- // Add remainder
- if ( mode != modeOther )
- {
- parts.add( version.substring( start, i ) );
- }
-
- return parts.toArray( new String[parts.size()] );
- }
- }
|