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

org.opentripplanner.routing.impl.TransitAlertServiceImpl Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
package org.opentripplanner.routing.impl;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import org.opentripplanner.model.FeedScopedId;
import org.opentripplanner.model.Stop;
import org.opentripplanner.routing.alertpatch.EntitySelector;
import org.opentripplanner.routing.alertpatch.TransitAlert;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.services.TransitAlertService;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * When an alert is added with more than one transit entity, e.g. a Stop and a Trip, both conditions must be met for
 * the alert to be displayed. This is the case in both the Norwegian interpretation of SIRI, and the GTFS-RT alerts
 * specification.
 *
 */
public class TransitAlertServiceImpl implements TransitAlertService {

    private final Graph graph;

    private Multimap alerts = HashMultimap.create();

    public TransitAlertServiceImpl(Graph graph) {
        this.graph = graph;
    }

    @Override
    public Collection getAllAlerts() {
        return new HashSet<>(alerts.values());
    }

    @Override
    public TransitAlert getAlertById(String id) {
        return alerts
            .values()
            .stream()
            .filter(transitAlert -> transitAlert.getId().equals(id))
            .findAny()
            .orElse(null);
    }

    @Override
    public Collection getStopAlerts(FeedScopedId stopId) {
        Set result = new HashSet<>(alerts.get(new EntitySelector.Stop(stopId)));
        if (result.isEmpty()) {
            // Search for alerts on parent-stop
            if (graph != null && graph.index != null) {
                Stop quay = graph.index.getStopForId(stopId);
                if (quay != null) {
                    
                    // TODO - SIRI: Add alerts from parent- and multimodal-stops
                    /*
                    if ( quay.isPartOfStation()) {
                        // Add alerts for parent-station
                        result.addAll(patchesByStop.getOrDefault(quay.getParentStationFeedScopedId(), Collections.emptySet()));
                    }
                    if (quay.getMultiModalStation() != null) {
                        // Add alerts for multimodal-station
                        result.addAll(patchesByStop.getOrDefault(new FeedScopedId(stop.getAgencyId(), quay.getMultiModalStation()), Collections.emptySet()));
                    }
                    */
                }
            }
        }
        return result;
    }

    @Override
    public Collection getRouteAlerts(FeedScopedId route) {
        return alerts.get(new EntitySelector.Route(route));
    }

    @Override
    public Collection getTripAlerts(FeedScopedId trip) {
        return alerts.get(new EntitySelector.Trip(trip));
    }

    @Override
    public Collection getAgencyAlerts(FeedScopedId agency) {
        return alerts.get(new EntitySelector.Agency(agency));
    }

    @Override
    public Collection getStopAndRouteAlerts(FeedScopedId stop, FeedScopedId route) {
        return alerts.get(new EntitySelector.StopAndRoute(stop, route));
    }

    @Override
    public Collection getStopAndTripAlerts(FeedScopedId stop, FeedScopedId trip) {
        return alerts.get(new EntitySelector.StopAndTrip(stop, trip));
    }

    @Override
    public Collection getTripPatternAlerts(FeedScopedId pattern) {
        return alerts.get(new EntitySelector.TripPattern(pattern));
    }

    @Override
    public void setAlerts(Collection alerts) {
        Multimap newAlerts = HashMultimap.create();
        for (TransitAlert alert : alerts) {
            for (EntitySelector entity : alert.getEntities()) {
                newAlerts.put(entity, alert);
            }
        }

        this.alerts = newAlerts;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy