diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java index 0992bb6172..3af30ee6e2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java @@ -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 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 outcomes); + protected abstract ConditionOutcome getFinalMatchOutcome( + MemberMatchOutcomes memberOutcomes); + + protected static class MemberMatchOutcomes { + + private final List all; + + private final List matches; + + private final List nonMatches; + + public MemberMatchOutcomes(MemberConditions memberConditions) { + this.all = Collections.unmodifiableList(memberConditions.getMatchOutcomes()); + List matches = new ArrayList(); + List nonMatches = new ArrayList(); + for (ConditionOutcome outcome : this.all) { + (outcome.isMatch() ? matches : nonMatches).add(outcome); + } + this.matches = Collections.unmodifiableList(matches); + this.nonMatches = Collections.unmodifiableList(nonMatches); + } + + public List getAll() { + return this.all; + } + + public List getMatches() { + return this.matches; + } + + public List getNonMatches() { + return this.nonMatches; + } + + } private static class MemberConditions { diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AllNestedConditions.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AllNestedConditions.java index 69e5077b0e..3bbca0c42b 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AllNestedConditions.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AllNestedConditions.java @@ -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: * *
  * static class OnJndiOrProperty extends AllNestedConditions {
@@ -33,20 +46,11 @@ public abstract class AllNestedConditions extends AbstractNestedCondition {
 	}
 
 	@Override
-	protected ConditionOutcome buildConditionOutcome(List outcomes) {
-		List match = new ArrayList();
-		List nonMatch = new ArrayList();
-		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");
 	}
 
 }
diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AnyNestedCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AnyNestedCondition.java
index 02779b9e9f..3b8bc151ee 100644
--- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AnyNestedCondition.java
+++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AnyNestedCondition.java
@@ -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:
  *
  * 
  * static class OnJndiOrProperty extends AnyNestedCondition {
@@ -42,7 +39,7 @@ import org.springframework.core.annotation.Order;
  * 
* * @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 outcomes) { - List match = new ArrayList(); - List nonMatch = new ArrayList(); - 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"); + } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/NoneNestedConditions.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/NoneNestedConditions.java new file mode 100644 index 0000000000..e2287723f0 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/NoneNestedConditions.java @@ -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: + * + *
+ * static class OnJndiOrProperty extends NoneOfNestedConditions {
+ *
+ *    @ConditionalOnJndi()
+ *    static class OnJndi {
+ *    }
+
+ *    @ConditionalOnProperty("something")
+ *    static class OnProperty {
+ *    }
+ *
+ * }
+ * 
+ * + * @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"); + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/NoneOfNestedConditions.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/NoneOfNestedConditions.java deleted file mode 100644 index 292386541f..0000000000 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/NoneOfNestedConditions.java +++ /dev/null @@ -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: - * - *
- * static class OnJndiOrProperty extends NoneOfNestedConditions {
- *
- *    @ConditionalOnJndi()
- *    static class OnJndi {
- *    }
-
- *    @ConditionalOnProperty("something")
- *    static class OnProperty {
- *    }
- *
- * }
- * 
- * - * @author Phillip Webb - * @since 1.2.0 - */ -public abstract class NoneOfNestedConditions extends AbstractNestedCondition { - - public NoneOfNestedConditions(ConfigurationPhase configurationPhase) { - super(configurationPhase); - } - - @Override - protected ConditionOutcome buildConditionOutcome(List outcomes) { - List match = new ArrayList(); - List nonMatch = new ArrayList(); - 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"); - } - -} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AllNestedConditionsTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AllNestedConditionsTests.java index 94945c8224..b22d10927e 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AllNestedConditionsTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AllNestedConditionsTests.java @@ -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}. */ diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java index ad1fbb1d12..0c49db89fb 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java @@ -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. diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/NoneOfNestedConditionsTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/NoneNestedConditionsTests.java similarity index 94% rename from spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/NoneOfNestedConditionsTests.java rename to spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/NoneNestedConditionsTests.java index ee9f536d9d..aca0c6f74d 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/NoneOfNestedConditionsTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/NoneNestedConditionsTests.java @@ -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);