Skip to content

Commit

Permalink
Add support for functional registration of application listener
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Jan 3, 2024
1 parent efb97cc commit fba1dc5
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,8 @@

package org.springframework.context.event;

import java.util.function.Consumer;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.ResolvableType;
Expand Down Expand Up @@ -53,4 +55,38 @@ default boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
*/
boolean supportsEventType(ResolvableType eventType);

/**
* Create a new {@code ApplicationListener} for the given event type.
* @param eventType the event to listen to
* @param consumer the consumer to invoke when a matching event is fired
* @param <E> the specific {@code ApplicationEvent} subclass to listen to
* @return a corresponding {@code ApplicationListener} instance
* @since 6.1.3
*/
static <E extends ApplicationEvent> GenericApplicationListener forEventType(Class<E> eventType, Consumer<E> consumer) {
return new GenericApplicationListenerDelegate<>(eventType, consumer);
}

class GenericApplicationListenerDelegate<E extends ApplicationEvent> implements GenericApplicationListener {

private final Class<E> eventType;

private final Consumer<E> delegate;

public GenericApplicationListenerDelegate(Class<E> eventType, Consumer<E> delegate) {
this.eventType = eventType;
this.delegate = delegate;
}

@Override
public void onApplicationEvent(ApplicationEvent event) {
this.delegate.accept(this.eventType.cast(event));
}

@Override
public boolean supportsEventType(ResolvableType candidateEventType) {
return this.eventType.isAssignableFrom(candidateEventType.toClass());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* 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
*
* https://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.springframework.context.event;

import java.util.function.Consumer;

import org.junit.jupiter.api.Test;
import org.mockito.InOrder;

import org.springframework.core.ResolvableType;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;

/**
* Tests for {@link GenericApplicationListener}.
*
* @author Stephane Nicoll
*/
class GenericApplicationListenerTests extends AbstractApplicationEventListenerTests {

@Test
void forEventTypeWithStrictTypeMatching() {
GenericApplicationListener listener = GenericApplicationListener
.forEventType(StringEvent.class, event -> {});
assertThat(listener.supportsEventType(ResolvableType.forClass(StringEvent.class))).isTrue();
}

@Test
void forEventTypeWithSubClass() {
GenericApplicationListener listener = GenericApplicationListener
.forEventType(GenericTestEvent.class, event -> {});
assertThat(listener.supportsEventType(ResolvableType.forClass(StringEvent.class))).isTrue();
}

@Test
void forEventTypeWithSuperClass() {
GenericApplicationListener listener = GenericApplicationListener
.forEventType(StringEvent.class, event -> {});
assertThat(listener.supportsEventType(ResolvableType.forClass(GenericTestEvent.class))).isFalse();
}

@Test
void forEventTypeInvokesConsumer() {
Consumer<StringEvent> consumer = mock(Consumer.class);
GenericApplicationListener listener = GenericApplicationListener
.forEventType(StringEvent.class, consumer);
StringEvent event = new StringEvent(this, "one");
StringEvent event2 = new StringEvent(this, "two");
listener.onApplicationEvent(event);
listener.onApplicationEvent(event2);
InOrder ordered = inOrder(consumer);
ordered.verify(consumer).accept(event);
ordered.verify(consumer).accept(event2);
}

}

0 comments on commit fba1dc5

Please sign in to comment.