EventBus源码理解补充

1、SubscriberMethod

EventBus内部使用并生成订阅者索引的一个类,里面储存了订阅方法相关的信息,源码如下:

public class SubscriberMethod {
    //订阅方法
    final Method method;
    //订阅方法执行的线程
    final ThreadMode threadMode;
    //订阅方法的参数
    final Class eventType;
    //优先级
    final int priority;
    //是否是粘性事件
    final boolean sticky;
    //由订阅者class、订阅方法名、订阅方法参数组成的是一个字符串
    //通过checkMethodString 方法生成,用于在equals方法中快速进行比较两个订阅方法是否是同一个,
    String methodString;

    public SubscriberMethod(Method method, Class eventType, ThreadMode threadMode, int priority, boolean sticky) {
        this.method = method;
        this.threadMode = threadMode;
        this.eventType = eventType;
        this.priority = priority;
        this.sticky = sticky;
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        } else if (other instanceof SubscriberMethod) {
            checkMethodString();
            SubscriberMethod otherSubscriberMethod = (SubscriberMethod)other;
            otherSubscriberMethod.checkMethodString();
            // Don't use method.equals because of http://code.google.com/p/android/issues/detail?id=7811#c6
            return methodString.equals(otherSubscriberMethod.methodString);
        } else {
            return false;
        }
    }

    private synchronized void checkMethodString() {
        if (methodString == null) {
            // Method.toString has more overhead, just take relevant parts of the method
            StringBuilder builder = new StringBuilder(64);
            builder.append(method.getDeclaringClass().getName());
            builder.append('#').append(method.getName());
            builder.append('(').append(eventType.getName());
            methodString = builder.toString();
        }
    }

    @Override
    public int hashCode() {
        return method.hashCode();
    }

你可能感兴趣的:(EventBus源码理解补充)