[size=small]初次接触MINA,看了看多处理器的代码,发现一些有意思的现象,按说写Apache框架的这些人都是高手的,怎么会犯这样的错,下面是DemuxingIoHandler的部分说明。这个类中有个方法要用深度优先遍历一颗继承树。[/size]
[size=small]I7 既然是C3 的接口,从图中可以看出I8, I9, I3, I4 都是接口,那么I7 怎么可以继承2个接口,如果是实现I8 和I9,那I7 怎么还可能是接口?好奇怪的现象。。[/size]
下边是这个深度优先遍历的实现:
Consider the following type hierarchy (Cx are classes while Ix are interfaces):
C3 - I7 - I9
| | /\
| I8 I3 I4
|
C2 - I5 - I6
|
C1 - I1 - I2 - I4
| |
| I3
Object
When message is of type C3 this hierarchy will be searched in the following order: C3, I7, I8, I9, I3, I4, C2, I5, I6, C1, I1, I2, I3, I4, Object.
[size=small]I7 既然是C3 的接口,从图中可以看出I8, I9, I3, I4 都是接口,那么I7 怎么可以继承2个接口,如果是实现I8 和I9,那I7 怎么还可能是接口?好奇怪的现象。。[/size]
下边是这个深度优先遍历的实现:
private final Map<Class, MessageHandler> type2handler
= new Hashtable<Class, MessageHandler>();
private MessageHandler<Object> findHandler( Class type, Set<Class> triedClasses )
{
MessageHandler handler = null;
if( triedClasses != null && triedClasses.contains( type ) )
return null;
/*
* Try the registered handlers for an immediate match.
*/
handler = type2handler.get( type );
if( handler == null )
{
/*
* No immediate match could be found. Search the type's interfaces.
*/
if( triedClasses == null )
triedClasses = new IdentityHashSet<Class>();
triedClasses.add( type );
Class[] interfaces = type.getInterfaces();
for( int i = 0; i < interfaces.length; i ++ )
{
handler = findHandler( interfaces[ i ], triedClasses );
if( handler != null )
break;
}
}
if( handler == null )
{
/*
* No match in type's interfaces could be found. Search the
* superclass.
*/
Class superclass = type.getSuperclass();
if( superclass != null )
handler = findHandler( superclass );
}
/*
* Make sure the handler is added to the cache. By updating the cache
* here all the types (superclasses and interfaces) in the path which
* led to a match will be cached along with the immediate message type.
*/
if( handler != null )
findHandlerCache.put( type, handler );
return handler;
}