Author: niclas Date: Mon Aug 2 07:48:59 2004 New Revision: 35590 Modified: avalon/trunk/runtime/framework/impl/src/java/org/apache/avalon/framework/context/DefaultContext.java avalon/trunk/runtime/framework/impl/src/test/org/apache/avalon/framework/context/test/ContextTestCase.java Log: Added equals(), hashCode() and Serializable to DefaultContext, and updated the testcases for it.
Modified: avalon/trunk/runtime/framework/impl/src/java/org/apache/avalon/framework/context/DefaultContext.java ============================================================================== --- avalon/trunk/runtime/framework/impl/src/java/org/apache/avalon/framework/context/DefaultContext.java (original) +++ avalon/trunk/runtime/framework/impl/src/java/org/apache/avalon/framework/context/DefaultContext.java Mon Aug 2 07:48:59 2004 @@ -27,7 +27,7 @@ * @version $Id$ */ public class DefaultContext - implements Context + implements Context, Serializable { private static final class Hidden implements Serializable { @@ -202,5 +202,57 @@ "Context is read only and can not be modified"; throw new IllegalStateException( message ); } + } + + /** Check for equality between two DefaultContext objects. + * + * <p>Equality is said to be true if, and only if, the following + * criteria are met;<p> + * <ul> + * <li>They are both of the same class.</li> + * <li>They both have the same parent.</li> + * <li>The content of the context map are identical, i.e HashMap.equals()</li> + * <li>The have the same readOnly state.</li> + * </ul> + * + * @since 4.5 + */ + public boolean equals( Object o ) + { + if( this == o ) + return true; + + if( ! (o.getClass().equals( getClass() ) ) ) + return false; + + DefaultContext other = (DefaultContext) o ; + + if( ! m_contextData.equals( other.m_contextData ) ) + return false; + + if( m_parent == null ) + { + if( other.m_parent != null ) + return false; + } + else + { + if( ! m_parent.equals( other.m_parent ) ) + return false; + } + + return m_readOnly == other.m_readOnly; + } + + + public int hashCode() + { + int hash = m_contextData.hashCode(); + if( m_parent != null ) + hash ^= m_parent.hashCode(); + else + hash >>>= 3; + hash >>>= m_readOnly ? 7 : 13 ; + return hash; } } Modified: avalon/trunk/runtime/framework/impl/src/test/org/apache/avalon/framework/context/test/ContextTestCase.java ============================================================================== --- avalon/trunk/runtime/framework/impl/src/test/org/apache/avalon/framework/context/test/ContextTestCase.java (original) +++ avalon/trunk/runtime/framework/impl/src/test/org/apache/avalon/framework/context/test/ContextTestCase.java Mon Aug 2 07:48:59 2004 @@ -182,4 +182,63 @@ // Supposed to be thrown. } } + + public void testEquals() + throws Exception + { + // Different set of parents. + DefaultContext p1 = new DefaultContext(); + p1.put( "test", "CoolTest" ); + DefaultContext p2 = new DefaultContext(); + p2.put( "test", "Cool Test" ); + + DefaultContext c1 = new DefaultContext( p1 ); + DefaultContext c2 = new DefaultContext( p1 ); + DefaultContext c3 = new DefaultContext( p1 ); + DefaultContext c4 = new DefaultContext( p1 ); + DefaultContext c5 = new DefaultContext( p2 ); + + c1.put( "test", "Cool Test" ); + c2.put( "test", "Cool Test" ); + c3.put( "test", "Cool Test" ); + c3.put( "test2", "Cool Test" ); + c4.put( "test", "Cool Test" ); + c4.makeReadOnly(); + c5.put( "test", "Cool Test" ); + + assertEquals( "Identical", c1, c2 ); + assertTrue( "ContextData", ! c1.equals( c3 ) ); + assertTrue( "ReadOnly", ! c1.equals( c4 ) ); + assertTrue( "Parent", ! c1.equals( c5 ) ); + + } + + public void testHashcode() + throws Exception + { + // Different set of parents. + DefaultContext p1 = new DefaultContext(); + p1.put( "test", "CoolTest" ); + DefaultContext p2 = new DefaultContext(); + p2.put( "test", "Cool Test" ); + + DefaultContext c1 = new DefaultContext( p1 ); + DefaultContext c2 = new DefaultContext( p1 ); + DefaultContext c3 = new DefaultContext( p1 ); + DefaultContext c4 = new DefaultContext( p1 ); + DefaultContext c5 = new DefaultContext( p2 ); + + c1.put( "test", "Cool Test" ); + c2.put( "test", "Cool Test" ); + c3.put( "test", "Cool Test" ); + c3.put( "test2", "Cool Test" ); + c4.put( "test", "Cool Test" ); + c4.makeReadOnly(); + c5.put( "test", "Cool Test" ); + + assertEquals( "Identical", c1.hashCode(), c2.hashCode() ); + assertTrue( "ContextData", c1.hashCode() != c3.hashCode() ); + assertTrue( "ReadOnly", c1.hashCode() != c4.hashCode() ); + assertTrue( "Parent", c1.hashCode() != c5.hashCode() ); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]