Skip to content

Commit 138d667

Browse files
committed
Polish nested conditions
1 parent 1f202e3 commit 138d667

File tree

8 files changed

+143
-106
lines changed

8 files changed

+143
-106
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java

+59-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package org.springframework.boot.autoconfigure.condition;
218

319
import java.io.IOException;
@@ -21,7 +37,10 @@
2137
import org.springframework.util.MultiValueMap;
2238
import org.springframework.util.StringUtils;
2339

24-
public abstract class AbstractNestedCondition extends SpringBootCondition implements
40+
/**
41+
* @author Phillip Webb
42+
*/
43+
abstract class AbstractNestedCondition extends SpringBootCondition implements
2544
ConfigurationCondition {
2645

2746
private final ConfigurationPhase configurationPhase;
@@ -39,14 +58,47 @@ public ConfigurationPhase getConfigurationPhase() {
3958
@Override
4059
public ConditionOutcome getMatchOutcome(ConditionContext context,
4160
AnnotatedTypeMetadata metadata) {
42-
MemberConditions memberConditions = new MemberConditions(context, getClass()
43-
.getName());
44-
List<ConditionOutcome> outcomes = memberConditions.getMatchOutcomes();
45-
return buildConditionOutcome(outcomes);
61+
String className = getClass().getName();
62+
MemberConditions memberConditions = new MemberConditions(context, className);
63+
MemberMatchOutcomes memberOutcomes = new MemberMatchOutcomes(memberConditions);
64+
return getFinalMatchOutcome(memberOutcomes);
4665
}
4766

48-
protected abstract ConditionOutcome buildConditionOutcome(
49-
List<ConditionOutcome> outcomes);
67+
protected abstract ConditionOutcome getFinalMatchOutcome(
68+
MemberMatchOutcomes memberOutcomes);
69+
70+
protected static class MemberMatchOutcomes {
71+
72+
private final List<ConditionOutcome> all;
73+
74+
private final List<ConditionOutcome> matches;
75+
76+
private final List<ConditionOutcome> nonMatches;
77+
78+
public MemberMatchOutcomes(MemberConditions memberConditions) {
79+
this.all = Collections.unmodifiableList(memberConditions.getMatchOutcomes());
80+
List<ConditionOutcome> matches = new ArrayList<ConditionOutcome>();
81+
List<ConditionOutcome> nonMatches = new ArrayList<ConditionOutcome>();
82+
for (ConditionOutcome outcome : this.all) {
83+
(outcome.isMatch() ? matches : nonMatches).add(outcome);
84+
}
85+
this.matches = Collections.unmodifiableList(matches);
86+
this.nonMatches = Collections.unmodifiableList(nonMatches);
87+
}
88+
89+
public List<ConditionOutcome> getAll() {
90+
return this.all;
91+
}
92+
93+
public List<ConditionOutcome> getMatches() {
94+
return this.matches;
95+
}
96+
97+
public List<ConditionOutcome> getNonMatches() {
98+
return this.nonMatches;
99+
}
100+
101+
}
50102

51103
private static class MemberConditions {
52104

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1-
package org.springframework.boot.autoconfigure.condition;
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
216

3-
import java.util.ArrayList;
4-
import java.util.List;
17+
package org.springframework.boot.autoconfigure.condition;
518

619
import org.springframework.context.annotation.Condition;
720

821
/**
9-
* {@link Condition} that will match when all nested class conditions match.
10-
* Can be used to create composite conditions, for example:
22+
* {@link Condition} that will match when all nested class conditions match. Can be used
23+
* to create composite conditions, for example:
1124
*
1225
* <pre class="code">
1326
* static class OnJndiOrProperty extends AllNestedConditions {
@@ -33,20 +46,11 @@ public AllNestedConditions(ConfigurationPhase configurationPhase) {
3346
}
3447

3548
@Override
36-
protected ConditionOutcome buildConditionOutcome(List<ConditionOutcome> outcomes) {
37-
List<ConditionOutcome> match = new ArrayList<ConditionOutcome>();
38-
List<ConditionOutcome> nonMatch = new ArrayList<ConditionOutcome>();
39-
for (ConditionOutcome outcome : outcomes) {
40-
if (outcome.isMatch()) {
41-
match.add(outcome);
42-
}
43-
else {
44-
nonMatch.add(outcome);
45-
}
46-
}
47-
return new ConditionOutcome(match.size() == outcomes.size(),
48-
"all match resulted in " + match + " matches and " + nonMatch
49-
+ " non matches");
49+
protected ConditionOutcome getFinalMatchOutcome(MemberMatchOutcomes memberOutcomes) {
50+
return new ConditionOutcome(memberOutcomes.getMatches().size() == memberOutcomes
51+
.getAll().size(), "nested all match resulted in "
52+
+ memberOutcomes.getMatches() + " matches and "
53+
+ memberOutcomes.getNonMatches() + " non matches");
5054
}
5155

5256
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AnyNestedCondition.java

+9-18
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@
1616

1717
package org.springframework.boot.autoconfigure.condition;
1818

19-
import java.util.ArrayList;
20-
import java.util.List;
21-
2219
import org.springframework.context.annotation.Condition;
2320
import org.springframework.core.Ordered;
2421
import org.springframework.core.annotation.Order;
2522

2623
/**
27-
* {@link Condition} that will match when any nested class condition matches.
28-
* Can be used to create composite conditions, for example:
24+
* {@link Condition} that will match when any nested class condition matches. Can be used
25+
* to create composite conditions, for example:
2926
*
3027
* <pre class="code">
3128
* static class OnJndiOrProperty extends AnyNestedCondition {
@@ -42,7 +39,7 @@
4239
* </pre>
4340
*
4441
* @author Phillip Webb
45-
* @since 1.2.0
42+
* @since 1.3.0
4643
*/
4744
@Order(Ordered.LOWEST_PRECEDENCE - 20)
4845
public abstract class AnyNestedCondition extends AbstractNestedCondition {
@@ -52,18 +49,12 @@ public AnyNestedCondition(ConfigurationPhase configurationPhase) {
5249
}
5350

5451
@Override
55-
protected ConditionOutcome buildConditionOutcome(List<ConditionOutcome> outcomes) {
56-
List<ConditionOutcome> match = new ArrayList<ConditionOutcome>();
57-
List<ConditionOutcome> nonMatch = new ArrayList<ConditionOutcome>();
58-
for (ConditionOutcome outcome : outcomes) {
59-
if (outcome.isMatch()) {
60-
match.add(outcome);
61-
} else {
62-
nonMatch.add(outcome);
63-
}
64-
}
65-
return new ConditionOutcome(match.size() > 0, "any match resulted in " + match + " matches and " + nonMatch
66-
+ " non matches");
52+
protected ConditionOutcome getFinalMatchOutcome(MemberMatchOutcomes memberOutcomes) {
53+
return new ConditionOutcome(memberOutcomes.getMatches().size() > 0,
54+
"nested any match resulted in " + memberOutcomes.getMatches()
55+
+ " matches and " + memberOutcomes.getNonMatches()
56+
+ " non matches");
57+
6758
}
6859

6960
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.springframework.boot.autoconfigure.condition;
2+
3+
import org.springframework.context.annotation.Condition;
4+
5+
/**
6+
* {@link Condition} that will match when none of the nested class conditions match. Can
7+
* be used to create composite conditions, for example:
8+
*
9+
* <pre class="code">
10+
* static class OnJndiOrProperty extends NoneOfNestedConditions {
11+
*
12+
* &#064;ConditionalOnJndi()
13+
* static class OnJndi {
14+
* }
15+
16+
* &#064;ConditionalOnProperty("something")
17+
* static class OnProperty {
18+
* }
19+
*
20+
* }
21+
* </pre>
22+
*
23+
* @author Phillip Webb
24+
* @since 1.3.0
25+
*/
26+
public abstract class NoneNestedConditions extends AbstractNestedCondition {
27+
28+
public NoneNestedConditions(ConfigurationPhase configurationPhase) {
29+
super(configurationPhase);
30+
}
31+
32+
@Override
33+
protected ConditionOutcome getFinalMatchOutcome(MemberMatchOutcomes memberOutcomes) {
34+
return new ConditionOutcome(memberOutcomes.getMatches().isEmpty(),
35+
"nested none match resulted in " + memberOutcomes.getMatches()
36+
+ " matches and " + memberOutcomes.getNonMatches()
37+
+ " non matches");
38+
}
39+
40+
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/NoneOfNestedConditions.java

-50
This file was deleted.

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AllNestedConditionsTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,16 @@
1616

1717
package org.springframework.boot.autoconfigure.condition;
1818

19-
import static org.hamcrest.Matchers.equalTo;
20-
import static org.junit.Assert.assertThat;
21-
2219
import org.junit.Test;
2320
import org.springframework.boot.test.EnvironmentTestUtils;
2421
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2522
import org.springframework.context.annotation.Bean;
2623
import org.springframework.context.annotation.Conditional;
2724
import org.springframework.context.annotation.Configuration;
2825

26+
import static org.hamcrest.Matchers.equalTo;
27+
import static org.junit.Assert.assertThat;
28+
2929
/**
3030
* Tests for {@link AllNestedConditions}.
3131
*/

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,20 +16,20 @@
1616

1717
package org.springframework.boot.autoconfigure.condition;
1818

19-
import static org.hamcrest.Matchers.equalTo;
20-
import static org.junit.Assert.assertThat;
21-
2219
import org.junit.Test;
2320
import org.springframework.boot.test.EnvironmentTestUtils;
2421
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2522
import org.springframework.context.annotation.Bean;
2623
import org.springframework.context.annotation.Conditional;
2724
import org.springframework.context.annotation.Configuration;
2825

26+
import static org.hamcrest.Matchers.equalTo;
27+
import static org.junit.Assert.assertThat;
28+
2929
/**
30-
* Tests for {@link NoneOfNestedConditions}.
30+
* Tests for {@link NoneNestedConditions}.
3131
*/
32-
public class NoneOfNestedConditionsTests {
32+
public class NoneNestedConditionsTests {
3333

3434
@Test
3535
public void neither() throws Exception {
@@ -78,7 +78,7 @@ public String myBean() {
7878

7979
}
8080

81-
static class NeitherPropertyANorPropertyBCondition extends NoneOfNestedConditions {
81+
static class NeitherPropertyANorPropertyBCondition extends NoneNestedConditions {
8282

8383
public NeitherPropertyANorPropertyBCondition() {
8484
super(ConfigurationPhase.PARSE_CONFIGURATION);

0 commit comments

Comments
 (0)