mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Polish nested conditions
This commit is contained in:
parent
1f202e3e47
commit
138d66706a
@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2012-2015 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
|
||||
*
|
||||
* http://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.boot.autoconfigure.condition;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -21,7 +37,10 @@ import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public abstract class AbstractNestedCondition extends SpringBootCondition implements
|
||||
/**
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
abstract class AbstractNestedCondition extends SpringBootCondition implements
|
||||
ConfigurationCondition {
|
||||
|
||||
private final ConfigurationPhase configurationPhase;
|
||||
@ -39,14 +58,47 @@ public abstract class AbstractNestedCondition extends SpringBootCondition implem
|
||||
@Override
|
||||
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
||||
AnnotatedTypeMetadata metadata) {
|
||||
MemberConditions memberConditions = new MemberConditions(context, getClass()
|
||||
.getName());
|
||||
List<ConditionOutcome> outcomes = memberConditions.getMatchOutcomes();
|
||||
return buildConditionOutcome(outcomes);
|
||||
String className = getClass().getName();
|
||||
MemberConditions memberConditions = new MemberConditions(context, className);
|
||||
MemberMatchOutcomes memberOutcomes = new MemberMatchOutcomes(memberConditions);
|
||||
return getFinalMatchOutcome(memberOutcomes);
|
||||
}
|
||||
|
||||
protected abstract ConditionOutcome buildConditionOutcome(
|
||||
List<ConditionOutcome> outcomes);
|
||||
protected abstract ConditionOutcome getFinalMatchOutcome(
|
||||
MemberMatchOutcomes memberOutcomes);
|
||||
|
||||
protected static class MemberMatchOutcomes {
|
||||
|
||||
private final List<ConditionOutcome> all;
|
||||
|
||||
private final List<ConditionOutcome> matches;
|
||||
|
||||
private final List<ConditionOutcome> nonMatches;
|
||||
|
||||
public MemberMatchOutcomes(MemberConditions memberConditions) {
|
||||
this.all = Collections.unmodifiableList(memberConditions.getMatchOutcomes());
|
||||
List<ConditionOutcome> matches = new ArrayList<ConditionOutcome>();
|
||||
List<ConditionOutcome> nonMatches = new ArrayList<ConditionOutcome>();
|
||||
for (ConditionOutcome outcome : this.all) {
|
||||
(outcome.isMatch() ? matches : nonMatches).add(outcome);
|
||||
}
|
||||
this.matches = Collections.unmodifiableList(matches);
|
||||
this.nonMatches = Collections.unmodifiableList(nonMatches);
|
||||
}
|
||||
|
||||
public List<ConditionOutcome> getAll() {
|
||||
return this.all;
|
||||
}
|
||||
|
||||
public List<ConditionOutcome> getMatches() {
|
||||
return this.matches;
|
||||
}
|
||||
|
||||
public List<ConditionOutcome> getNonMatches() {
|
||||
return this.nonMatches;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class MemberConditions {
|
||||
|
||||
|
@ -1,13 +1,26 @@
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
/*
|
||||
* Copyright 2012-2015 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
|
||||
*
|
||||
* http://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.
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import org.springframework.context.annotation.Condition;
|
||||
|
||||
/**
|
||||
* {@link Condition} that will match when all nested class conditions match.
|
||||
* Can be used to create composite conditions, for example:
|
||||
* {@link Condition} that will match when all nested class conditions match. Can be used
|
||||
* to create composite conditions, for example:
|
||||
*
|
||||
* <pre class="code">
|
||||
* static class OnJndiOrProperty extends AllNestedConditions {
|
||||
@ -33,20 +46,11 @@ public abstract class AllNestedConditions extends AbstractNestedCondition {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ConditionOutcome buildConditionOutcome(List<ConditionOutcome> outcomes) {
|
||||
List<ConditionOutcome> match = new ArrayList<ConditionOutcome>();
|
||||
List<ConditionOutcome> nonMatch = new ArrayList<ConditionOutcome>();
|
||||
for (ConditionOutcome outcome : outcomes) {
|
||||
if (outcome.isMatch()) {
|
||||
match.add(outcome);
|
||||
}
|
||||
else {
|
||||
nonMatch.add(outcome);
|
||||
}
|
||||
}
|
||||
return new ConditionOutcome(match.size() == outcomes.size(),
|
||||
"all match resulted in " + match + " matches and " + nonMatch
|
||||
+ " non matches");
|
||||
protected ConditionOutcome getFinalMatchOutcome(MemberMatchOutcomes memberOutcomes) {
|
||||
return new ConditionOutcome(memberOutcomes.getMatches().size() == memberOutcomes
|
||||
.getAll().size(), "nested all match resulted in "
|
||||
+ memberOutcomes.getMatches() + " matches and "
|
||||
+ memberOutcomes.getNonMatches() + " non matches");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,16 +16,13 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
/**
|
||||
* {@link Condition} that will match when any nested class condition matches.
|
||||
* Can be used to create composite conditions, for example:
|
||||
* {@link Condition} that will match when any nested class condition matches. Can be used
|
||||
* to create composite conditions, for example:
|
||||
*
|
||||
* <pre class="code">
|
||||
* static class OnJndiOrProperty extends AnyNestedCondition {
|
||||
@ -42,7 +39,7 @@ import org.springframework.core.annotation.Order;
|
||||
* </pre>
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.2.0
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@Order(Ordered.LOWEST_PRECEDENCE - 20)
|
||||
public abstract class AnyNestedCondition extends AbstractNestedCondition {
|
||||
@ -52,18 +49,12 @@ public abstract class AnyNestedCondition extends AbstractNestedCondition {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ConditionOutcome buildConditionOutcome(List<ConditionOutcome> outcomes) {
|
||||
List<ConditionOutcome> match = new ArrayList<ConditionOutcome>();
|
||||
List<ConditionOutcome> nonMatch = new ArrayList<ConditionOutcome>();
|
||||
for (ConditionOutcome outcome : outcomes) {
|
||||
if (outcome.isMatch()) {
|
||||
match.add(outcome);
|
||||
} else {
|
||||
nonMatch.add(outcome);
|
||||
}
|
||||
}
|
||||
return new ConditionOutcome(match.size() > 0, "any match resulted in " + match + " matches and " + nonMatch
|
||||
+ " non matches");
|
||||
protected ConditionOutcome getFinalMatchOutcome(MemberMatchOutcomes memberOutcomes) {
|
||||
return new ConditionOutcome(memberOutcomes.getMatches().size() > 0,
|
||||
"nested any match resulted in " + memberOutcomes.getMatches()
|
||||
+ " matches and " + memberOutcomes.getNonMatches()
|
||||
+ " non matches");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,40 @@
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import org.springframework.context.annotation.Condition;
|
||||
|
||||
/**
|
||||
* {@link Condition} that will match when none of the nested class conditions match. Can
|
||||
* be used to create composite conditions, for example:
|
||||
*
|
||||
* <pre class="code">
|
||||
* static class OnJndiOrProperty extends NoneOfNestedConditions {
|
||||
*
|
||||
* @ConditionalOnJndi()
|
||||
* static class OnJndi {
|
||||
* }
|
||||
|
||||
* @ConditionalOnProperty("something")
|
||||
* static class OnProperty {
|
||||
* }
|
||||
*
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public abstract class NoneNestedConditions extends AbstractNestedCondition {
|
||||
|
||||
public NoneNestedConditions(ConfigurationPhase configurationPhase) {
|
||||
super(configurationPhase);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ConditionOutcome getFinalMatchOutcome(MemberMatchOutcomes memberOutcomes) {
|
||||
return new ConditionOutcome(memberOutcomes.getMatches().isEmpty(),
|
||||
"nested none match resulted in " + memberOutcomes.getMatches()
|
||||
+ " matches and " + memberOutcomes.getNonMatches()
|
||||
+ " non matches");
|
||||
}
|
||||
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.annotation.Condition;
|
||||
|
||||
/**
|
||||
* {@link Condition} that will match when none of the nested class conditions match.
|
||||
* Can be used to create composite conditions, for example:
|
||||
*
|
||||
* <pre class="code">
|
||||
* static class OnJndiOrProperty extends NoneOfNestedConditions {
|
||||
*
|
||||
* @ConditionalOnJndi()
|
||||
* static class OnJndi {
|
||||
* }
|
||||
|
||||
* @ConditionalOnProperty("something")
|
||||
* static class OnProperty {
|
||||
* }
|
||||
*
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public abstract class NoneOfNestedConditions extends AbstractNestedCondition {
|
||||
|
||||
public NoneOfNestedConditions(ConfigurationPhase configurationPhase) {
|
||||
super(configurationPhase);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ConditionOutcome buildConditionOutcome(List<ConditionOutcome> outcomes) {
|
||||
List<ConditionOutcome> match = new ArrayList<ConditionOutcome>();
|
||||
List<ConditionOutcome> nonMatch = new ArrayList<ConditionOutcome>();
|
||||
for (ConditionOutcome outcome : outcomes) {
|
||||
if (outcome.isMatch()) {
|
||||
match.add(outcome);
|
||||
} else {
|
||||
nonMatch.add(outcome);
|
||||
}
|
||||
}
|
||||
return new ConditionOutcome(match.size() == 0, "none of match resulted in " + match + " matches and "
|
||||
+ nonMatch + " non matches");
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
* Copyright 2012-2015 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.
|
||||
@ -16,9 +16,6 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
@ -26,6 +23,9 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link AllNestedConditions}.
|
||||
*/
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
* Copyright 2012-2015 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.
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
* Copyright 2012-2015 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.
|
||||
@ -16,9 +16,6 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
@ -26,10 +23,13 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link NoneOfNestedConditions}.
|
||||
* Tests for {@link NoneNestedConditions}.
|
||||
*/
|
||||
public class NoneOfNestedConditionsTests {
|
||||
public class NoneNestedConditionsTests {
|
||||
|
||||
@Test
|
||||
public void neither() throws Exception {
|
||||
@ -78,7 +78,7 @@ public class NoneOfNestedConditionsTests {
|
||||
|
||||
}
|
||||
|
||||
static class NeitherPropertyANorPropertyBCondition extends NoneOfNestedConditions {
|
||||
static class NeitherPropertyANorPropertyBCondition extends NoneNestedConditions {
|
||||
|
||||
public NeitherPropertyANorPropertyBCondition() {
|
||||
super(ConfigurationPhase.PARSE_CONFIGURATION);
|
Loading…
Reference in New Issue
Block a user