private IntegerAggregate setDistance(Node start, Node finish, IntegerAggregate distance)
  {
    IntegerAggregate oldValue = distances.get(NodePair.get(start, finish));
    boolean addDistance = oldValue == null || (oldValue.isNonterminating() && !distance.isNonterminating());

    if (addDistance)
      distances.put(NodePair.get(start, finish), distance);

    if (addDistance && start.getId() < getMaxStartNode() && finish.getId() < getMaxTargetNode() &&
        start.getId() >= 0 && finish.getId() >= 0)
    {
      for (IntegerAggregationListener listener : listeners)
        listener.aggregateChanged(start.getId(), finish.getId(), distance, this);
    }
    return oldValue;
  }