private void processComparativeOne( Comparable<?> condition, Set<Object> retValue, Integer cumulativeTimes, Comparable<?> atomIncrValue, boolean needMergeValueInCloseInterval) { Comparative comp = (Comparative) condition; int comparison = comp.getComparison(); switch (comparison) { case 0: // 为0 的时候表示纯粹的包装对象。 process( comp.getValue(), retValue, cumulativeTimes, atomIncrValue, needMergeValueInCloseInterval); break; case Comparative.Equivalent: // 等于关系,直接放在collection retValue.add(EnumeratorUtils.toPrimaryValue(comp.getValue())); break; case Comparative.GreaterThan: case Comparative.GreaterThanOrEqual: case Comparative.LessThan: case Comparative.LessThanOrEqual: // 各种需要全取的情况 throw new EnumerationInterruptException(comp); default: throw new NotSupportException(); } }
private void processComparativeAnd( Comparable<?> condition, Set<Object> retValue, Integer cumulativeTimes, Comparable<?> atomIncrValue, boolean needMergeValueInCloseInterval) { List<Comparative> andList = ((ComparativeAND) condition).getList(); // 多余两个感觉没什么实际的意义,碰到了再处理 if (andList.size() == 2) { Comparable<?> arg1 = andList.get(0); Comparable<?> arg2 = andList.get(1); Comparative compArg1 = valid2varableInAndIsNotComparativeBaseList(arg1); Comparative compArg2 = valid2varableInAndIsNotComparativeBaseList(arg2); int compResult = 0; try { compArg1.setValue(EnumeratorUtils.toPrimaryValue(compArg1.getValue())); compArg2.setValue(EnumeratorUtils.toPrimaryValue(compArg2.getValue())); compResult = compArg1.getValue().compareTo(compArg2.getValue()); } catch (NullPointerException e) { throw new IllegalArgumentException("ComparativeAnd args is null", e); } if (compResult == 0) { // 值相等,如果都含有=关系,那么还有个公共点,否则一个公共点都没有 if (containsEquvilentRelation(compArg1) && containsEquvilentRelation(compArg2)) { retValue.add(compArg1.getValue()); } } else if (compResult < 0) { // arg1 < arg2 processTwoDifferentArgsInComparativeAnd( retValue, compArg1, compArg2, cumulativeTimes, atomIncrValue, needMergeValueInCloseInterval); } else { // compResult>0 // arg1 > arg2 processTwoDifferentArgsInComparativeAnd( retValue, compArg2, compArg1, cumulativeTimes, atomIncrValue, needMergeValueInCloseInterval); } } else { throw new TddlRuleException("ComparativeAND only support two args"); } }
/** * 根据传入的参数决定使用哪类枚举器 * * @param comp * @param needMergeValueInCloseInterval * @return */ private CloseIntervalFieldsEnumeratorHandler getCloseIntervalEnumeratorHandlerByComparative( Comparative comp, boolean needMergeValueInCloseInterval) { if (!needMergeValueInCloseInterval) { return enumeratorMap.get(DEFAULT_ENUMERATOR); } if (comp == null) { throw new IllegalArgumentException("comp is null"); } Comparable value = comp.getValue(); if (value instanceof ComparativeBaseList) { ComparativeBaseList comparativeBaseList = (ComparativeBaseList) value; for (Comparative comparative : comparativeBaseList.getList()) { return getCloseIntervalEnumeratorHandlerByComparative( comparative, needMergeValueInCloseInterval); } throw new NotSupportException(); // 不可能到这一步 } else if (value instanceof Comparative) { return getCloseIntervalEnumeratorHandlerByComparative(comp, needMergeValueInCloseInterval); } else { // 表明是一个comparative对象 CloseIntervalFieldsEnumeratorHandler enumeratorHandler = enumeratorMap.get(value.getClass().getName()); if (enumeratorHandler != null) { return enumeratorHandler; } else { return enumeratorMap.get(DEFAULT_ENUMERATOR); } } }
/** * 处理一个and条件中 x > 1 and x = 3 类似这样的情况,因为前面已经对from 和 to 相等的情况作了处理 因此这里只需要处理不等的情况中的上述问题。 同时也处理了x = 1 * and x = 2这种情况。以及x = 1 and x>2 和x < 1 and x =2这种情况 * * @param from * @param to * @return */ protected static Comparable compareAndGetIntersactionOneValue(Comparative from, Comparative to) { // x = from and x <= to if (from.getComparison() == Comparative.Equivalent) { if (to.getComparison() == Comparative.LessThan || to.getComparison() == Comparative.LessThanOrEqual) { return from.getValue(); } } // x <= from and x = to if (to.getComparison() == Comparative.Equivalent) { if (from.getComparison() == Comparative.GreaterThan || from.getComparison() == Comparative.GreaterThanOrEqual) { return to.getValue(); } } return null; }
private Comparative valid2varableInAndIsNotComparativeBaseList(Comparable<?> arg) { if (arg instanceof ComparativeBaseList) { throw new TddlRuleException("ComparativeAND only support two args"); } if (arg instanceof Comparative) { Comparative comp = ((Comparative) arg); int comparison = comp.getComparison(); if (comparison == 0) { // 0的时候意味着这个非ComparativeBaseList的Comparative是个纯粹的包装对象。 return valid2varableInAndIsNotComparativeBaseList(comp.getValue()); } else { // 其他就是有意义的值对象了 return comp; } } else { // 否则就是基本对象,应该用等于包装 // return new Comparative(Comparative.Equivalent,arg); throw new IllegalArgumentException("input value is not a comparative: " + arg); } }