/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper.reader.gtfs;
import com.carrotsearch.hppc.IntHashSet;
import com.google.transit.realtime.GtfsRealtime;
import static com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.SKIPPED;
public class RealtimeFeed {
private final IntHashSet blockedEdges;
private RealtimeFeed(IntHashSet blockedEdges) {
this.blockedEdges = blockedEdges;
}
public static RealtimeFeed empty() {
return new RealtimeFeed(new IntHashSet());
}
public static RealtimeFeed fromProtobuf(GtfsStorage staticGtfs, GtfsRealtime.FeedMessage feedMessage) {
final IntHashSet blockedEdges = new IntHashSet();
feedMessage.getEntityList().stream()
.filter(GtfsRealtime.FeedEntity::hasTripUpdate)
.map(GtfsRealtime.FeedEntity::getTripUpdate)
.forEach(tripUpdate -> {
final int[] boardEdges = staticGtfs.getBoardEdgesForTrip().get(tripUpdate.getTrip());
final int[] leaveEdges = staticGtfs.getAlightEdgesForTrip().get(tripUpdate.getTrip());
tripUpdate.getStopTimeUpdateList().stream()
.filter(stopTimeUpdate -> stopTimeUpdate.getScheduleRelationship() == SKIPPED)
.mapToInt(stu -> stu.getStopSequence()-1) // stop sequence number is 1-based, not 0-based
.forEach(skippedStopSequenceNumber -> {
blockedEdges.add(boardEdges[skippedStopSequenceNumber]);
blockedEdges.add(leaveEdges[skippedStopSequenceNumber]);
});
});
return new RealtimeFeed(blockedEdges);
}
boolean isBlocked(int edgeId) {
return blockedEdges.contains(edgeId);
}
}