@Test public void testDouble_divide() { FunctionArgumentAttributeValue attr0 = null; FunctionArgumentAttributeValue attr1 = null; FunctionArgumentAttributeValue attr2 = null; try { attr0 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(0)); attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(1.5)); attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(2.5)); } catch (Exception e) { fail("creating attributes e="+e); } FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_DOUBLE_DIVIDE; assertEquals(XACML3.ID_FUNCTION_DOUBLE_DIVIDE, fd.getId()); assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeArgs().getId()); assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeId()); assertFalse(fd.returnsBag()); assertEquals(new Integer(2), fd.getNumArgs()); arguments.add(attr1); arguments.add(attr2); ExpressionResult res = fd.evaluate(null, arguments); assertTrue(res.isOk()); Double resValue = (Double)res.getValue().getValue(); assertEquals(new Double(0.6), resValue); arguments.clear(); arguments.add(attr1); arguments.add(attr0); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:double-divide Divide by 0 error: 1.5, 0.0", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); }
@Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { List<T> convertedArguments = new ArrayList<T>(); Status status = this.validateArguments(arguments, convertedArguments); if (!status.getStatusCode().equals(StdStatusCode.STATUS_CODE_OK)) { return ExpressionResult.newError(getFunctionStatus(status)); } ExpressionResult expressionResult = null; try { switch (operation) { case ADD: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).add( (BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) + (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case SUBTRACT: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).subtract( (BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) - (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case MULTIPLY: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).multiply((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) * (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case DIVIDE: if (this.getDataType() == DataTypes.DT_INTEGER) { if ( ((BigInteger)convertedArguments.get(1)).equals(new BigInteger("0")) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).divide((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { if ((Double)convertedArguments.get(1) == 0) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) / (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case MOD: if ( ((BigInteger)convertedArguments.get(1)).equals(new BigInteger("0")) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).remainder((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); break; case ABS: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).abs() ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, Math.abs((Double)convertedArguments.get(0))); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case ROUND: doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (double)(Math.round((Double)convertedArguments.get(0))) ); expressionResult = ExpressionResult.newSingle(doubleResult); break; case FLOOR: doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, Math.floor((Double)convertedArguments.get(0))); expressionResult = ExpressionResult.newSingle(doubleResult); break; } } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } String args = arguments.get(0).getValue().toString(); if (arguments.size() > 1) { args += ", " + arguments.get(1).getValue().toString(); } expressionResult = ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message + " args: " + args + " " + e.getMessage() )); } return expressionResult; }
FunctionDefinitionArithmetic extends FunctionDefinitionHomogeneousSimple<T,T> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { List<T> convertedArguments = new ArrayList<T>(); Status status = this.validateArguments(arguments, convertedArguments); if (!status.getStatusCode().equals(StdStatusCode.STATUS_CODE_OK)) { return ExpressionResult.newError(getFunctionStatus(status)); } ExpressionResult expressionResult = null; try { switch (operation) { case ADD: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).add( (BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) + (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case SUBTRACT: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).subtract( (BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) - (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case MULTIPLY: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).multiply((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) * (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case DIVIDE: if (this.getDataType() == DataTypes.DT_INTEGER) { if ( ((BigInteger)convertedArguments.get(1)).equals(new BigInteger("0")) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).divide((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { if ((Double)convertedArguments.get(1) == 0) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) / (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case MOD: if ( ((BigInteger)convertedArguments.get(1)).equals(new BigInteger("0")) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).remainder((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); break; case ABS: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).abs() ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, Math.abs((Double)convertedArguments.get(0))); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case ROUND: doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (double)(Math.round((Double)convertedArguments.get(0))) ); expressionResult = ExpressionResult.newSingle(doubleResult); break; case FLOOR: doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, Math.floor((Double)convertedArguments.get(0))); expressionResult = ExpressionResult.newSingle(doubleResult); break; } } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } String args = arguments.get(0).getValue().toString(); if (arguments.size() > 1) { args += ", " + arguments.get(1).getValue().toString(); } expressionResult = ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message + " args: " + args + " " + e.getMessage() )); } return expressionResult; } }
FunctionDefinitionArithmetic extends FunctionDefinitionHomogeneousSimple<T,T> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { List<T> convertedArguments = new ArrayList<T>(); Status status = this.validateArguments(arguments, convertedArguments); if (!status.getStatusCode().equals(StdStatusCode.STATUS_CODE_OK)) { return ExpressionResult.newError(getFunctionStatus(status)); } ExpressionResult expressionResult = null; try { switch (operation) { case ADD: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).add( (BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) + (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case SUBTRACT: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).subtract( (BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) - (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case MULTIPLY: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).multiply((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) * (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case DIVIDE: if (this.getDataType() == DataTypes.DT_INTEGER) { if ( ((BigInteger)convertedArguments.get(1)).equals(new BigInteger("0")) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).divide((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { if ((Double)convertedArguments.get(1) == 0) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) / (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case MOD: if ( ((BigInteger)convertedArguments.get(1)).equals(new BigInteger("0")) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).remainder((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); break; case ABS: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).abs() ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, Math.abs((Double)convertedArguments.get(0))); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case ROUND: doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (double)(Math.round((Double)convertedArguments.get(0))) ); expressionResult = ExpressionResult.newSingle(doubleResult); break; case FLOOR: doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, Math.floor((Double)convertedArguments.get(0))); expressionResult = ExpressionResult.newSingle(doubleResult); break; } } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } String args = arguments.get(0).getValue().toString(); if (arguments.size() > 1) { args += ", " + arguments.get(1).getValue().toString(); } expressionResult = ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message + " args: " + args + " " + e.getMessage() )); } return expressionResult; } FunctionDefinitionArithmetic(Identifier idIn, DataType<T> dataTypeArgsIn, OPERATION op, int nArgs); }
FunctionDefinitionArithmetic extends FunctionDefinitionHomogeneousSimple<T,T> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { List<T> convertedArguments = new ArrayList<T>(); Status status = this.validateArguments(arguments, convertedArguments); if (!status.getStatusCode().equals(StdStatusCode.STATUS_CODE_OK)) { return ExpressionResult.newError(getFunctionStatus(status)); } ExpressionResult expressionResult = null; try { switch (operation) { case ADD: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).add( (BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) + (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case SUBTRACT: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).subtract( (BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) - (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case MULTIPLY: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).multiply((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) * (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case DIVIDE: if (this.getDataType() == DataTypes.DT_INTEGER) { if ( ((BigInteger)convertedArguments.get(1)).equals(new BigInteger("0")) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).divide((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { if ((Double)convertedArguments.get(1) == 0) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) / (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case MOD: if ( ((BigInteger)convertedArguments.get(1)).equals(new BigInteger("0")) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).remainder((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); break; case ABS: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).abs() ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, Math.abs((Double)convertedArguments.get(0))); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case ROUND: doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (double)(Math.round((Double)convertedArguments.get(0))) ); expressionResult = ExpressionResult.newSingle(doubleResult); break; case FLOOR: doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, Math.floor((Double)convertedArguments.get(0))); expressionResult = ExpressionResult.newSingle(doubleResult); break; } } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } String args = arguments.get(0).getValue().toString(); if (arguments.size() > 1) { args += ", " + arguments.get(1).getValue().toString(); } expressionResult = ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message + " args: " + args + " " + e.getMessage() )); } return expressionResult; } FunctionDefinitionArithmetic(Identifier idIn, DataType<T> dataTypeArgsIn, OPERATION op, int nArgs); @Override ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments); }
FunctionDefinitionArithmetic extends FunctionDefinitionHomogeneousSimple<T,T> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { List<T> convertedArguments = new ArrayList<T>(); Status status = this.validateArguments(arguments, convertedArguments); if (!status.getStatusCode().equals(StdStatusCode.STATUS_CODE_OK)) { return ExpressionResult.newError(getFunctionStatus(status)); } ExpressionResult expressionResult = null; try { switch (operation) { case ADD: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).add( (BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) + (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case SUBTRACT: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).subtract( (BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) - (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case MULTIPLY: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).multiply((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) * (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case DIVIDE: if (this.getDataType() == DataTypes.DT_INTEGER) { if ( ((BigInteger)convertedArguments.get(1)).equals(new BigInteger("0")) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).divide((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); } else { if ((Double)convertedArguments.get(1) == 0) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (Double)convertedArguments.get(0) / (Double)convertedArguments.get(1)); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case MOD: if ( ((BigInteger)convertedArguments.get(1)).equals(new BigInteger("0")) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() +" Divide by 0 error: "+ arguments.get(0).getValue().getValue().toString() + ", " + arguments.get(1).getValue().getValue().toString())); } integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).remainder((BigInteger)convertedArguments.get(1)) ); expressionResult = ExpressionResult.newSingle(integerResult); break; case ABS: if (this.getDataType() == DataTypes.DT_INTEGER) { integerResult = new StdAttributeValue<BigInteger>(XACML.ID_DATATYPE_INTEGER, ((BigInteger)convertedArguments.get(0)).abs() ); expressionResult = ExpressionResult.newSingle(integerResult); } else { doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, Math.abs((Double)convertedArguments.get(0))); expressionResult = ExpressionResult.newSingle(doubleResult); } break; case ROUND: doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, (double)(Math.round((Double)convertedArguments.get(0))) ); expressionResult = ExpressionResult.newSingle(doubleResult); break; case FLOOR: doubleResult = new StdAttributeValue<Double>(XACML.ID_DATATYPE_DOUBLE, Math.floor((Double)convertedArguments.get(0))); expressionResult = ExpressionResult.newSingle(doubleResult); break; } } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } String args = arguments.get(0).getValue().toString(); if (arguments.size() > 1) { args += ", " + arguments.get(1).getValue().toString(); } expressionResult = ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message + " args: " + args + " " + e.getMessage() )); } return expressionResult; } FunctionDefinitionArithmetic(Identifier idIn, DataType<T> dataTypeArgsIn, OPERATION op, int nArgs); @Override ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments);  }