Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
138d6670
Commit
138d6670
authored
Sep 03, 2015
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish nested conditions
parent
1f202e3e
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
143 additions
and
56 deletions
+143
-56
AbstractNestedCondition.java
...boot/autoconfigure/condition/AbstractNestedCondition.java
+59
-7
AllNestedConditions.java
...ork/boot/autoconfigure/condition/AllNestedConditions.java
+23
-19
AnyNestedCondition.java
...work/boot/autoconfigure/condition/AnyNestedCondition.java
+9
-18
NoneNestedConditions.java
...rk/boot/autoconfigure/condition/NoneNestedConditions.java
+40
-0
AllNestedConditionsTests.java
...oot/autoconfigure/condition/AllNestedConditionsTests.java
+4
-4
AnyNestedConditionTests.java
...boot/autoconfigure/condition/AnyNestedConditionTests.java
+1
-1
NoneNestedConditionsTests.java
...ot/autoconfigure/condition/NoneNestedConditionsTests.java
+7
-7
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AbstractNestedCondition.java
View file @
138d6670
/*
* 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
(
o
utcomes
);
String
className
=
getClass
().
getName
();
MemberConditions
memberConditions
=
new
MemberConditions
(
context
,
className
);
MemberMatchOutcomes
memberOutcomes
=
new
MemberMatchOutcomes
(
memberConditions
);
return
getFinalMatchOutcome
(
memberO
utcomes
);
}
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
{
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AllNestedConditions.java
View file @
138d6670
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"
);
}
}
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/AnyNestedCondition.java
View file @
138d6670
...
...
@@ -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"
);
}
}
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/None
Of
NestedConditions.java
→
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/NoneNestedConditions.java
View file @
138d6670
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:
* {@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 {
...
...
@@ -24,27 +21,20 @@ import org.springframework.context.annotation.Condition;
* </pre>
*
* @author Phillip Webb
* @since 1.
2
.0
* @since 1.
3
.0
*/
public
abstract
class
None
Of
NestedConditions
extends
AbstractNestedCondition
{
public
abstract
class
NoneNestedConditions
extends
AbstractNestedCondition
{
public
None
Of
NestedConditions
(
ConfigurationPhase
configurationPhase
)
{
public
NoneNestedConditions
(
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"
);
protected
ConditionOutcome
getFinalMatchOutcome
(
MemberMatchOutcomes
memberOutcomes
)
{
return
new
ConditionOutcome
(
memberOutcomes
.
getMatches
().
isEmpty
(),
"nested none match resulted in "
+
memberOutcomes
.
getMatches
()
+
" matches and "
+
memberOutcomes
.
getNonMatches
()
+
" non matches"
);
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AllNestedConditionsTests.java
View file @
138d6670
/*
* Copyright 2012-201
4
the original author or authors.
* Copyright 2012-201
5
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}.
*/
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/AnyNestedConditionTests.java
View file @
138d6670
/*
* Copyright 2012-201
4
the original author or authors.
* Copyright 2012-201
5
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.
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/None
Of
NestedConditionsTests.java
→
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/NoneNestedConditionsTests.java
View file @
138d6670
/*
* Copyright 2012-201
4
the original author or authors.
* Copyright 2012-201
5
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 None
Of
NestedConditions}.
* Tests for {@link NoneNestedConditions}.
*/
public
class
None
Of
NestedConditionsTests
{
public
class
NoneNestedConditionsTests
{
@Test
public
void
neither
()
throws
Exception
{
...
...
@@ -78,7 +78,7 @@ public class NoneOfNestedConditionsTests {
}
static
class
NeitherPropertyANorPropertyBCondition
extends
None
Of
NestedConditions
{
static
class
NeitherPropertyANorPropertyBCondition
extends
NoneNestedConditions
{
public
NeitherPropertyANorPropertyBCondition
()
{
super
(
ConfigurationPhase
.
PARSE_CONFIGURATION
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment