FilterUtils { public static IFilter merge(IFilter filter) throws EmptyResultFilterException { if (filter == null || filter instanceof IBooleanFilter) { return filter; } filter = toDNFAndFlat(filter); List<List<IFilter>> DNFNodes = toDNFNodesArray(filter); List<List<IFilter>> newDNFNodes = new LinkedList(); boolean existGroupFilter = false; for (List<IFilter> DNFNode : DNFNodes) { List<IFilter> newDNFNode = new LinkedList(); for (IFilter f : DNFNode) { if (f instanceof IGroupFilter) { IFilter mf = mergeOrDNFNodes((IGroupFilter) f); newDNFNode.add(mf); if (mf instanceof IGroupFilter) { existGroupFilter = true; } } else { newDNFNode.add(f); } } newDNFNodes.add(newDNFNode); } if (!existGroupFilter) { if (!needToMerge(newDNFNodes)) { return filter; } newDNFNodes = mergeOrDNFNodes(mergeAndDNFNodesArray(newDNFNodes)); } if (newDNFNodes == null || newDNFNodes.isEmpty() || newDNFNodes.get(0) == null || newDNFNodes.get(0).isEmpty() || DNFNodes.get(0).get(0) == null) { IBooleanFilter f = ASTNodeFactory.getInstance().createBooleanFilter(); f.setOperation(OPERATION.CONSTANT); f.setColumn("1"); f.setColumnName(ObjectUtils.toString("1")); return f; } else { return DNFToOrLogicTree(newDNFNodes); } }  static IFilter toDNFAndFlat(IFilter node); static IFilter toDNF(IFilter node); static List<List<IFilter>> toDNFNodesArray(IFilter node); static List<IFilter> toDNFNode(IFilter node); static Map<Object, List<IFilter>> toColumnFiltersMap(List<IFilter> DNFNode); static boolean isDNF(IFilter node); static boolean isCNFNode(IFilter node); static IFilter merge(IFilter filter); static IFilter DNFToOrLogicTree(List<List<IFilter>> DNFNodes); static IFilter DNFToAndLogicTree(List<IFilter> DNFNode); static IFilter and(IFilter root, IFilter o); static IFilter or(IFilter root, IFilter o); static IBooleanFilter equal(Object columnName, Object value); static boolean isConstFilter(IBooleanFilter f); static IFilter createFilter(String where); static boolean isConstValue(Object v); static Object getFilterColumn(IFilter filter);  }
@Test public void testMerge() { try { IFilter filter = and(filter("A", 1, OPERATION.GT), filter("A", 10, OPERATION.LT)); Assert.assertEquals("(A > 1 AND A < 10)", FilterUtils.merge(filter).toString()); filter = and(filter("A", 1, OPERATION.GT), filter("A", 10, OPERATION.LT_EQ)); filter = newAnd(and(filter("A", 2, OPERATION.GT_EQ), filter("A", 11, OPERATION.LT)), filter); Assert.assertEquals("(A >= 2 AND A <= 10)", FilterUtils.merge(filter).toString()); } catch (EmptyResultFilterException e) { Assert.fail(); } try { IFilter filter = and(filter("A", 1, OPERATION.GT), filter("A", 0, OPERATION.LT)); FilterUtils.merge(filter); Assert.fail(); } catch (EmptyResultFilterException e) { } try { IFilter filter = or(filter("A", 1, OPERATION.GT), filter("A", 3, OPERATION.LT)); Assert.assertEquals("1", FilterUtils.merge(filter).toString()); filter = or(filter("A", 1, OPERATION.GT), filter("A", 3, OPERATION.GT)); Assert.assertEquals("A > 1", FilterUtils.merge(filter).toString()); filter = or(filter("A", 1, OPERATION.GT), filter("A", 5, OPERATION.EQ)); Assert.assertEquals("A > 1", FilterUtils.merge(filter).toString()); } catch (EmptyResultFilterException e) { Assert.fail(); } try { IFilter filter = or(or(filter("A", 3, OPERATION.GT_EQ), filter("A", 1, OPERATION.LT_EQ)), filter("A", Arrays.asList(1, 2, 3, 4), OPERATION.IN)); Assert.assertEquals("(A IN [1, 2, 3, 4] OR A >= 3 OR A <= 1)", FilterUtils.merge(filter).toString()); } catch (EmptyResultFilterException e) { Assert.fail(); } }