Skip to content

Commit

Permalink
Polish apache#3984 : Rename Listener to be EventListener
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed May 17, 2019
1 parent e9e7292 commit 18be1c9
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import static java.util.Collections.sort;
import static java.util.Collections.unmodifiableList;
import static org.apache.dubbo.common.event.Listener.findEventType;
import static org.apache.dubbo.common.event.EventListener.findEventType;

/**
* The abstract {@link EventDispatcher} providers the common implementation.
Expand All @@ -39,7 +39,7 @@ public abstract class AbstractEventDispatcher implements EventDispatcher {

private final Object mutex = new Object();

private final ConcurrentMap<Class<? extends Event>, List<Listener>> listenersCache = new ConcurrentHashMap<>();
private final ConcurrentMap<Class<? extends Event>, List<EventListener>> listenersCache = new ConcurrentHashMap<>();

private final Executor executor;

Expand All @@ -57,22 +57,22 @@ protected AbstractEventDispatcher(Executor executor) {
}

@Override
public void addListener(Listener<?> listener) throws NullPointerException, IllegalArgumentException {
public void addEventListener(EventListener<?> listener) throws NullPointerException, IllegalArgumentException {
assertListener(listener);
doInListener(listener, listeners -> {
addIfAbsent(listeners, listener);
});
}

@Override
public void removeListener(Listener<?> listener) throws NullPointerException, IllegalArgumentException {
public void removeEventListener(EventListener<?> listener) throws NullPointerException, IllegalArgumentException {
assertListener(listener);
doInListener(listener, listeners -> listeners.remove(listener));
}

@Override
public List<Listener<?>> getAllListeners() {
List<Listener<?>> listeners = new LinkedList<>();
public List<EventListener<?>> getAllEventListeners() {
List<EventListener<?>> listeners = new LinkedList<>();

listenersCache
.entrySet()
Expand Down Expand Up @@ -120,13 +120,13 @@ public final Executor getExecutor() {
return executor;
}

protected void doInListener(Listener<?> listener, Consumer<Collection<Listener>> consumer) {
protected void doInListener(EventListener<?> listener, Consumer<Collection<EventListener>> consumer) {

Class<? extends Event> eventType = findEventType(listener);

if (eventType != null) {
synchronized (mutex) {
List<Listener> listeners = listenersCache.computeIfAbsent(eventType, e -> new LinkedList<>());
List<EventListener> listeners = listenersCache.computeIfAbsent(eventType, e -> new LinkedList<>());
// consume
consumer.accept(listeners);
// sort
Expand All @@ -135,7 +135,7 @@ protected void doInListener(Listener<?> listener, Consumer<Collection<Listener>>
}
}

static void assertListener(Listener<?> listener) throws NullPointerException {
static void assertListener(EventListener<?> listener) throws NullPointerException {
if (listener == null) {
throw new NullPointerException("The listener must not be null.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* {@link Event Dubbo Event} Dispatcher
*
* @see Event
* @see Listener
* @see EventListener
* @since 2.7.2
*/
public interface EventDispatcher {
Expand All @@ -38,84 +38,84 @@ public interface EventDispatcher {
Executor DIRECT_EXECUTOR = Runnable::run;

/**
* Add a {@link Listener Dubbo event listener}
* Add a {@link EventListener Dubbo event listener}
*
* @param listener a {@link Listener Dubbo event listener}
* If current {@link Listener} is existed, return <code>false</code>
* @param listener a {@link EventListener Dubbo event listener}
* If current {@link EventListener} is existed, return <code>false</code>
* @throws NullPointerException if <code>listener</code> argument is <code>null</code>
* @throws IllegalArgumentException if <code>listener</code> argument is not concrete instance
*/
void addListener(Listener<?> listener) throws NullPointerException, IllegalArgumentException;
void addEventListener(EventListener<?> listener) throws NullPointerException, IllegalArgumentException;

/**
* Add one or more {@link Listener Dubbo event listeners}
* Add one or more {@link EventListener Dubbo event listeners}
*
* @param listener a {@link Listener Dubbo event listener}
* @param others an optional {@link Listener Dubbo event listeners}
* @param listener a {@link EventListener Dubbo event listener}
* @param others an optional {@link EventListener Dubbo event listeners}
* @throws NullPointerException if one of arguments is <code>null</code>
* @throws IllegalArgumentException if one of arguments argument is not concrete instance
*/
default void addListeners(Listener<?> listener, Listener<?>... others) throws NullPointerException,
default void addEventListeners(EventListener<?> listener, EventListener<?>... others) throws NullPointerException,
IllegalArgumentException {
List<Listener<?>> listeners = new ArrayList<>(1 + others.length);
List<EventListener<?>> listeners = new ArrayList<>(1 + others.length);
listeners.add(listener);
listeners.addAll(Arrays.asList(others));
addListeners(listeners);
addEventListeners(listeners);
}

/**
* Add multiple {@link Listener Dubbo event listeners}
* Add multiple {@link EventListener Dubbo event listeners}
*
* @param listeners the {@link Listener Dubbo event listeners}
* @param listeners the {@link EventListener Dubbo event listeners}
* @throws NullPointerException if <code>listeners</code> argument is <code>null</code>
* @throws IllegalArgumentException if any element of <code>listeners</code> is not concrete instance
*/
default void addListeners(Iterable<Listener<?>> listeners) throws NullPointerException, IllegalArgumentException {
stream(listeners.spliterator(), false).forEach(this::addListener);
default void addEventListeners(Iterable<EventListener<?>> listeners) throws NullPointerException, IllegalArgumentException {
stream(listeners.spliterator(), false).forEach(this::addEventListener);
}

/**
* Remove a a {@link Listener Dubbo event listener}
* Remove a a {@link EventListener Dubbo event listener}
*
* @param listener a {@link Listener Dubbo event listener}
* @param listener a {@link EventListener Dubbo event listener}
* @return If remove successfully, return <code>true</code>.
* If current {@link Listener} is existed, return <code>false</code>
* If current {@link EventListener} is existed, return <code>false</code>
* @throws NullPointerException if <code>listener</code> argument is <code>null</code>
*/
void removeListener(Listener<?> listener) throws NullPointerException, IllegalArgumentException;
void removeEventListener(EventListener<?> listener) throws NullPointerException, IllegalArgumentException;

/**
* Remove a {@link Listener Dubbo event listener}
* Remove a {@link EventListener Dubbo event listener}
*
* @param listeners the {@link Listener Dubbo event listeners}
* @param listeners the {@link EventListener Dubbo event listeners}
* @return If remove successfully, return <code>true</code>.
* If current {@link Listener} is existed, return <code>false</code>
* If current {@link EventListener} is existed, return <code>false</code>
* @throws NullPointerException if <code>listener</code> argument is <code>null</code>
* @throws IllegalArgumentException if any element of <code>listeners</code> is not concrete instance
*/
default void removeListeners(Iterable<Listener<?>> listeners) throws NullPointerException, IllegalArgumentException {
stream(listeners.spliterator(), false).forEach(this::removeListener);
default void removeListeners(Iterable<EventListener<?>> listeners) throws NullPointerException, IllegalArgumentException {
stream(listeners.spliterator(), false).forEach(this::removeEventListener);
}

/**
* Remove all {@link Listener Dubbo event listeners}
* Remove all {@link EventListener Dubbo event listeners}
*
* @return a amount of removed listeners
*/
default void removeAllListeners() {
removeListeners(getAllListeners());
default void removeAllEventListeners() {
removeListeners(getAllEventListeners());
}

/**
* Get all registered {@link Listener Dubbo event listeners}
* Get all registered {@link EventListener Dubbo event listeners}
*
* @return non-null read-only ordered {@link Listener Dubbo event listeners}
* @see Listener#getPriority()
* @return non-null read-only ordered {@link EventListener Dubbo event listeners}
* @see EventListener#getPriority()
*/
List<Listener<?>> getAllListeners();
List<EventListener<?>> getAllEventListeners();

/**
* Dispatch a Dubbo event to the registered {@link Listener Dubbo event listeners}
* Dispatch a Dubbo event to the registered {@link EventListener Dubbo event listeners}
*
* @param event a {@link Event Dubbo event}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,25 @@

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.EventListener;
import java.util.Objects;

import static java.lang.Integer.compare;
import static org.apache.dubbo.common.utils.ReflectUtils.findParameterizedTypes;

/**
* The {@link Event Dubbo Event} Listener that is based on Java standard {@link EventListener} interface supports
* The {@link Event Dubbo Event} Listener that is based on Java standard {@link java.util.EventListener} interface supports
* the generic {@link Event}.
* <p>
* The {@link #onEvent(Event) handle method} will be notified when the matched-type {@link Event Dubbo Event} is
* published, whose priority could be changed by {@link #getPriority()} method.
*
* @param <E> the concrete class of {@link Event Dubbo Event}
* @see Event
* @see EventListener
* @see java.util.EventListener
* @since 2.7.2
*/
@FunctionalInterface
public interface Listener<E extends Event> extends EventListener, Comparable<Listener<E>> {
public interface EventListener<E extends Event> extends java.util.EventListener, Comparable<EventListener<E>> {

/**
* Handle a {@link Event Dubbo Event} when it's be published
Expand All @@ -47,12 +46,12 @@ public interface Listener<E extends Event> extends EventListener, Comparable<Lis
void onEvent(E event);

/**
* The priority of {@link Listener current listener}.
* The priority of {@link EventListener current listener}.
*
* @return the value is more greater, the priority is more lower.
* {@link Integer#MIN_VALUE} indicates the highest priority. The default value is {@link Integer#MAX_VALUE}.
* The comparison rule , refer to {@link #compareTo(Listener)}.
* @see #compareTo(Listener)
* The comparison rule , refer to {@link #compareTo(EventListener)}.
* @see #compareTo(EventListener)
* @see Integer#MAX_VALUE
* @see Integer#MIN_VALUE
*/
Expand All @@ -61,33 +60,33 @@ default int getPriority() {
}

@Override
default int compareTo(Listener<E> another) {
default int compareTo(EventListener<E> another) {
return compare(this.getPriority(), another.getPriority());
}

/**
* Find the {@link Class type} {@link Event Dubbo event} from the specified {@link Listener Dubbo event listener}
* Find the {@link Class type} {@link Event Dubbo event} from the specified {@link EventListener Dubbo event listener}
*
* @param listener the {@link Class class} of {@link Listener Dubbo event listener}
* @param listener the {@link Class class} of {@link EventListener Dubbo event listener}
* @return <code>null</code> if not found
*/
static Class<? extends Event> findEventType(Listener<?> listener) {
static Class<? extends Event> findEventType(EventListener<?> listener) {
return findEventType(listener.getClass());
}

/**
* Find the {@link Class type} {@link Event Dubbo event} from the specified {@link Listener Dubbo event listener}
* Find the {@link Class type} {@link Event Dubbo event} from the specified {@link EventListener Dubbo event listener}
*
* @param listenerClass the {@link Class class} of {@link Listener Dubbo event listener}
* @param listenerClass the {@link Class class} of {@link EventListener Dubbo event listener}
* @return <code>null</code> if not found
*/
static Class<? extends Event> findEventType(Class<?> listenerClass) {
Class<? extends Event> eventType = null;

if (listenerClass != null && Listener.class.isAssignableFrom(listenerClass)) {
if (listenerClass != null && EventListener.class.isAssignableFrom(listenerClass)) {
eventType = findParameterizedTypes(listenerClass)
.stream()
.map(Listener::findEventType)
.map(EventListener::findEventType)
.filter(Objects::nonNull)
.findAny()
.get();
Expand All @@ -98,16 +97,16 @@ static Class<? extends Event> findEventType(Class<?> listenerClass) {

/**
* Find the type {@link Event Dubbo event} from the specified {@link ParameterizedType} presents
* a class of {@link Listener Dubbo event listener}
* a class of {@link EventListener Dubbo event listener}
*
* @param parameterizedType the {@link ParameterizedType} presents a class of {@link Listener Dubbo event listener}
* @param parameterizedType the {@link ParameterizedType} presents a class of {@link EventListener Dubbo event listener}
* @return <code>null</code> if not found
*/
static Class<? extends Event> findEventType(ParameterizedType parameterizedType) {
Class<? extends Event> eventType = null;

Type rawType = parameterizedType.getRawType();
if ((rawType instanceof Class) && Listener.class.isAssignableFrom((Class) rawType)) {
if ((rawType instanceof Class) && EventListener.class.isAssignableFrom((Class) rawType)) {
Type[] typeArguments = parameterizedType.getActualTypeArguments();
for (Type typeArgument : typeArguments) {
if (typeArgument instanceof Class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.concurrent.atomic.AtomicInteger;

public abstract class AbstractListener<E extends Event> implements Listener<E> {
public abstract class AbstractEventListener<E extends Event> implements EventListener<E> {

private final AtomicInteger eventOccurs = new AtomicInteger(0);

Expand Down
Loading

0 comments on commit 18be1c9

Please sign in to comment.