final static int MAX_TRIES = 10;
public List<Itinerary> fetchRoutesForSpecification( RouteSpecification routeSpecification )
throws FoundNoRoutesException
{
final Location origin = routeSpecification.origin().get();
final Location destination = routeSpecification.destination().get();
List<TransitPath> transitPaths;
List<Itinerary> itineraries = new ArrayList<Itinerary>();
// Try a MAX_TRIES times to avoid empty results too often
int tries = 0;
do
{
try
{
transitPaths = graphTraversalService.findShortestPath( origin.getCode(), destination.getCode() );
}
catch( RemoteException e )
{
logger.error( e.getMessage(), e );
return Collections.emptyList();
}
// The returned result is then translated back into our domain model.
for( TransitPath transitPath : transitPaths )
{
final Itinerary itinerary = toItinerary( transitPath );
// Use the specification to safe-guard against invalid itineraries
// We can use the side-effects free method of the RouteSpecification data object
if( routeSpecification.isSatisfiedBy( itinerary ) )
{
itineraries.add( itinerary );
}
}
}
while( tries++ < MAX_TRIES && itineraries.size() == 0 );
if( itineraries.size() == 0 )
{
throw new FoundNoRoutesException( destination.name().get(),
new LocalDate( routeSpecification.arrivalDeadline().get() ) );
}
return itineraries;
}