All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.gtfs.mapping.TripMapper Maven / Gradle / Ivy

package org.opentripplanner.gtfs.mapping;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.opentripplanner.transit.model.basic.I18NString;
import org.opentripplanner.transit.model.basic.NonLocalizedString;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.util.MapUtils;

/** Responsible for mapping GTFS TripMapper into the OTP model. */
class TripMapper {

  private final RouteMapper routeMapper;
  private final DirectionMapper directionMapper;
  private TranslationHelper translationHelper;

  private final Map mappedTrips = new HashMap<>();

  TripMapper(
    RouteMapper routeMapper,
    DirectionMapper directionMapper,
    TranslationHelper translationHelper
  ) {
    this.routeMapper = routeMapper;
    this.directionMapper = directionMapper;
    this.translationHelper = translationHelper;
  }

  Collection map(Collection trips) {
    return MapUtils.mapToList(trips, this::map);
  }

  Trip map(org.onebusaway.gtfs.model.Trip orginal) {
    return orginal == null ? null : mappedTrips.computeIfAbsent(orginal, this::doMap);
  }

  Collection getMappedTrips() {
    return mappedTrips.values();
  }

  private Trip doMap(org.onebusaway.gtfs.model.Trip rhs) {
    var lhs = Trip.of(AgencyAndIdMapper.mapAgencyAndId(rhs.getId()));

    lhs.withRoute(routeMapper.map(rhs.getRoute()));
    lhs.withServiceId(AgencyAndIdMapper.mapAgencyAndId(rhs.getServiceId()));
    lhs.withShortName(rhs.getTripShortName());
    I18NString tripHeadsign = null;
    if (rhs.getTripHeadsign() != null) {
      tripHeadsign =
        translationHelper.getTranslation(
          org.onebusaway.gtfs.model.Trip.class,
          "tripHeadsign",
          rhs.getId().getId(),
          rhs.getTripHeadsign()
        );
    }
    lhs.withHeadsign(tripHeadsign);
    lhs.withDirection(directionMapper.map(rhs.getDirectionId(), lhs.getId()));
    lhs.withGtfsBlockId(rhs.getBlockId());
    lhs.withShapeId(AgencyAndIdMapper.mapAgencyAndId(rhs.getShapeId()));
    lhs.withWheelchairBoarding(WheelchairAccessibilityMapper.map(rhs.getWheelchairAccessible()));
    lhs.withBikesAllowed(BikeAccessMapper.mapForTrip(rhs));
    lhs.withGtfsFareId(rhs.getFareId());

    return lhs.build();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy