Skip to content

Commit eff3760

Browse files
committed
Added the ability to tell if a condition has been negated
1 parent 49d9a9d commit eff3760

3 files changed

Lines changed: 101 additions & 2 deletions

File tree

‎api-tests/src/test/java/org/ocpsoft/rewrite/config/NotTest.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.junit.Assert;
1919
import org.junit.Test;
2020

21+
import org.ocpsoft.rewrite.context.EvaluationContext;
22+
import org.ocpsoft.rewrite.event.Rewrite;
2123
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
2224
import org.ocpsoft.rewrite.test.MockRewrite;
2325

@@ -40,4 +42,48 @@ public void testNotTrueIsFalse()
4042
Condition condition = Not.any(new True());
4143
Assert.assertFalse(condition.evaluate(new MockRewrite(), new MockEvaluationContext()));
4244
}
45+
46+
@Test
47+
public void testNotCountOdd()
48+
{
49+
Condition assertCountOdd = new Condition()
50+
{
51+
@Override
52+
public boolean evaluate(Rewrite event, EvaluationContext context)
53+
{
54+
Assert.assertEquals(1, Conditions.getNegationCount(context));
55+
Assert.assertTrue(Conditions.isNegated(context));
56+
return true;
57+
}
58+
};
59+
Condition condition = Not.any(assertCountOdd);
60+
Assert.assertFalse(condition.evaluate(new MockRewrite(), new MockEvaluationContext()));
61+
}
62+
63+
@Test
64+
public void testNotCountEvent()
65+
{
66+
Condition assertCountOdd = new Condition()
67+
{
68+
@Override
69+
public boolean evaluate(Rewrite event, EvaluationContext context)
70+
{
71+
Assert.assertEquals(3, Conditions.getNegationCount(context));
72+
Assert.assertTrue(Conditions.isNegated(context));
73+
return true;
74+
}
75+
};
76+
Condition assertCountEven = new Condition()
77+
{
78+
@Override
79+
public boolean evaluate(Rewrite event, EvaluationContext context)
80+
{
81+
Assert.assertEquals(4, Conditions.getNegationCount(context));
82+
Assert.assertFalse(Conditions.isNegated(context));
83+
return true;
84+
}
85+
};
86+
Condition condition = Not.any(Not.any(Not.any(And.all(assertCountOdd, Not.any(assertCountEven)))));
87+
Assert.assertTrue(condition.evaluate(new MockRewrite(), new MockEvaluationContext()));
88+
}
4389
}

‎api/src/main/java/org/ocpsoft/rewrite/config/Conditions.java‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,52 @@
4242
*
4343
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
4444
*/
45-
public class Conditions
45+
public final class Conditions
4646
{
47+
public static final String NEGATION_COUNT_KEY = Conditions.class.getCanonicalName() + "_NotCount";
48+
49+
/**
50+
* Indicates whether the current {@link Condition} is being negated.
51+
*/
52+
public static boolean isNegated(final EvaluationContext context)
53+
{
54+
return getNegationCount(context) % 2 == 1;
55+
}
56+
57+
/**
58+
* Returns the number of "Not"s that have been evaluated in the current evaluation process.
59+
*
60+
* For example, when(Not.any(MyCondition)) would have a "NotCount" of "1" during the evaluation of "MyCondition".
61+
*
62+
* when(Not.any(Not.any(MyCondition))) would have a "NotCount" of "2" during the evaluation of "MyCondition".
63+
*
64+
* This is useful for conditions that may have side effects, as they will know whether or not their condition is being
65+
* negated.
66+
*/
67+
public static int getNegationCount(final EvaluationContext context)
68+
{
69+
if (context == null)
70+
return 0;
71+
72+
Integer count = (Integer)context.get(NEGATION_COUNT_KEY);
73+
return count == null ? 0 : count;
74+
}
75+
76+
/**
77+
* This increments the number of negations, allowing us to determine (during evaluation) whether the results of a particular
78+
* {@link Condition} will be negated.
79+
*/
80+
static void incrementNegationCount(final EvaluationContext context, int adjustment)
81+
{
82+
if (context == null)
83+
return;
84+
85+
Integer count = Conditions.getNegationCount(context);
86+
count += adjustment;
87+
context.put(NEGATION_COUNT_KEY, count);
88+
}
89+
90+
4791
/**
4892
* Return a new {@link DefaultConditionBuilder} that evaluates to {@link True} when
4993
* {@link #evaluate(Rewrite, EvaluationContext)} is invoked.

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ public static Not any(final Condition condition)
5353
@Override
5454
public boolean evaluate(final Rewrite event, final EvaluationContext context)
5555
{
56-
return condition.evaluate(event, context) != true;
56+
Conditions.incrementNegationCount(context, 1);
57+
try
58+
{
59+
return condition.evaluate(event, context) != true;
60+
}
61+
finally
62+
{
63+
Conditions.incrementNegationCount(context, -1);
64+
}
5765
}
5866

5967
@Override
@@ -68,4 +76,5 @@ public String toString()
6876
return "Not.any(" + condition + ")";
6977
}
7078

79+
7180
}

0 commit comments

Comments
 (0)