From 473e2f367f1e4640655d03ba6694c594e2e542f7 Mon Sep 17 00:00:00 2001 From: Jesus Castagnetto Date: Fri, 1 Feb 2013 17:59:16 -0800 Subject: [PATCH] Implementing Xor operation --- .../refine/expr/functions/booleans/Xor.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 main/src/com/google/refine/expr/functions/booleans/Xor.java diff --git a/main/src/com/google/refine/expr/functions/booleans/Xor.java b/main/src/com/google/refine/expr/functions/booleans/Xor.java new file mode 100644 index 000000000..7ce081861 --- /dev/null +++ b/main/src/com/google/refine/expr/functions/booleans/Xor.java @@ -0,0 +1,42 @@ +/* + Implementing a naive XOR operation + Copyright 2013, Jesus M. Castagnetto + License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause) +*/ + +package com.google.refine.expr.functions.booleans; + +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONWriter; + +import com.google.refine.expr.EvalError; +import com.google.refine.grel.ControlFunctionRegistry; +import com.google.refine.grel.Function; + +public class Xor implements Function { + + @Override + public Object call(Properties bindings, Object[] args) { + if (args.length == 2 && + args[0] != null && args[0] instanceof Boolean && + args[1] != null && args[0] instanceof Boolean) { + boolean o1 = ((Boolean) args[0]).booleanValue(); + boolean o2 = ((Boolean) args[1]).booleanValue(); + return o1 != o2; + } + return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 booleans"); + } + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("XORs two boolean values"); + writer.key("params"); writer.value("boolean a, boolean b"); + writer.key("returns"); writer.value("boolean"); + writer.endObject(); + } +}