avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From blorit...@apache.org
Subject cvs commit: avalon/src/test/org/apache/avalon/framework/test VersionTestCase.java
Date Tue, 20 May 2003 19:41:56 GMT
bloritsch    2003/05/20 12:41:55

  Modified:    src/java/org/apache/avalon/framework Version.java
               src/test/org/apache/avalon/framework/test
                        VersionTestCase.java
  Log:
  add some more stuff to Version to ensure its flexible use in the bundle API--all dealing
with comparisons
  
  Revision  Changes    Path
  1.27      +61 -23    avalon/src/java/org/apache/avalon/framework/Version.java
  
  Index: Version.java
  ===================================================================
  RCS file: /home/cvs/avalon/src/java/org/apache/avalon/framework/Version.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- Version.java	11 Feb 2003 16:19:27 -0000	1.26
  +++ Version.java	20 May 2003 19:41:55 -0000	1.27
  @@ -90,7 +90,7 @@
    * @version CVS $Revision$ $Date$
    */
   public final class Version
  -    implements Serializable
  +    implements Comparable, Serializable
   {
       private int m_major;
       private int m_minor;
  @@ -200,22 +200,19 @@
        */
       public boolean equals( final Version other )
       {
  -        if( m_major != other.m_major )
  -        {
  -            return false;
  -        }
  -        else if( m_minor != other.m_minor )
  -        {
  -            return false;
  -        }
  -        else if( m_micro != other.m_micro )
  -        {
  -            return false;
  -        }
  -        else
  -        {
  -            return true;
  -        }
  +    	boolean isEqual = ( getMajor() == other.getMajor() );
  +    	
  +		if ( isEqual )
  +		{
  +			isEqual = ( getMinor() == other.getMinor() );
  +		}
  +    	
  +		if ( isEqual )
  +		{
  +			isEqual = ( getMicro() == other.getMicro() );
  +		}
  +
  +        return isEqual;
       }
   
       /**
  @@ -228,14 +225,33 @@
        */
       public boolean equals( final Object other )
       {
  +    	boolean isEqual = false;
  +    	
           if( other instanceof Version )
           {
  -            return equals( (Version)other );
  -        }
  -        else
  -        {
  -            return false;
  +            isEqual = equals( (Version)other );
           }
  +
  +        return isEqual;
  +    }
  +    
  +    /**
  +     * Add a hashing function to ensure the Version object is
  +     * treated as expected in hashmaps and sets.  NOTE: any
  +     * time the equals() is overridden, hashCode() should also
  +     * be overridden.
  +     * 
  +     * @return the hashCode
  +     */
  +    public int hashCode()
  +    {
  +    	int hash = getMajor();
  +    	hash >>>= 17;
  +    	hash += getMinor();
  +    	hash >>>= 17;
  +    	hash += getMicro();
  +    	
  +    	return hash;
       }
   
       /**
  @@ -300,4 +316,26 @@
       {
           return m_major + "." + m_minor + "." + m_micro;
       }
  +
  +    /**
  +     * Compare two versions together according to the
  +     * Comparable interface.
  +     * 
  +     * @return number indicating relative value (-1, 0, 1)
  +     */
  +	public int compareTo(Object o) {
  +		Version other = (Version)o;
  +		int val = 0;
  +
  +		if ( getMajor() < other.getMajor() ) val = -1;
  +		if ( 0 == val && getMajor() > other.getMajor() ) val = 1;
  +
  +		if ( 0 == val && getMinor() < other.getMinor() ) val = -1;
  +		if ( 0 == val && getMinor() > other.getMinor() ) val = 1;
  +
  +		if ( 0 == val && getMicro() < other.getMicro() ) val = -1;
  +		if ( 0 == val && getMicro() > other.getMicro() ) val = 1;
  +
  +		return val;
  +	}
   }
  
  
  
  1.6       +46 -0     avalon/src/test/org/apache/avalon/framework/test/VersionTestCase.java
  
  Index: VersionTestCase.java
  ===================================================================
  RCS file: /home/cvs/avalon/src/test/org/apache/avalon/framework/test/VersionTestCase.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- VersionTestCase.java	22 Mar 2003 10:59:18 -0000	1.5
  +++ VersionTestCase.java	20 May 2003 19:41:55 -0000	1.6
  @@ -117,4 +117,50 @@
           assertTrue( ! v1.complies( v4 ) );
           assertTrue( ! v4.complies( v1 ) );
       }
  +    
  +    public void testHashCode()
  +    {
  +    	final Version v1 = new Version( 5, 1, 0 );
  +    	final Version v2 = new Version( 1, 0, 3 );
  +    	final Version v3 = new Version( 1, 0, 3 );
  +    	
  +		assertEquals( calculateHash(v1), v1.hashCode() );
  +		assertEquals( calculateHash(v2), v2.hashCode() );
  +		
  +		assertTrue( v1.hashCode() != v2.hashCode() );
  +		assertTrue( ! v1.equals(v2) );
  +		
  +		assertEquals( v2.hashCode(), v3.hashCode() );
  +		assertEquals( v2, v3 );
  +    }
  +    
  +    public void testComparable()
  +    {
  +    	final Version v1 = new Version( 1, 0, 0 );
  +    	final Version v2 = new Version( 2, 0, 0 );
  +    	final Version v3 = new Version( 2, 1, 0 );
  +    	final Version v4 = new Version( 2, 1, 1 );
  +    	final Version v5 = new Version( 1, 0, 0 );
  +    	
  +    	assertEquals( 0, v1.compareTo(v5) );
  +    	assertEquals( 0, v5.compareTo(v1) );
  +    	
  +		assertEquals( -1, v1.compareTo(v2) );
  +		assertEquals( 1, v2.compareTo(v1) );
  +    	
  +		assertEquals( -1, v2.compareTo(v3) );
  +		assertEquals( 1, v3.compareTo(v2) );
  +    	
  +		assertEquals( -1, v3.compareTo(v4) );
  +		assertEquals( 1, v4.compareTo(v3) );
  +    }
  +
  +	private int calculateHash(final Version v) {
  +		int hash = v.getMajor();
  +		hash >>>= 17;
  +		hash += v.getMinor();
  +		hash >>>= 17;
  +		hash += v.getMicro();
  +		return hash;
  +	}
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org


Mime
View raw message