Contents > 9 Extending the Metrics and Rule Engine > 9.4 Boolean Functions > 9.4.2 Implementation of the Boolean Function

9.4.2 Implementation of the Boolean Function

The following listing shows the complete implementation of the XOR function outlined in the previous section. The function successively evaluates the condition expressions passed as arguments to the XOR function. The result of the function is true if exactly one of the individual condition expressions is true:
  package com.acme;

  import com.sdmetrics.math.ExpressionNode;
  import com.sdmetrics.metrics.BooleanOperation;
  import com.sdmetrics.metrics.SDMetricsException;
  import com.sdmetrics.metrics.Variables;
  import com.sdmetrics.model.ModelElement;

01 public class BooleanOperationXOR extends BooleanOperation {

   @Override
02 public boolean calculateValue(ModelElement element, 
03                               ExpressionNode node,
04                               Variables vars) 
05                               throws SDMetricsException {

06   int trueConditions = 0;
07   int index = 0;
08   while (index < node.getOperandCount() && trueConditions <= 1) {
09     if (evalBooleanExpression(element, node.getOperand(index), vars)) {
10       trueConditions++;
       }
11     index++;
     }
12   return trueConditions == 1;
   }
}
Let's go through the salient points of this implementation line by line: