class HandlerUtil implements ApplicationListener<ContextRefreshedEvent> {

	public static <H, T> H getPreferredHandler(Class<H> handlerType, Class<T> type) {
		
		if (handlerType == null || type == null) {
			throw new IllegalArgumentException("You must specify both a handlerType and a type");
		}
		List<H> handlers = getHandlersForType(handlerType, type);
		if (handlers == null || handlers.isEmpty()) {
			throw new APIException("handler.type.not.found", new Object[] { handlerType, type });
		}
		
		if (handlers.size() > 1) {
			int order1 = getOrderOfHandler(handlers.get(0).getClass());
			int order2 = getOrderOfHandler(handlers.get(1).getClass());
			if (order1 == order2) {
				throw new APIException("handler.type.multiple", new Object[] { handlerType, type });
			}
		}
		
		return handlers.get(0);
	}

}