Code duplication for just an if condition
Just add a parameter boolean incrementOnAnyDepth and in your if condition write

if ( x.getDestination() == e &amp;&amp; (incrementOnAnyDepth || currentDepth == maxDepth) )
{
    counter++;
}`