📄 simpletest.java
字号:
// Set exp1 to the first part of the expression and exp2 to the second exp1 = str.substring(0, i-1).trim(); // Do not trim exp2 now, because we might have to truncate it later exp2 = str.substring(++i); exp2Index = i; continue; } if (c == NOT_CHAR && i < length && str.charAt(i) == EQUAL_CHAR) { condition = NOT_EQUAL; // Set exp1 to the first part of the expression and exp2 to the second exp1 = str.substring(0, i-1).trim(); // Do not trim exp2 now, because we might have to truncate it later exp2 = str.substring(++i); exp2Index = i; continue; } if (c == LESS_CHAR && i < length && str.charAt(i) == EQUAL_CHAR) { condition = LESS_EQUAL; // Set exp1 to the first part of the expression and exp2 to the second exp1 = str.substring(0, i-1).trim(); // Do not trim exp2 now, because we might have to truncate it later exp2 = str.substring(++i); exp2Index = i; continue; } if (c == LESS_CHAR) { condition = LESS; // Set exp1 to the first part of the expression and exp2 to the second exp1 = str.substring(0, i-1).trim(); // Do not trim exp2 now, because we might have to truncate it later exp2 = str.substring(i); exp2Index = i; continue; } if (c == GREATER_CHAR && i < length && str.charAt(i) == EQUAL_CHAR) { condition = GREATER_EQUAL; // Set exp1 to the first part of the expression and exp2 to the second exp1 = str.substring(0, i-1).trim(); // Do not trim exp2 now, because we might have to truncate it later exp2 = str.substring(++i); exp2Index = i; continue; } if (c == GREATER_CHAR) { condition = GREATER; // Set exp1 to the first part of the expression and exp2 to the second exp1 = str.substring(0, i-1).trim(); // Do not trim exp2 now, because we might have to truncate it later exp2 = str.substring(i); exp2Index = i; continue; } } if (exp2 != null) exp2 = exp2.trim(); return -1; } /** * This method checks if the expression is a constant value. * If it is not a constant then the method returns the object NO_CONSTANT * If it is a constant then it returns the constant value which may be null */ protected Object getConstant(String exp) { Query q = Query.getQuery(exp); QuerySegment[] segments = q.getSegments(); QuerySegment segment = segments[0]; switch (segment.getType()) { case QuerySegment.STRING: return segment.getId(); // the integer is the first value and only value in the segment case QuerySegment.NUMBER: return segment.getValues().get(0); // the reserved keyword "true" case QuerySegment.TRUE: return Boolean.TRUE; // the reserved keyword "false" case QuerySegment.FALSE: return Boolean.FALSE; // the reserved keyword "null" case QuerySegment.NULL: return null; default: return NO_CONSTANT; } } public boolean test(ValueStack stack) { return test(stack, null, null); } /** * The values in prevVal1 and prevVal2 will always be sent in. They can be null * because they are not used or because the value found was null. This does not matter. * The expression after this one will only care about the value IF the same expression * existed in this test as in the next test. */ protected boolean test(ValueStack stack, Object prevVal1, Object prevVal2) { Object val1; Object val2 = null; boolean result = false; // Check if the first expression is not a constant if (value1 == NO_CONSTANT) { // Check if it is the same value as one already evaluated. Otherwise look // it up on the value stack if (sameVal1 == 0) val1 = stack.findValue(q1); else if (sameVal1 == 1) val1 = prevVal1; else val1 = prevVal2; } // The value is a constant so just assign it else { val1 = value1; } if (condition == CHECK_NULL) { // No equals expression. Just check if the value is null or not result = (val1 == null ? false : true); if (neg1) result = !result; } // The expression contains a condition like == or != else { if (value2 == NO_CONSTANT) { if (sameVal2 == 0) val2 = stack.findValue(q2); else if (sameVal2 == 1) val2 = prevVal1; else val2 = prevVal2; } else val2 = value2; int comparison = -1; // less than // We resolve nulls by hand if ( val1 == null || val2 == null ) { if ( val1 == null && val2 == null ) // equal to { comparison = 0; // equal to } else if (val2 == null) // greater than comparison = 1; } else if ( val1 instanceof Number && val2 instanceof Number ) { double number1 = ((Number)val1).doubleValue(); double number2 = ((Number)val2).doubleValue(); if (number1 > number2) comparison = 1; // greater than else if ( number1 == number2 ) { long longBits1 = Double.doubleToLongBits(number1); long longBits2 = Double.doubleToLongBits(number2); if ( longBits1 > longBits2 ) comparison = 1; else if ( longBits1 == longBits2 ) comparison = 0; } } // If operands are of the same type, do a direct comparison else if ( val1.getClass().equals(val2.getClass()) && (val1 instanceof Comparable)) { comparison = ((Comparable)val1).compareTo((Comparable)val2); } else { // If the operands aren't the same type or aren't comparible, then // convert them to strings and do a comparison // Mo: I think that this could lead to some difficult to predict or unpredictable behavior comparison = ((Comparable)val1.toString()).compareTo((Comparable)val2.toString()); } // Now call resolve to determine the resulting value result = resolve(comparison, condition); } switch(operator) { case NONE: return result; case AND: if (result) return nextTest.test(stack, val1, val2); else return false; case OR: if (result) return true; else return nextTest.test(stack, val1, val2); default: return false; } } /** * determine true or false by comparing the comparison result * with the operator. * * @param comp the comparison result * @param condition the condition * @return the boolean result * */ protected boolean resolve(int comp, int condition) { //log.debug( "comp: " + comp + ", operatr: " + operatr); if ( comp == 0 ) { switch (condition) { case EQUAL: case GREATER_EQUAL: case LESS_EQUAL: return true; default: return false; } } if ( comp > 0 ) { switch (condition) { case GREATER: case GREATER_EQUAL: case NOT_EQUAL: return true; default: return false; } } switch (condition) { case LESS: case LESS_EQUAL: case NOT_EQUAL: return true; } return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -