java 通用事件回调类(观察者队列)

来源:互联网 发布:演讲学而知不足 编辑:程序博客网 时间:2024/06/07 07:13
//文件名 CCommonDelegate.java
//作者  龙远智 @imo云办公室
//邮件  lyzlyz_2004@126.com
//时间  2014.Aug.5
//功能  委托库 CCommonDelegate
//声明  您可以自由使用和修改本函数库,但请保留本文件头

package com.imo.base;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.Iterator;

public class CCommonDelegate {
    private Class<?>[] paramsType=null;
    private class InnStruct<T>
    {
        public Method m_mthod = null;
        public T m_objRef = null;
        private Class<?> classzz=null;
        InnStruct(T o, Method m)
        {
            classzz = o.getClass();
            m_mthod = m;
            m_objRef = o;
        }
    }
    
    LinkedList<InnStruct<?>> m_lsObservers = new LinkedList<InnStruct<?>>();
    
    public CCommonDelegate(Class<?>[] paramsType)
    {
        this.paramsType=paramsType;
    }
    public <T> boolean Bind(T o, String sMethodName)
    {
        assert(paramsType != null);
        if(hasBind(o))
            return true;
        Method m = null;
        try
        {
            m = o.getClass().getMethod(sMethodName, paramsType);
        }
        catch(NoSuchMethodException e)
        {
            e.printStackTrace();
            return false;
        }
        catch(SecurityException e)
        {
            e.printStackTrace();
            return false;
        }
        
        InnStruct<T> obj = new InnStruct<T>(o, m);
        synchronized(this)
        {
            m_lsObservers.add(obj);
        }
        
        return true;
    }
    public boolean hasBind(Object o)
    {
        synchronized(this)
        {
            Iterator<InnStruct<?>> it = m_lsObservers.iterator();
            for(; it.hasNext();)
            {
                if(it.next().m_objRef == o)
                {
                    return true;
                }
            }
            return false;
        }
    }
    public void UnBind(Object o)
    {
        synchronized(this)
        {
            Iterator<InnStruct<?>> it = m_lsObservers.iterator();
            for(; it.hasNext();)
            {
                if(it.next().m_objRef == o)
                {
                    it.remove();
                    return;
                }
            }
        }
    }
    public void clear()
    {
        synchronized(this)
        {
            m_lsObservers.clear();
        }
    }
    
    public void invoke(Object... params) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
    {
        boolean bEqual = arrayContentsEq(paramsType, params, false);
        if(!bEqual)
        {
            CheckParamTypesError(params);
        }
        CallObservers(params);
    }
    
    public void invokeWithSubClassTolerant(Object... params) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
    {
        boolean bEqual = arrayContentsEq(paramsType, params, true);
        if(!bEqual)
        {
            CheckParamTypesError(params);
        }
        CallObservers(params);
    }
    
    
    private void CheckParamTypesError(Object... params) throws IllegalArgumentException
    {
        StringBuffer sDesc = new StringBuffer("CCommonDelegate invoke paramters type mismatch, expect:(");
        int i=0;
        for(i=0;i<paramsType.length;++i)
        {
            sDesc.append(paramsType[i].getName()).append(",");
        }
        sDesc.append("), but get:(");
        for(i=0;i<params.length;++i)
        {
            sDesc.append(params[i].getClass().getName()).append(",");
        }
        sDesc.append(")");
        throw new IllegalArgumentException(sDesc.toString());
    }
    
    private void CallObservers(Object... params) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
    {
        LinkedList<InnStruct<?>> oListTmp = null;
        synchronized(this)
        {
            oListTmp = new LinkedList<InnStruct<?>>(m_lsObservers);
        }
        for(InnStruct<?> oRef:oListTmp)
        {
            oRef.m_mthod.invoke(oRef.m_objRef, params);
        }
        oListTmp.clear();
        oListTmp = null;
    }
    
    private static boolean arrayContentsEq(Class<?>[] a1, Object[] a2, boolean bSubClassTolerant)
    {
        if (a1 == null)
        {
            return a2 == null || a2.length == 0;
        }

        if (a2 == null)
        {
            return a1.length == 0;
        }

        if (a1.length != a2.length)
        {
            return false;
        }

        for (int i = 0; i < a1.length; i++)
        {
            if(bSubClassTolerant)
            {
                 if (a2[i] != null && !a1[i].isInstance(a2[i]))
                 {
                     return false;
                 }
            }
            else
            {
                if (a2[i] != null && a1[i] != a2[i].getClass())
                {
                    return false;
                }
            }
        }
        return true;
    }
}



使用:

class A

{

public CCommonDelegate evt_OnNetWorkException = new CCommonDelegate(new Class<?>[]{Integer.class});


private void func

 {

   evt_OnNetWorkException.invoke(1);

 }

}


class B

{

  public void onNetworkException(Integer nErrcode)

 {

  ......

 }

public B(A objA)

{

  objA.evt_OnNetWorkException.Bind(this, "onNetworkException");

}

}


0 0
原创粉丝点击