MINA学习笔记——奇怪的处理器DemuxingIoHandler

[size=small]初次接触MINA,看了看多处理器的代码,发现一些有意思的现象,按说写Apache框架的这些人都是高手的,怎么会犯这样的错,下面是DemuxingIoHandler的部分说明。这个类中有个方法要用深度优先遍历一颗继承树。[/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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值