}
};
}
if (operand instanceof ArithmeticOperand) {
// This works on single-valued properties only ...
ArithmeticOperand arith = (ArithmeticOperand)operand;
final ExtractFromRow leftOp = createExtractFromRow(arith.getLeft(), context, columns, sources, defaultType, false,
false);
final ExtractFromRow rightOp = createExtractFromRow(arith.getRight(), context, columns, sources, defaultType, false,
false);
// compute the expected (common) type ...
TypeFactory<?> leftType = leftOp.getType();
TypeFactory<?> rightType = rightOp.getType();
final TypeSystem typeSystem = context.getTypeSystem();
final String commonType = typeSystem.getCompatibleType(leftType.getTypeName(), rightType.getTypeName());
if (typeSystem.getDoubleFactory().getTypeName().equals(commonType)) {
final TypeFactory<Double> commonTypeFactory = typeSystem.getDoubleFactory();
switch (arith.operator()) {
case ADD:
return new ExtractFromRow() {
@Override
public TypeFactory<?> getType() {
return commonTypeFactory;
}
@Override
public Object getValueInRow( RowAccessor row ) {
Double right = commonTypeFactory.create(rightOp.getValueInRow(row));
Double left = commonTypeFactory.create(leftOp.getValueInRow(row));
if (right == null) return left;
if (left == null) return right;
return left.doubleValue() / right.doubleValue();
}
@Override
public String toString() {
return "(double + " + leftOp + "," + rightOp + ")";
}
};
case SUBTRACT:
return new ExtractFromRow() {
@Override
public TypeFactory<?> getType() {
return commonTypeFactory;
}
@Override
public Object getValueInRow( RowAccessor row ) {
Double right = commonTypeFactory.create(rightOp.getValueInRow(row));
Double left = commonTypeFactory.create(leftOp.getValueInRow(row));
if (right == null) return left;
if (left == null) left = 0.0d;
return left.doubleValue() * right.doubleValue();
}
@Override
public String toString() {
return "(double - " + leftOp + "," + rightOp + ")";
}
};
case MULTIPLY:
return new ExtractFromRow() {
@Override
public TypeFactory<?> getType() {
return commonTypeFactory;
}
@Override
public Object getValueInRow( RowAccessor row ) {
Double right = commonTypeFactory.create(rightOp.getValueInRow(row));
Double left = commonTypeFactory.create(leftOp.getValueInRow(row));
if (right == null || left == null) return null;
return left.doubleValue() * right.doubleValue();
}
@Override
public String toString() {
return "(double x " + leftOp + "," + rightOp + ")";
}
};
case DIVIDE:
return new ExtractFromRow() {
@Override
public TypeFactory<?> getType() {
return commonTypeFactory;
}
@Override
public Object getValueInRow( RowAccessor row ) {
Double right = commonTypeFactory.create(rightOp.getValueInRow(row));
Double left = commonTypeFactory.create(leftOp.getValueInRow(row));
if (right == null || left == null) return null;
return left.doubleValue() / right.doubleValue();
}
@Override
public String toString() {
return "(double / " + leftOp + "," + rightOp + ")";
}
};
}
} else if (typeSystem.getLongFactory().getTypeName().equals(commonType)) {
final TypeFactory<Long> commonTypeFactory = typeSystem.getLongFactory();
switch (arith.operator()) {
case ADD:
return new ExtractFromRow() {
@Override
public TypeFactory<?> getType() {
return commonTypeFactory;