leosutic 2002/10/04 02:22:27
Modified: util/src/java/org/apache/excalibur/util Delegate.java
Log:
Added type-checking code to the DelegateHandler's handling of the
equals() method, added handling of the hashCode method in the DelegateHander
and added support for equals and hashCode to MultiDelegates.
This is so delegates and MultiDelegates can be used in collections
such as Sets - for example, in a MultiDelegate (which has Set-like
behavior).
Revision Changes Path
1.10 +28 -7
jakarta-avalon-excalibur/util/src/java/org/apache/excalibur/util/Delegate.java
Index: Delegate.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/util/src/java/org/apache/excalibur/util/Delegate.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- Delegate.java 4 Oct 2002 02:57:47 -0000 1.9
+++ Delegate.java 4 Oct 2002 09:22:27 -0000 1.10
@@ -134,12 +134,8 @@
* matches the signature (although the method name can be different).
* </p>
*
- * <h2>MultiDelegates</h2>
- * <p>
- * The full text on <code>MultiDelegate</code> is with the {@link MultiDelegate}
- * JavaDocs. MultiDelegates will invoke a whole <em>set</em> of Delegates (or
- * objects that implement the delegate's interface). The criteria that we use
- * to test if two delegates are equal are:
+ * <h3>Equality</h3>
+ * The criteria that we use to test if two delegates are equal are:
*
* <ul>
* <li>
@@ -149,6 +145,12 @@
* </li>
* <li>They refer to the same method as resolved by
<code>Method.equals</code>.</li>
* </ul>
+ *
+ * <h2>MultiDelegates</h2>
+ * <p>
+ * The full text on <code>MultiDelegate</code> is with the {@link MultiDelegate}
+ * JavaDocs. MultiDelegates will invoke a whole <em>set</em> of Delegates (or
+ * objects that implement the delegate's interface).
* </p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
@@ -234,6 +236,7 @@
{
private final Object m_instance;
private final Method m_delegateMethod;
+ private final Integer m_hashCode;
/**
* Create a new InvocationHandler for the delegate we manufactured,
@@ -248,6 +251,7 @@
{
m_instance = instance;
m_delegateMethod = extractMethod( m_instance.getClass(), methodName,
signature );
+ m_hashCode = new Integer( m_delegateMethod.hashCode () ^
m_instance.hashCode () );
}
/**
@@ -263,6 +267,7 @@
{
m_instance = null;
m_delegateMethod = extractMethod( klass, methodName, signature );
+ m_hashCode = new Integer( m_delegateMethod.hashCode () ^ klass.hashCode
() );
}
/**
@@ -337,7 +342,7 @@
{
Object other = args[0];
- if( other instanceof Proxy )
+ if( other instanceof Proxy && Proxy.getInvocationHandler(other)
instanceof DelegateHandler)
{
DelegateHandler otherHandler = (DelegateHandler)
Proxy.getInvocationHandler( other );
if ( otherHandler.m_instance == m_instance &&
@@ -355,6 +360,10 @@
return Boolean.FALSE;
}
}
+ else if( m.getName().equals( "hashCode" ) && args == null )
+ {
+ return m_hashCode;
+ }
else
{
return m_delegateMethod.invoke( m_instance, args );
@@ -418,8 +427,10 @@
private final Class delegateClass;
private final List handlers = new ArrayList ();
+ private final Integer hash = new Integer( this.hashCode() );
private static final Method addMethod;
private static final Method removeMethod;
+ private static final Method equalsMethod;
/*
* Static initializer. Finds the add and remove methods in the
MultiDelegate interface.
@@ -430,6 +441,7 @@
{
addMethod = MultiDelegate.class.getDeclaredMethod( "add", new
Class[]{ Object.class } );
removeMethod = MultiDelegate.class.getDeclaredMethod( "remove", new
Class[]{ Object.class } );
+ equalsMethod = Object.class.getDeclaredMethod( "equals", new
Class[]{ Object.class } );
}
catch (Exception e)
{
@@ -499,6 +511,15 @@
}
return null;
}
+ else if (method.equals (equalsMethod))
+ {
+ Object other = args[0];
+ return other == proxy ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if( method.getName().equals( "hashCode" ) && args == null )
+ {
+ return hash;
+ }
else
{
List activeList = new ArrayList ();
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>