Skip to content

Commit

Permalink
Refactor some aspects of alert handling
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Jan 10, 2024
1 parent d7e9ecd commit bfd802b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,29 @@
package org.opentripplanner.routing.alertpatch;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
* Represents a period of time, in terms of seconds in [start, end)
*
* @author novalis
*/
public class TimePeriod {

// TODO OTP2 - This class is used both for internal and external API representation,
// - a external API version should be created to decouple the internal model
// - from any API usage.

public static final long OPEN_ENDED = Long.MAX_VALUE;

@JsonSerialize
public long startTime;
public final long startTime;

@JsonSerialize
public long endTime;
public final long endTime;

public TimePeriod(long start, long end) {
this.startTime = start;
this.endTime = end;
}

public TimePeriod() {}

public int hashCode() {
return (int) ((startTime & 0x7fff) + (endTime & 0x7fff));
}

public boolean equals(Object o) {
if (!(o instanceof TimePeriod)) {
if (!(o instanceof TimePeriod other)) {
return false;
}
TimePeriod other = (TimePeriod) o;
return other.startTime == startTime && other.endTime == endTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.google.transit.realtime.GtfsRealtime.TimeRange;
import com.google.transit.realtime.GtfsRealtime.TripDescriptor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.opentripplanner.framework.i18n.I18NString;
Expand All @@ -27,8 +26,6 @@

/**
* This updater only includes GTFS-Realtime Service Alert feeds.
*
* @author novalis
*/
public class AlertsUpdateHandler {

Expand All @@ -46,15 +43,16 @@ public class AlertsUpdateHandler {
private final DirectionMapper directionMapper = new DirectionMapper(DataImportIssueStore.NOOP);

public void update(FeedMessage message) {
Collection<TransitAlert> alerts = new ArrayList<>();
for (FeedEntity entity : message.getEntityList()) {
if (!entity.hasAlert()) {
continue;
}
GtfsRealtime.Alert alert = entity.getAlert();
String id = entity.getId();
alerts.add(mapAlert(id, alert));
}
var alerts = message
.getEntityList()
.stream()
.filter(FeedEntity::hasAlert)
.map(e -> {
var alert = e.getAlert();
var id = e.getId();
return mapAlert(id, alert);
})
.toList();
transitAlertService.setAlerts(alerts);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected void runPolling() {
final FeedMessage feed = otpHttpClient.getAndMap(
URI.create(url),
this.headers.asMap(),
FeedMessage.PARSER::parseFrom
FeedMessage::parseFrom
);

long feedTimestamp = feed.getHeader().getTimestamp();
Expand Down
Loading

0 comments on commit bfd802b

Please sign in to comment.