Skip to content

In DefaultFareServiceImpl fare calculation logic issue. #319

Description

@lbt05

Here is the code in org.opentripplanner.routing.impl.DefaultFareServiceImpl.java :

private Fare _getCost(GraphPath path, Set<String> allowedFareIds) {
    List<Ride> rides = createRides(path);
    // If there are no rides, there's no fare.
    if (rides.size() == 0) {
      return null;
    }
    Fare fare = new Fare();
    boolean hasFare = false;
    for (Map.Entry<FareType, Collection<FareRuleSet>> kv : fareRulesPerType.entrySet()) {
      FareType fareType = kv.getKey();
      Collection<FareRuleSet> fareRules;
      if (allowedFareIds != null) {
        fareRules = kv.getValue().stream()
            .filter(f -> allowedFareIds.contains(f.getFareAttribute().getId().toString()))
            .collect(Collectors.toList());
      } else {
        fareRules = kv.getValue();
      }
      // Get the currency from the first fareAttribute, assuming that all tickets use the same
      // currency.
      Currency currency = null;
      if (fareRules.size() > 0) {
        currency =
            Currency.getInstance(fareRules.iterator().next().getFareAttribute().getCurrencyType());
      }
      hasFare = populateFare(fare, currency, fareType, rides, fareRules);
      // log.info("Fares for {} available: {}", fareType, hasFare);
    }
    return hasFare ? fare : null;
  }

inside the for loop, if we have 4 fareTypes need to calculate, let's assume the result as below:
1_fareType : hasFare = true
2_fareType: hasFare = true
3_fareType: hasFare = true
4_fareType: hasFare = false
what will be the result return from this method ? null right ?
I wonder this is written as design or it's a logic error.
if it's a logic error , I advice to update the code to be
hasFare = populateFare(fare, currency, fareType, rides, fareRules) || hasFare;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions