It sounds like you'd need to implement your own UniquenessFilter, one which checks uniqueness on the last pair of nodes in a Path, right?
Look at Uniqueness class for an example of a factory for common UniquenessFilters. I would implement it something like this (type in this mail client only):
class NodePairUniqueness implements UniquenessFilter {
private final Set<Pair<Long,Long>> pairs = new HashSet<Pair<Long,Long>>();
public bollean checkFirst( TraversalBranch branch ) {
return true;
}
public boolean check( TraversalBranch branch ) {
Path position = branch.position();
Node nextToLast = null;
Node last = null;
Iterator<Node> nodes = position.nodes().iterator();
while ( nodes.hasNext() ) {
nextToLast = last;
last = nodes.next();
}
Pair<Long,Long> lastTwoNodes = Pair.of( nextToLast.getId(), last.getId() );
return pairs.add( lastTwoNodes );
}
}
I also realize that it would be grand with a Path#reversedNodes() or something this started from the end node and back to the start node. And memory-wise it would be more efficient with a custom PrimitiveLongPair class or similar instead of the Pair<Long,Long>.