GH-1396: Declarables constructor is too strict

Resolves https://github.com/spring-projects/spring-amqp/issues/1396

* Make constructor more flexible by upper bounded wildcard
* Review amendments
This commit is contained in:
Björn Michael
2021-12-02 04:24:21 +01:00
committed by GitHub
parent a7fd715479
commit aeabc562ef
2 changed files with 59 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 the original author or authors.
* Copyright 2018-2021 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.
@@ -30,6 +30,7 @@ import org.springframework.util.ObjectUtils;
* broker using a single bean declaration for the collection.
*
* @author Gary Russell
* @author Björn Michael
* @since 2.1
*/
public class Declarables {
@@ -42,7 +43,7 @@ public class Declarables {
}
}
public Declarables(Collection<Declarable> declarables) {
public Declarables(Collection<? extends Declarable> declarables) {
Assert.notNull(declarables, "declarables cannot be null");
this.declarables.addAll(declarables);
}
@@ -58,11 +59,10 @@ public class Declarables {
* @return the filtered list.
* @since 2.2
*/
@SuppressWarnings("unchecked")
public <T> List<T> getDeclarablesByType(Class<T> type) {
return this.declarables.stream()
.filter(type::isInstance)
.map(dec -> (T) dec)
.map(type::cast)
.collect(Collectors.toList());
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2021 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.amqp.core;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.jupiter.api.Test;
/**
* @author Björn Michael
* @since 2.4
*/
public class DeclarablesTests {
@Test
public void getDeclarables() {
List<Queue> queues = List.of(
new Queue("q1", false, false, true),
new Queue("q2", false, false, true));
Declarables declarables = new Declarables(queues);
assertThat(declarables.getDeclarables()).hasSameElementsAs(queues);
}
@Test
public void getDeclarablesByType() {
Queue queue = new Queue("queue");
TopicExchange exchange = new TopicExchange("exchange");
Binding binding = BindingBuilder.bind(queue).to(exchange).with("foo.bar");
Declarables declarables = new Declarables(queue, exchange, binding);
assertThat(declarables.getDeclarablesByType(Queue.class)).containsExactlyInAnyOrder(queue);
assertThat(declarables.getDeclarablesByType(Exchange.class)).containsExactlyInAnyOrder(exchange);
assertThat(declarables.getDeclarablesByType(Binding.class)).containsExactlyInAnyOrder(binding);
assertThat(declarables.getDeclarablesByType(Declarable.class)).containsExactlyInAnyOrder(
queue, exchange, binding);
}
}