Skip to content

Commit 7fbe0aa

Browse files
committed
ConditionBuilder API is now fluent
1 parent b3b691b commit 7fbe0aa

21 files changed

Lines changed: 401 additions & 42 deletions

File tree

‎api/src/main/java/com/ocpsoft/rewrite/config/And.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2828
*/
29-
public class And implements Condition
29+
public class And extends ConditionBuilder
3030
{
3131
private final List<Condition> conditions;
3232

‎api/src/main/java/com/ocpsoft/rewrite/config/Condition.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import com.ocpsoft.rewrite.event.Rewrite;
1919

2020
/**
21-
* A condition that must be met in order for evaluation to return true.
21+
* A condition that must be met in order for evaluation to return true. You may create custom {@link Condition}
22+
* implementations. If creating custom implementations, you should likely extend {@link ConditionBuilder}, which adds
23+
* logical operators to any class extending it.
2224
*
2325
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2426
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
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+
package com.ocpsoft.rewrite.config;
17+
18+
/**
19+
* Used as a base class to create fluent relationships between {@link Condition} objects; this class adds logical
20+
* operators to any class extending it.
21+
*
22+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
23+
*/
24+
public abstract class ConditionBuilder implements Condition
25+
{
26+
public ConditionBuilder and(final Condition condition)
27+
{
28+
return And.all(this, condition);
29+
}
30+
31+
public ConditionBuilder andNot(final Condition condition)
32+
{
33+
return And.all(this, Not.any(condition));
34+
}
35+
36+
public ConditionBuilder or(final Condition condition)
37+
{
38+
return Or.any(this, condition);
39+
}
40+
41+
public ConditionBuilder orNot(final Condition condition)
42+
{
43+
return Or.any(this, Not.any(condition));
44+
}
45+
}

‎api/src/main/java/com/ocpsoft/rewrite/config/ConfigurationLoader.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public class ConfigurationLoader
3232
{
3333
public static Logger log = Logger.getLogger(ConfigurationLoader.class);
3434

35+
@SuppressWarnings({ "rawtypes", "unchecked" })
3536
public static Configuration loadConfiguration(final Object context)
3637
{
37-
@SuppressWarnings("unchecked")
3838
ServiceLoader<ConfigurationProvider> loader = ServiceLoader.load(ConfigurationProvider.class);
3939
List<ConfigurationProvider> providers = Iterators.asList(loader.iterator());
4040

api/src/main/java/com/ocpsoft/rewrite/config/Conditions.java renamed to api/src/main/java/com/ocpsoft/rewrite/config/Direction.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,17 @@
1616
package com.ocpsoft.rewrite.config;
1717

1818
/**
19-
* Abstract access to conditional primitives.
20-
*
2119
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
22-
*
2320
*/
24-
public abstract class Conditions
21+
public abstract class Direction extends ConditionBuilder
2522
{
26-
public Not not(final Condition condition)
27-
{
28-
return Not.any(condition);
29-
}
30-
31-
public And and(final Condition... conditions)
23+
public static Inbound isInbound()
3224
{
33-
return And.all(conditions);
25+
return Inbound.only();
3426
}
3527

36-
public Or or(final Condition... conditions)
28+
public static Outbound isOutbound()
3729
{
38-
return Or.any(conditions);
30+
return Outbound.only();
3931
}
4032
}

‎api/src/main/java/com/ocpsoft/rewrite/config/False.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2424
*
2525
*/
26-
public class False implements Condition
26+
public class False extends ConditionBuilder
2727
{
2828
@Override
2929
public boolean evaluate(final Rewrite event)

‎api/src/main/java/com/ocpsoft/rewrite/config/Inbound.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2525
*/
26-
public class Inbound implements Condition
26+
public class Inbound extends ConditionBuilder
2727
{
2828
private Inbound()
2929
{}

‎api/src/main/java/com/ocpsoft/rewrite/config/Not.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@
2323
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2424
*
2525
*/
26-
public class Not implements Condition
26+
public class Not extends ConditionBuilder
2727
{
2828
private final Condition condition;
2929

3030
private Not(final Condition condition)
31-
3231
{
3332
this.condition = condition;
3433
}

‎api/src/main/java/com/ocpsoft/rewrite/config/Or.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2727
*
2828
*/
29-
public class Or implements Condition
29+
public class Or extends ConditionBuilder
3030
{
3131
private final List<Condition> conditions;
3232

‎api/src/main/java/com/ocpsoft/rewrite/config/Outbound.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@
2323
*
2424
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2525
*/
26-
public class Outbound implements Condition
26+
public class Outbound extends ConditionBuilder
2727
{
28+
public Outbound()
29+
{}
30+
2831
@Override
2932
public boolean evaluate(final Rewrite event)
3033
{
3134
return event instanceof OutboundRewrite;
3235
}
36+
37+
public static Outbound only()
38+
{
39+
return new Outbound();
40+
}
3341
}

0 commit comments

Comments
 (0)