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

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

There is a newer version: 2.5.0
Show newest version
package org.opentripplanner.gtfs.mapping;

import org.opentripplanner.graph_builder.DataImportIssueStore;
import org.opentripplanner.graph_builder.issues.ParentStationNotFound;
import org.opentripplanner.model.BoardingArea;
import org.opentripplanner.model.FeedScopedId;
import org.opentripplanner.model.Station;
import org.opentripplanner.model.StationElement;
import org.opentripplanner.model.Stop;
import org.opentripplanner.model.impl.EntityById;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Links child stops with parent stations by adding bidirectional object references.
 */
class StopToParentStationLinker {
    private final EntityById otpStations = new EntityById<>();
    private final EntityById otpStationElements = new EntityById<>();
    private final EntityById boardingAreas = new EntityById<>();

    private final Map stationElementsToStations = new HashMap<>();
    private final Map boardingAreasToStops = new HashMap<>();

    private final DataImportIssueStore issueStore;

    StopToParentStationLinker(DataImportIssueStore issueStore) {
        this.issueStore = issueStore;
    }

    void addStation(Station station) {
        otpStations.add(station);
    }

    void addStationElement(StationElement stationElement, String stationId) {
        otpStationElements.add(stationElement);
        if (stationId != null) {
            stationElementsToStations.put(
                stationElement,
                new FeedScopedId(stationElement.getId().getFeedId(), stationId)
            );
        }
    }

    void addBoardingArea(BoardingArea boardingArea, String stopId) {
        boardingAreas.add(boardingArea);
        boardingAreasToStops.put(
            boardingArea,
            new FeedScopedId(boardingArea.getId().getFeedId(), stopId)
        );
    }

    void link() {
        for (Map.Entry entry : stationElementsToStations.entrySet()) {
            StationElement stationElement = entry.getKey();
            FeedScopedId stationId = entry.getValue();
            Station otpStation = otpStations.get(stationId);
            if (otpStation == null) {
                issueStore.add(new ParentStationNotFound(stationElement, stationId.getId()));
            } else {
                stationElement.setParentStation(otpStation);
                if (stationElement instanceof Stop) {
                    otpStation.addChildStop((Stop) stationElement);
                }
            }
        }

        for (Map.Entry entry : boardingAreasToStops.entrySet()) {
            BoardingArea boardingArea = entry.getKey();
            FeedScopedId stopId = entry.getValue();
            StationElement otpStop = otpStationElements.get(stopId);
            if (!(otpStop instanceof Stop)) {
                issueStore.add(new ParentStationNotFound(boardingArea, stopId.getId()));
            } else {
                boardingArea.setParentStop((Stop) otpStop);
                ((Stop) otpStop).addBoardingArea(boardingArea);
                boardingArea.setParentStation(otpStop.getParentStation());
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy