diff --git a/com.ibm.wala.util/src/com/ibm/wala/util/collections/FilterPredicate.java b/com.ibm.wala.util/src/com/ibm/wala/util/collections/FilterPredicate.java new file mode 100644 index 000000000..878b173bf --- /dev/null +++ b/com.ibm.wala.util/src/com/ibm/wala/util/collections/FilterPredicate.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2011 IBM Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package com.ibm.wala.util.collections; + +import com.ibm.wala.util.Predicate; + +/** + * A migration aid, to move from Filter to Predicate + */ +@Deprecated +public class FilterPredicate extends Predicate { + + public static FilterPredicate toPredicate(Filter f) { + return new FilterPredicate(f); + } + + private final Filter f; + + private FilterPredicate(Filter f) { + this.f = f; + } + + @Override + public boolean test(T t) { + return f.accepts(t); + } + +} diff --git a/com.ibm.wala.util/src/com/ibm/wala/util/graph/GraphSlicer.java b/com.ibm.wala.util/src/com/ibm/wala/util/graph/GraphSlicer.java index e49d3c5a1..e1c9a7be8 100644 --- a/com.ibm.wala.util/src/com/ibm/wala/util/graph/GraphSlicer.java +++ b/com.ibm.wala.util/src/com/ibm/wala/util/graph/GraphSlicer.java @@ -18,8 +18,10 @@ import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; +import com.ibm.wala.util.Predicate; import com.ibm.wala.util.collections.Filter; import com.ibm.wala.util.collections.FilterIterator; +import com.ibm.wala.util.collections.FilterPredicate; import com.ibm.wala.util.collections.HashSetFactory; import com.ibm.wala.util.collections.Iterator2Collection; import com.ibm.wala.util.collections.IteratorUtil; @@ -29,6 +31,7 @@ import com.ibm.wala.util.graph.impl.GraphInverter; import com.ibm.wala.util.graph.traverse.DFS; /** + * Utilities related to simple graph subset operations. */ public class GraphSlicer { @@ -41,15 +44,14 @@ public class GraphSlicer { * @return the set of nodes in g, from which any of the targets (nodes that f accepts) is reachable. * @throws WalaException */ - public static Set slice(Graph g, Filter f){ - + public static Set slice(Graph g, Predicate p){ if (g == null) { throw new IllegalArgumentException("g is null"); } HashSet roots = HashSetFactory.make(); for (Iterator it = g.iterator(); it.hasNext();) { T o = it.next(); - if (f.accepts(o)) { + if (p.test(o)) { roots.add(o); } } @@ -57,13 +59,22 @@ public class GraphSlicer { Set result = DFS.getReachableNodes(GraphInverter.invert(g), roots); return result; - + } + + @Deprecated + public static Set slice(Graph g, Filter f){ + return slice(g, FilterPredicate.toPredicate(f)); + } + + @Deprecated + public static Graph prune(final Graph g, final Filter f) { + return prune(g, FilterPredicate.toPredicate(f)); } /** - * Prune a graph to only the nodes accepted by the filter f + * Prune a graph to only the nodes accepted by the {@link Predicate} p */ - public static Graph prune(final Graph g, final Filter f) { + public static Graph prune(final Graph g, final Predicate p) { if (g == null) { throw new IllegalArgumentException("g is null"); } @@ -71,7 +82,7 @@ public class GraphSlicer { int nodeCount = -1; public Iterator iterator() { - return new FilterIterator(g.iterator(), f); + return Predicate.filter(g.iterator(), p).iterator(); } public int getNumberOfNodes() { @@ -89,23 +100,23 @@ public class GraphSlicer { Assertions.UNREACHABLE(); } - public boolean containsNode(T N) { - return f.accepts(N) && g.containsNode(N); + public boolean containsNode(T n) { + return p.test(n) && g.containsNode(n); } }; final EdgeManager e = new EdgeManager() { - public Iterator getPredNodes(T N) { - return new FilterIterator(g.getPredNodes(N), f); + public Iterator getPredNodes(T n) { + return Predicate.filter(g.getPredNodes(n), p).iterator(); } - public int getPredNodeCount(T N) { - return IteratorUtil.count(getPredNodes(N)); + public int getPredNodeCount(T n) { + return IteratorUtil.count(getPredNodes(n)); } - public Iterator getSuccNodes(T N) { - return new FilterIterator(g.getSuccNodes(N), f); + public Iterator getSuccNodes(T n) { + return Predicate.filter(g.getSuccNodes(n), p).iterator(); } public int getSuccNodeCount(T N) { @@ -133,7 +144,7 @@ public class GraphSlicer { } public boolean hasEdge(T src, T dst) { - return g.hasEdge(src, dst) && f.accepts(src) && f.accepts(dst); + return g.hasEdge(src, dst) && p.test(src) && p.test(dst); } };