EventBus 읽기 소스(2) - SubscriberMethod
3173 단어 EventBus 읽기
이것은 우리가 시작할 때 코드를 통람하기에 적합하다. 코드에 대해 대체적으로 이해한 후에 세밀하게 분석하려면 뿌리부터 시작해야 한다.
이 문장
SubscriberMethod
이 클래스는 ThreadMode에 기반을 두고 있을 뿐 다른 클래스에 기반을 두고 있지 않기 때문에 우리는 안심하고 읽을 수 있다.
Subscriber는 구독자를 뜻합니다.말 그대로 구독자만 하는 방법이다.우리가 등록하는 방법 같지 않을까 추측해 보세요 (예를 들어 onMainThread (Event event).
소스:
/*
* Copyright (C) 2012-2016 Markus Junginger, greenrobot (http://greenrobot.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.greenrobot.eventbus;
import java.lang.reflect.Method;
/** Used internally by EventBus and generated subscriber indexes. */
public class SubscriberMethod {
final Method method;//
final ThreadMode threadMode;// ,
final Class> eventType;// ,
final int priority;//
final boolean sticky;//
/** Used for efficient comparison */
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();
}
}
코드가 많지 않습니다.
이해하기 쉽다.
잔소리 안 해.