From f119bd971b3ad726b39ce2b51d967a5f065d1db0 Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Sat, 22 Jan 2005 22:01:15 +0000 Subject: [PATCH] Xor. Not strictly necessary, but the logical equivalent is a dog to type. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277431 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/conditions.html | 9 ++ src/etc/testcases/taskdefs/conditions/xor.xml | 95 +++++++++++++++++++ .../tools/ant/taskdefs/condition/Xor.java | 46 +++++++++ .../tools/ant/types/defaults.properties | 2 +- .../tools/ant/taskdefs/condition/XorTest.java | 67 +++++++++++++ 5 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 src/etc/testcases/taskdefs/conditions/xor.xml create mode 100644 src/main/org/apache/tools/ant/taskdefs/condition/Xor.java create mode 100644 src/testcases/org/apache/tools/ant/taskdefs/condition/XorTest.java diff --git a/docs/manual/CoreTasks/conditions.html b/docs/manual/CoreTasks/conditions.html index f0ec8172d..bb1fd1c96 100644 --- a/docs/manual/CoreTasks/conditions.html +++ b/docs/manual/CoreTasks/conditions.html @@ -49,6 +49,15 @@

or

shortcut semantics as the Java || operator, as soon as one of the nested conditions is true, no other condition will be evaluated.

+

xor

+

The <xor> element performs an exclusive +or on all nested elements, similar to the ^ operator +in Java. It only evaluates to true if an odd number of nested conditions +are true. There is no shortcutting of evaluation, unlike the <and> +and <or> tests. +It doesn't have any attributes and accepts all nested +elements of the condition task as nested elements as well.

+

available

This condition is identical to the Available task, all attributes and nested diff --git a/src/etc/testcases/taskdefs/conditions/xor.xml b/src/etc/testcases/taskdefs/conditions/xor.xml new file mode 100644 index 000000000..b7bf94334 --- /dev/null +++ b/src/etc/testcases/taskdefs/conditions/xor.xml @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/Xor.java b/src/main/org/apache/tools/ant/taskdefs/condition/Xor.java new file mode 100644 index 000000000..b6ab7f5c0 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/condition/Xor.java @@ -0,0 +1,46 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * 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.apache.tools.ant.taskdefs.condition; + +import org.apache.tools.ant.BuildException; + +import java.util.Enumeration; + +/** + * Xor task to exclusive or operations. + * This does not shortcut stuff + */ +public class Xor extends ConditionBase implements Condition { + + /** + * @return true if all the contained conditions evaluates to true + * @throws org.apache.tools.ant.BuildException + * if an error occurs + */ + public boolean eval() throws BuildException { + Enumeration e = getConditions(); + //initial state is false. + boolean state=false; + while (e.hasMoreElements()) { + Condition c = (Condition) e.nextElement(); + //every condition is xored against the previous one + state ^= c.eval(); + } + return state; + } + +} diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties index 0f444fecf..41c18033f 100644 --- a/src/main/org/apache/tools/ant/types/defaults.properties +++ b/src/main/org/apache/tools/ant/types/defaults.properties @@ -41,4 +41,4 @@ ispingable=org.apache.tools.ant.taskdefs.optional.condition.IsPingable mavenrepository=org.apache.tools.ant.taskdefs.repository.MavenRepository scriptselector=org.apache.tools.ant.types.optional.ScriptSelector scriptcondition=org.apache.tools.ant.types.optional.ScriptCondition - +xor=org.apache.tools.ant.taskdefs.condition.Xor diff --git a/src/testcases/org/apache/tools/ant/taskdefs/condition/XorTest.java b/src/testcases/org/apache/tools/ant/taskdefs/condition/XorTest.java new file mode 100644 index 000000000..49c91aaea --- /dev/null +++ b/src/testcases/org/apache/tools/ant/taskdefs/condition/XorTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * 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.apache.tools.ant.taskdefs.condition; + +import org.apache.tools.ant.BuildFileTest; + +/** + * Test that Xor follows the conventional boolean logic semantics + * (a ^ b) === (a||b)&!(a&&b) + */ +public class XorTest extends BuildFileTest { + + public XorTest(String name) { + super(name); + } + + + /** + * The JUnit setup method + */ + public void setUp() { + configureProject("src/etc/testcases/taskdefs/conditions/xor.xml"); + } + + public void testEmpty() { + executeTarget("testEmpty"); + } + + public void test0() { + executeTarget("test0"); + } + + public void test1() { + executeTarget("test1"); + } + + public void test00() { + executeTarget("test00"); + } + + public void test10() { + executeTarget("test10"); + } + + public void test01() { + executeTarget("test01"); + } + + public void test11() { + executeTarget("test11"); + } + +}