@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(); } }
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); if (!needToMerge(DNFNodes)) { return filter; } DNFNodes = mergeOrDNFNodes(mergeAndDNFNodesArray(DNFNodes)); if (DNFNodes == null || DNFNodes.isEmpty() || DNFNodes.get(0) == null || DNFNodes.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(DNFNodes); } }
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); if (!needToMerge(DNFNodes)) { return filter; } DNFNodes = mergeOrDNFNodes(mergeAndDNFNodesArray(DNFNodes)); if (DNFNodes == null || DNFNodes.isEmpty() || DNFNodes.get(0) == null || DNFNodes.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(DNFNodes); } } }
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); if (!needToMerge(DNFNodes)) { return filter; } DNFNodes = mergeOrDNFNodes(mergeAndDNFNodesArray(DNFNodes)); if (DNFNodes == null || DNFNodes.isEmpty() || DNFNodes.get(0) == null || DNFNodes.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(DNFNodes); } }  }
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); if (!needToMerge(DNFNodes)) { return filter; } DNFNodes = mergeOrDNFNodes(mergeAndDNFNodesArray(DNFNodes)); if (DNFNodes == null || DNFNodes.isEmpty() || DNFNodes.get(0) == null || DNFNodes.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(DNFNodes); } }  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(Comparable columnName, Comparable value); static boolean isConstFilter(IBooleanFilter f); static IFilter createFilter(String where); static boolean isConstValue(Object v); }
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); if (!needToMerge(DNFNodes)) { return filter; } DNFNodes = mergeOrDNFNodes(mergeAndDNFNodesArray(DNFNodes)); if (DNFNodes == null || DNFNodes.isEmpty() || DNFNodes.get(0) == null || DNFNodes.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(DNFNodes); } }  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(Comparable columnName, Comparable value); static boolean isConstFilter(IBooleanFilter f); static IFilter createFilter(String where); static boolean isConstValue(Object v);  }