cdc.graphs.impl.ImplicitSubGraph Maven / Gradle / Ivy
package cdc.graphs.impl;
import cdc.graphs.EdgeDirection;
import cdc.graphs.EdgeTip;
import cdc.graphs.GraphAdapter;
import cdc.util.function.IterableUtils;
/**
* Base implementation of implicit subgraph.
*
* Concrete implementation must implement:
*
* - {@code containsNode}.
*
- {@code containsEdge} if necessary.
* Default implementation accepts all edges that connect 2 accepted nodes.
*
*
* @author Damien Carbonne
*
* @param Node type.
* @param Edge type.
*/
public abstract class ImplicitSubGraph extends GraphFilter {
protected ImplicitSubGraph(GraphAdapter delegate) {
super(delegate);
}
@Override
public Iterable extends N> getNodes() {
return IterableUtils.filter(delegate.getNodes(), this::containsNode);
}
@Override
public Iterable extends E> getEdges() {
return IterableUtils.filter(delegate.getEdges(), this::containsEdge);
}
@Override
public boolean containsEdge(E edge) {
return getDelegate().containsEdge(edge)
&& containsNode(getDelegate().getTip(edge, EdgeTip.SOURCE))
&& containsNode(getDelegate().getTip(edge, EdgeTip.TARGET));
}
@Override
public Iterable extends E> getEdges(N node,
EdgeDirection direction) {
return IterableUtils.filter(delegate.getEdges(node, direction),
this::containsEdge);
}
@Override
public N getTip(E edge,
EdgeTip tip) {
return delegate.getTip(edge, tip);
}
}