📄 calculate.java
字号:
break; case TYPE_STRING: // run the decimal-formatting if (df != null && resultValue.compareTo(ZERO) > 0) { resultObj = df.format(resultValue); } else { resultObj = resultValue.toString(); } break; case TYPE_BIG_DECIMAL: resultObj = resultValue; break; } if (!mapAcsr.isEmpty()) { Map toMap = (Map) mapAcsr.get(methodContext); if (toMap == null) { if (Debug.verboseOn()) Debug.logVerbose("Map not found with name " + mapAcsr + ", creating new map", module); toMap = new HashMap(); mapAcsr.put(methodContext, toMap); } fieldAcsr.put(toMap, resultObj, methodContext); } else { fieldAcsr.put(methodContext, resultObj); } return true; } public String rawString() { // TODO: add all attributes and other info return "<calculate field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>"; } public String expandedString(MethodContext methodContext) { // TODO: something more than a stub/dummy return this.rawString(); } protected static interface SubCalc { public BigDecimal calcValue(MethodContext methodContext, int scale, int roundingMode); } protected static class NumberOp implements SubCalc { String valueStr; public NumberOp(Element element) { valueStr = element.getAttribute("value"); } public BigDecimal calcValue(MethodContext methodContext, int scale, int roundingMode) { String valueStr = methodContext.expandString(this.valueStr); BigDecimal value; try { value = new BigDecimal(valueStr); value = value.setScale(scale, roundingMode); } catch (Exception e) { Debug.logError(e, "Could not parse the number string: " + valueStr, module); throw new IllegalArgumentException("Could not parse the number string: " + valueStr); } // Debug.logInfo("calcValue number: " + value, module); return value; } } protected static class CalcOp implements SubCalc { public static final int OPERATOR_ADD = 1; public static final int OPERATOR_SUBTRACT = 2; public static final int OPERATOR_MULTIPLY = 3; public static final int OPERATOR_DIVIDE = 4; public static final int OPERATOR_NEGATIVE = 5; ContextAccessor mapAcsr; ContextAccessor fieldAcsr; String operatorStr; Calculate.SubCalc calcops[]; public CalcOp(Element element) { mapAcsr = new ContextAccessor(element.getAttribute("map-name")); fieldAcsr = new ContextAccessor(element.getAttribute("field-name")); operatorStr = element.getAttribute("operator"); List calcopElements = UtilXml.childElementList(element); calcops = new Calculate.SubCalc[calcopElements.size()]; Iterator calcopIter = calcopElements.iterator(); int i = 0; while (calcopIter.hasNext()) { Element calcopElement = (Element) calcopIter.next(); String nodeName = calcopElement.getNodeName(); if ("calcop".equals(calcopElement.getNodeName())) { calcops[i] = new Calculate.CalcOp(calcopElement); } else if ("number".equals(calcopElement.getNodeName())) { calcops[i] = new Calculate.NumberOp(calcopElement); } else { Debug.logError("Error: calculate operation unknown with type " + nodeName, module); } // Debug.logInfo("Added operation type " + nodeName + " in position " + i, module); i++; } } public BigDecimal calcValue(MethodContext methodContext, int scale, int roundingMode) { String operatorStr = methodContext.expandString(this.operatorStr); int operator = CalcOp.OPERATOR_ADD; if ("get".equals(operatorStr)) { operator = CalcOp.OPERATOR_ADD; } else if ("add".equals(operatorStr)) { operator = CalcOp.OPERATOR_ADD; } else if ("subtract".equals(operatorStr)) { operator = CalcOp.OPERATOR_SUBTRACT; } else if ("multiply".equals(operatorStr)) { operator = CalcOp.OPERATOR_MULTIPLY; } else if ("divide".equals(operatorStr)) { operator = CalcOp.OPERATOR_DIVIDE; } else if ("negative".equals(operatorStr)) { operator = CalcOp.OPERATOR_NEGATIVE; } BigDecimal resultValue = ZERO; resultValue = resultValue.setScale(scale, roundingMode); boolean isFirst = true; // if a fieldAcsr was specified, get the field from the map or result and use it as the initial value if (!fieldAcsr.isEmpty()) { Object fieldObj = null; if (!mapAcsr.isEmpty()) { Map fromMap = (Map) mapAcsr.get(methodContext); if (fromMap == null) { if (Debug.verboseOn()) Debug.logVerbose("Map not found with name " + mapAcsr + ", creating new map", module); fromMap = new HashMap(); mapAcsr.put(methodContext, fromMap); } fieldObj = fieldAcsr.get(fromMap, methodContext); } else { fieldObj = fieldAcsr.get(methodContext); } if (fieldObj != null) { if (fieldObj instanceof Double) { resultValue = new BigDecimal(((Double) fieldObj).doubleValue()); } else if (fieldObj instanceof Long) { resultValue = BigDecimal.valueOf(((Long) fieldObj).longValue()); } else if (fieldObj instanceof Float) { resultValue = new BigDecimal(((Float) fieldObj).floatValue()); } else if (fieldObj instanceof Integer) { resultValue = BigDecimal.valueOf(((Integer) fieldObj).longValue()); } else if (fieldObj instanceof String) { resultValue = new BigDecimal((String) fieldObj); } else if (fieldObj instanceof BigDecimal) { resultValue = (BigDecimal) fieldObj; } if (operator == OPERATOR_NEGATIVE) resultValue = resultValue.negate(); isFirst = false; } else { if (Debug.infoOn()) Debug.logInfo("Field not found with field-name " + fieldAcsr + ", and map-name " + mapAcsr + "using a default of 0", module); } } for (int i = 0; i < calcops.length; i++) { if (isFirst) { resultValue = calcops[i].calcValue(methodContext, scale, roundingMode); if (operator == OPERATOR_NEGATIVE) resultValue = resultValue.negate(); isFirst = false; } else { switch (operator) { case OPERATOR_ADD: resultValue = resultValue.add(calcops[i].calcValue(methodContext, scale, roundingMode)); break; case OPERATOR_SUBTRACT: case OPERATOR_NEGATIVE: resultValue = resultValue.subtract(calcops[i].calcValue(methodContext, scale, roundingMode)); break; case OPERATOR_MULTIPLY: resultValue = resultValue.multiply(calcops[i].calcValue(methodContext, scale, roundingMode)); break; case OPERATOR_DIVIDE: resultValue = resultValue.divide(calcops[i].calcValue(methodContext, scale, roundingMode), scale, roundingMode); break; } } // Debug.logInfo("sub total so far: " + resultValue, module); } // Debug.logInfo("calcValue calcop: " + resultValue + "(field=" + fieldAcsr + ", map=" + mapAcsr + ")", module); return resultValue; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -