테스트

aion-server 4.8

Gitteol
최고관리자 · 1 · 💬 0 클론/새로받기
 4.8 61f661d · 1 commits 새로받기(Pull)
game-server/src/com/aionemu/gameserver/events/AbstractEventSource.java
package com.aionemu.gameserver.events;

import java.util.Collection;
import java.util.concurrent.CopyOnWriteArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aionemu.gameserver.utils.annotations.AnnotatedClass;
import com.aionemu.gameserver.utils.annotations.AnnotatedMethod;
import com.aionemu.gameserver.utils.annotations.AnnotationManager;

/**
 * @author Rolandas
 */
public abstract class AbstractEventSource<T extends AbstractEvent<?>> {

	private static Logger log = LoggerFactory.getLogger(AbstractEventSource.class);

	private Collection<EventListener<T>> listeners = new CopyOnWriteArrayList<>();

	public AbstractEventSource() {
		Class<?> theClass = getClass();
		AnnotatedClass annotatedClass;
		synchronized (theClass) {
			if (AnnotationManager.containsClass(theClass))
				return;

			annotatedClass = AnnotationManager.getAnnotatedClass(theClass);

			for (AnnotatedMethod method : annotatedClass.getAnnotatedMethods()) {
				if (addListenable(method))
					log.debug("Added method {}", method.getMethod());
			}
		}
	}

	protected abstract boolean addListenable(AnnotatedMethod annotatedMethod);

	public void addEventListener(EventListener<T> listener) {
		listeners.add(listener);
	}

	public void removeEventListener(EventListener<T> listener) {
		listeners.remove(listener);
	}

	public boolean hasSubscribers() {
		return listeners.size() > 0;
	}

	protected abstract boolean canHaveEventNotifications(T event);

	/**
	 * Method to notify all listeners about the event before it is actually handled
	 * 
	 * @param event
	 * @return true if notified
	 */
	protected boolean fireBeforeEvent(T event) {
		return fireBeforeEvent(event, null);
	}

	/**
	 * Method to pass event arguments to event listener. Override it in class and call it from event method
	 * 
	 * @param event
	 * @param callingArguments
	 * @return false if the class should not have listeners for the event
	 */
	protected boolean fireBeforeEvent(T event, Object[] callingArguments) {
		if (!canHaveEventNotifications(event))
			return false;
		event.callingArguments = callingArguments;
		for (EventListener<T> listener : listeners) {
			listener.onBeforeEvent(event);
		}
		return true;
	}

	/**
	 * Method to notify all listeners about the event after the handler was called
	 * 
	 * @param event
	 * @return true if notified
	 */
	protected boolean fireAfterEvent(T event) {
		return fireAfterEvent(event, null);
	}

	/**
	 * Method to pass event arguments to event listener. Override it in class and call it from event method
	 * 
	 * @param event
	 * @param callingArguments
	 * @return false if the class should not have listeners for the event or calling fireBeforeEvent disabled it by setting isHandled to false
	 */
	protected boolean fireAfterEvent(T event, Object[] callingArguments) {
		if (!canHaveEventNotifications(event) || !event.isHandled())
			return false;
		event.callingArguments = callingArguments;
		for (EventListener<T> listener : listeners) {
			listener.onAfterEvent(event);
		}
		return true;
	}

}

📎 첨부파일

댓글 작성 권한이 없습니다.
🏆 포인트 랭킹 TOP 10
순위 닉네임 포인트
1 no_profile 타키야겐지쪽지보내기 자기소개 아이디로 검색 전체게시물 102,949
2 no_profile 동가리쪽지보내기 자기소개 아이디로 검색 전체게시물 63,733
3 no_profile 라프텔쪽지보내기 자기소개 아이디로 검색 전체게시물 51,771
4 no_profile 불멸의행복쪽지보내기 자기소개 아이디로 검색 전체게시물 36,923
5 서번트쪽지보내기 자기소개 아이디로 검색 전체게시물 35,011
6 no_profile 닥터스쪽지보내기 자기소개 아이디로 검색 전체게시물 29,470
7 no_profile 검은고양이쪽지보내기 자기소개 아이디로 검색 전체게시물 29,077
8 no_profile Revolution쪽지보내기 자기소개 아이디로 검색 전체게시물 28,199
9 no_profile 보거스쪽지보내기 자기소개 아이디로 검색 전체게시물 26,731
10 no_profile 호롤롤로쪽지보내기 자기소개 아이디로 검색 전체게시물 17,020
알림 0