-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The provider can be called with -DgroupsExpression=and(group1,group2) for which the grammar would support and(GROUP...), or(GROUP...) and not(GROUP) Signed-off-by: Alex ROBUCHON <alex.robuchon@forgerock.com>
- Loading branch information
Alex ROBUCHON
committed
Feb 16, 2018
1 parent
a3f9d90
commit 87cd3f2
Showing
18 changed files
with
881 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
cuppa/src/main/java/org/forgerock/cuppa/internal/filters/expression/AndCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2018 ForgeRock AS. | ||
* | ||
* 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.forgerock.cuppa.internal.filters.expression; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
/** | ||
* A condition that composes other conditions with a logical AND. | ||
*/ | ||
class AndCondition extends ConditionWrapper { | ||
|
||
public static final AndCondition EMPTY = new AndCondition(Collections.emptyList()); | ||
private final Collection<Condition> conditions; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param conditions a list of condition to compose. | ||
*/ | ||
AndCondition(Collection<Condition> conditions) { | ||
this.conditions = Collections.unmodifiableCollection(conditions); | ||
} | ||
|
||
@Override | ||
public boolean shouldRun(Collection<String> tags) { | ||
return conditions.stream().allMatch(c -> c.shouldRun(tags)); | ||
} | ||
|
||
@Override | ||
public ConditionWrapper setConditions(Collection<Condition> conditions) { | ||
return new AndCondition(conditions); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
cuppa/src/main/java/org/forgerock/cuppa/internal/filters/expression/Condition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2018 ForgeRock AS. | ||
* | ||
* 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.forgerock.cuppa.internal.filters.expression; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* A condition used by {@link ExpressionTagTestBlockFilter}. | ||
*/ | ||
@FunctionalInterface | ||
public interface Condition { | ||
|
||
/** | ||
* Check if the list of tags is compliant with the condition. | ||
* | ||
* @param tags The collection of tags. | ||
* @return true if the condition complies with the tags supplied, false otherwise. | ||
*/ | ||
boolean shouldRun(Collection<String> tags); | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
cuppa/src/main/java/org/forgerock/cuppa/internal/filters/expression/ConditionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.forgerock.cuppa.internal.filters.expression; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* A factory to creates {@link ConditionWrapper}. | ||
*/ | ||
final class ConditionFactory { | ||
|
||
/** | ||
* link the operator to an instance of {@link Condition}. | ||
*/ | ||
enum ConditionEnum { | ||
AND("and", AndCondition.EMPTY), | ||
OR("or", OrCondition.EMPTY), | ||
NOT("not", NotCondition.EMPTY); | ||
|
||
private String operator; | ||
private ConditionWrapper condition; | ||
|
||
ConditionEnum(String operator, ConditionWrapper condition) { | ||
this.operator = operator; | ||
this.condition = condition; | ||
} | ||
|
||
/** | ||
* Get a ConditionEnum fron an string operator. | ||
* @param operator The operator we want to get the associated enum | ||
* @return The ConditionEnum | ||
*/ | ||
static ConditionEnum getFromOperator(String operator) { | ||
return Arrays.stream(ConditionEnum.values()) | ||
.filter(c -> c.operator.equals(operator)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException(operator + " is not supported. The list of " | ||
+ "supported operator is and, or, not")); | ||
} | ||
|
||
Condition getCondition(List<Condition> tags) { | ||
return condition.setConditions(tags); | ||
} | ||
} | ||
|
||
private ConditionFactory() { | ||
} | ||
|
||
/** | ||
* Create a {@link Condition}. | ||
* @param operator The operator we want to create a condition for. | ||
* @param tags The list of conditions that {@link ConditionWrapper} will include. | ||
* @return A condition | ||
*/ | ||
static Condition get(String operator, List<Condition> tags) { | ||
return ConditionEnum.getFromOperator(operator.trim().toLowerCase()) | ||
.getCondition(tags); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
cuppa/src/main/java/org/forgerock/cuppa/internal/filters/expression/ConditionWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.forgerock.cuppa.internal.filters.expression; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* A condition wrapper for Condition that needs to wrap other conditions. | ||
*/ | ||
abstract class ConditionWrapper implements Condition { | ||
|
||
/** | ||
* Create a new Condition with the given conditions. | ||
* @param conditions the collection of conditions | ||
* @return a new condition | ||
*/ | ||
abstract ConditionWrapper setConditions(Collection<Condition> conditions); | ||
} |
41 changes: 41 additions & 0 deletions
41
cuppa/src/main/java/org/forgerock/cuppa/internal/filters/expression/ContainsCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2018 ForgeRock AS. | ||
* | ||
* 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.forgerock.cuppa.internal.filters.expression; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* A condition that checks if a tag is contains in a collection of tags. | ||
*/ | ||
class ContainsCondition implements Condition { | ||
|
||
private String tag; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param tag A group/tag we want to search for. | ||
*/ | ||
ContainsCondition(String tag) { | ||
this.tag = tag; | ||
} | ||
|
||
@Override | ||
public boolean shouldRun(Collection<String> tags) { | ||
return tags.contains(tag); | ||
} | ||
} |
Oops, something went wrong.