Author: hboutemy Date: Sun Jan 17 18:07:06 2010 New Revision: 900175 URL: http://svn.apache.org/viewvc?rev=900175&view=rev Log: updated version comparison classes with modifications done earlier in maven-artifact
Modified: maven/mercury/trunk/mercury-core/src/main/java/org/apache/maven/mercury/artifact/version/ComparableVersion.java maven/mercury/trunk/mercury-core/src/test/java/org/apache/maven/mercury/artifact/version/ComparableVersionTest.java Modified: maven/mercury/trunk/mercury-core/src/main/java/org/apache/maven/mercury/artifact/version/ComparableVersion.java URL: http://svn.apache.org/viewvc/maven/mercury/trunk/mercury-core/src/main/java/org/apache/maven/mercury/artifact/version/ComparableVersion.java?rev=900175&r1=900174&r2=900175&view=diff ============================================================================== --- maven/mercury/trunk/mercury-core/src/main/java/org/apache/maven/mercury/artifact/version/ComparableVersion.java (original) +++ maven/mercury/trunk/mercury-core/src/main/java/org/apache/maven/mercury/artifact/version/ComparableVersion.java Sun Jan 17 18:07:06 2010 @@ -25,15 +25,35 @@ import java.util.Properties; import java.util.Stack; -/* - * Generic implementation of version comparison. +/** + * Generic implementation of version comparison. Features: + * <ul> + * <li>mixing of '<code>-</code>' (dash) and '<code>.</code>' (dot) separators,</li> + * <li>transition between characters and digits also constitutes a separator: + * <code>1.0alpha1 => [1, 0, alpha, 1]</code></li> + * <li>unlimited number of version components,</li> + * <li>version components in the text can be digits or strings</li> + * <li>strings are checked for well-known qualifiers and the qualifier ordering is used for version ordering. + * Well-known qualifiers (case insensitive):<ul> + * <li><code>snapshot</code></li> + * <li><code>alpha</code> or <code>a</code></li> + * <li><code>beta</code> or <code>b</code></li> + * <li><code>milestone</code> or <code>m</code></li> + * <li><code>rc</code> or <code>cr</code></li> + * <li><code>(the empty string)</code> or <code>ga</code> or <code>final</code></li> + * <li><code>sp</code></li> + * </ul> + * </li> + * <li>a dash usually precedes a qualifier, and is always less important than something preceded with a dot.</li> + * </ul> * + * @see <a href="http://docs.codehaus.org/display/MAVEN/Versioning">"Versioning" on Maven Wiki</a> * @author <a href="mailto:ken...@apache.org">Kenney Westerhof</a> - * @author <a href="mailto:hbout...@apache.org">Herve Boutemy</a> + * @author <a href="mailto:hbout...@apache.org">Hervé Boutemy</a> * @version $Id$ */ public class ComparableVersion - implements Comparable + implements Comparable<ComparableVersion> { private String value; @@ -125,7 +145,7 @@ { private final static String[] QUALIFIERS = { "alpha", "beta", "milestone", "rc", "snapshot", "", "sp" }; - private final static List _QUALIFIERS = Arrays.asList( QUALIFIERS ); + private final static List<String> _QUALIFIERS = Arrays.asList( QUALIFIERS ); private final static Properties ALIASES = new Properties(); static @@ -139,7 +159,7 @@ * A comparable for the empty-string qualifier. This one is used to determine if a given qualifier makes the * version older than one without a qualifier, or more recent. */ - private static Comparable RELEASE_VERSION_INDEX = String.valueOf( _QUALIFIERS.indexOf( "" ) ); + private static String RELEASE_VERSION_INDEX = String.valueOf( _QUALIFIERS.indexOf( "" ) ); private String value; @@ -175,16 +195,19 @@ } /** - * Returns a comparable for a qualifier. This method both takes into account the ordering of known qualifiers as - * well as lexical ordering for unknown qualifiers. just returning an Integer with the index here is faster, but - * requires a lot of if/then/else to check for -1 or QUALIFIERS.size and then resort to lexical ordering. Most - * comparisons are decided by the first character, so this is still fast. If more characters are needed then it - * requires a lexical sort anyway. + * Returns a comparable value for a qualifier. + * + * This method both takes into account the ordering of known qualifiers as well as lexical ordering for unknown + * qualifiers. + * + * just returning an Integer with the index here is faster, but requires a lot of if/then/else to check for -1 + * or QUALIFIERS.size and then resort to lexical ordering. Most comparisons are decided by the first character, + * so this is still fast. If more characters are needed then it requires a lexical sort anyway. * * @param qualifier - * @return + * @return an equivalent value that can be used with lexical comparison */ - public static Comparable comparableQualifier( String qualifier ) + public static String comparableQualifier( String qualifier ) { int i = _QUALIFIERS.indexOf( qualifier ); @@ -225,7 +248,7 @@ * with '-(number)' in the version specification). */ private static class ListItem - extends ArrayList + extends ArrayList<Item> implements Item { public int getType() @@ -240,9 +263,9 @@ void normalize() { - for ( ListIterator iterator = listIterator( size() ); iterator.hasPrevious(); ) + for ( ListIterator<Item> iterator = listIterator( size() ); iterator.hasPrevious(); ) { - Item item = (Item) iterator.previous(); + Item item = iterator.previous(); if ( item.isNull() ) { iterator.remove(); // remove null trailing items: 0, "", empty list @@ -275,8 +298,8 @@ return 1; // 1-1 > 1-sp case LIST_ITEM: - Iterator left = iterator(); - Iterator right = ( (ListItem) item ).iterator(); + Iterator<Item> left = iterator(); + Iterator<Item> right = ( (ListItem) item ).iterator(); while ( left.hasNext() || right.hasNext() ) { @@ -302,7 +325,7 @@ public String toString() { StringBuffer buffer = new StringBuffer( "(" ); - for ( Iterator iter = iterator(); iter.hasNext(); ) + for ( Iterator<Item> iter = iterator(); iter.hasNext(); ) { buffer.append( iter.next() ); if ( iter.hasNext() ) @@ -330,7 +353,7 @@ ListItem list = items; - Stack stack = new Stack(); + Stack<Item> stack = new Stack<Item>(); stack.push( list ); boolean isDigit = false; @@ -420,9 +443,9 @@ return isDigit ? new IntegerItem( buf ) : new StringItem( buf, false ); } - public int compareTo( Object o ) + public int compareTo( ComparableVersion o ) { - return items.compareTo( ( (ComparableVersion) o ).items ); + return items.compareTo( o.items ); } public String toString() Modified: maven/mercury/trunk/mercury-core/src/test/java/org/apache/maven/mercury/artifact/version/ComparableVersionTest.java URL: http://svn.apache.org/viewvc/maven/mercury/trunk/mercury-core/src/test/java/org/apache/maven/mercury/artifact/version/ComparableVersionTest.java?rev=900175&r1=900174&r2=900175&view=diff ============================================================================== --- maven/mercury/trunk/mercury-core/src/test/java/org/apache/maven/mercury/artifact/version/ComparableVersionTest.java (original) +++ maven/mercury/trunk/mercury-core/src/test/java/org/apache/maven/mercury/artifact/version/ComparableVersionTest.java Sun Jan 17 18:07:06 2010 @@ -29,6 +29,7 @@ * @author <a href="mailto:hbout...@apache.org">Herve Boutemy</a> * @version $Id$ */ +...@suppresswarnings( "unchecked" ) public class ComparableVersionTest extends TestCase { @@ -104,12 +105,19 @@ checkVersionsEqual( "1", "1-0" ); checkVersionsEqual( "1", "1.0-0" ); checkVersionsEqual( "1.0", "1.0-0" ); + // no separator between number and character checkVersionsEqual( "1a", "1.a" ); checkVersionsEqual( "1a", "1-a" ); checkVersionsEqual( "1a", "1.0-a" ); checkVersionsEqual( "1a", "1.0.0-a" ); checkVersionsEqual( "1.0a", "1.0.a" ); checkVersionsEqual( "1.0.0a", "1.0.0.a" ); + checkVersionsEqual( "1x", "1.x" ); + checkVersionsEqual( "1x", "1-x" ); + checkVersionsEqual( "1x", "1.0-x" ); + checkVersionsEqual( "1x", "1.0.0-x" ); + checkVersionsEqual( "1.0x", "1.0.x" ); + checkVersionsEqual( "1.0.0x", "1.0.0.x" ); // aliases checkVersionsEqual( "1ga", "1" ); @@ -120,6 +128,22 @@ checkVersionsEqual( "1a1", "1alpha1" ); checkVersionsEqual( "1b2", "1beta2" ); checkVersionsEqual( "1m3", "1milestone3" ); + + // case insensitive + checkVersionsEqual( "1X", "1x" ); + checkVersionsEqual( "1A", "1a" ); + checkVersionsEqual( "1B", "1b" ); + checkVersionsEqual( "1M", "1m" ); + checkVersionsEqual( "1Ga", "1" ); + checkVersionsEqual( "1GA", "1" ); + checkVersionsEqual( "1Final", "1" ); + checkVersionsEqual( "1FinaL", "1" ); + checkVersionsEqual( "1FINAL", "1" ); + checkVersionsEqual( "1Cr", "1Rc" ); + checkVersionsEqual( "1cR", "1rC" ); + checkVersionsEqual( "1m3", "1Milestone3" ); + checkVersionsEqual( "1m3", "1MileStone3" ); + checkVersionsEqual( "1m3", "1MILESTONE3" ); } public void testVersionComparing()