Skip to content

Commit

Permalink
Build SIRI loader in factory, inject into source
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Dec 11, 2024
1 parent 71fae3d commit 790bdc6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.opentripplanner.updater.siri.updater.SiriETHttpTripUpdateSource;
import org.opentripplanner.updater.siri.updater.SiriETUpdater;
import org.opentripplanner.updater.siri.updater.SiriHttpLoader;
import org.opentripplanner.updater.siri.updater.SiriLoaderFactory;
import org.opentripplanner.updater.siri.updater.SiriSXUpdater;
import org.opentripplanner.updater.siri.updater.google.SiriETGooglePubsubUpdater;
import org.opentripplanner.updater.siri.updater.lite.SiriETLiteHttpTripUpdateSource;
Expand Down Expand Up @@ -191,7 +192,10 @@ private List<GraphUpdater> createUpdatersFromConfig() {
new SiriETUpdater(
configItem,
provideSiriTimetableSnapshot(),
new SiriETHttpTripUpdateSource(configItem.sourceParameters()),
new SiriETHttpTripUpdateSource(
configItem.sourceParameters(),
SiriLoaderFactory.createLoader(configItem)
),
TripUpdateMetrics.streaming(configItem)
)
);
Expand All @@ -201,7 +205,10 @@ private List<GraphUpdater> createUpdatersFromConfig() {
new SiriETUpdater(
configItem,
provideSiriTimetableSnapshot(),
new SiriETLiteHttpTripUpdateSource(configItem.sourceParameters()),
new SiriETLiteHttpTripUpdateSource(
configItem.sourceParameters(),
SiriLoaderFactory.createLoader(configItem)
),
TripUpdateMetrics.batch(configItem)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public class SiriETHttpTripUpdateSource implements EstimatedTimetableSource {
private UpdateIncrementality updateIncrementality = FULL_DATASET;
private ZonedDateTime lastTimestamp = ZonedDateTime.now().minusMonths(1);

public SiriETHttpTripUpdateSource(Parameters parameters) {
public SiriETHttpTripUpdateSource(Parameters parameters, SiriLoader siriLoader) {
this.url = parameters.url();

this.requestorRef =
parameters.requestorRef() == null || parameters.requestorRef().isEmpty()
? "otp-" + UUID.randomUUID()
: parameters.requestorRef();

this.siriLoader = createLoader(url, parameters);
this.siriLoader = siriLoader;
}

@Override
Expand Down Expand Up @@ -81,22 +81,6 @@ public String toString() {
return ToStringBuilder.of(SiriETHttpTripUpdateSource.class).addStr("url", url).toString();
}

private static SiriLoader createLoader(String url, Parameters parameters) {
// Load real-time updates from a file.
if (SiriFileLoader.matchesUrl(url)) {
return new SiriFileLoader(url);
}
// Fallback to default loader
else {
return new SiriHttpLoader(
url,
parameters.timeout(),
parameters.httpRequestHeaders(),
parameters.previewInterval()
);
}
}

public interface Parameters {
String url();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.List;
import java.util.function.Consumer;
import org.opentripplanner.updater.siri.SiriTimetableSnapshotSource;
import org.opentripplanner.updater.siri.updater.lite.SiriETLiteHttpTripUpdateSource;
import org.opentripplanner.updater.siri.updater.lite.SiriETLiteUpdaterParameters;
import org.opentripplanner.updater.spi.PollingGraphUpdater;
import org.opentripplanner.updater.spi.PollingGraphUpdaterParameters;
import org.opentripplanner.updater.spi.ResultLogger;
Expand Down Expand Up @@ -106,6 +108,8 @@ public String toString() {
}

public interface Parameters extends UrlUpdaterParameters, PollingGraphUpdaterParameters {
String url();

boolean blockReadinessUntilInitialized();

boolean fuzzyTripMatching();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.opentripplanner.updater.siri.updater;

import org.opentripplanner.updater.siri.updater.lite.SiriETLiteUpdaterParameters;
import org.opentripplanner.updater.siri.updater.lite.SiriLiteHttpLoader;

/**
* Constructs a SiriLoader from the parameters of the updater.
*/
public class SiriLoaderFactory {

public static SiriLoader createLoader(SiriETUpdater.Parameters parameters) {
// Load real-time updates from a file.
if (SiriFileLoader.matchesUrl(parameters.url())) {
return new SiriFileLoader(parameters.url());
}
// Fallback to default loader
else {
return switch (parameters) {
case SiriETUpdaterParameters p -> new SiriHttpLoader(
p.url(),
p.timeout(),
p.httpRequestHeaders(),
p.previewInterval()
);
case SiriETLiteUpdaterParameters p -> new SiriLiteHttpLoader(
p.uri(),
p.timeout(),
p.httpRequestHeaders()
);
default -> throw new IllegalArgumentException("Unexpected value: " + parameters);
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.Optional;
import org.opentripplanner.framework.io.OtpHttpClientException;
import org.opentripplanner.updater.siri.updater.EstimatedTimetableSource;
import org.opentripplanner.updater.siri.updater.SiriFileLoader;
import org.opentripplanner.updater.siri.updater.SiriLoader;
import org.opentripplanner.updater.spi.HttpHeaders;
import org.opentripplanner.updater.trip.UpdateIncrementality;
Expand All @@ -31,10 +30,9 @@ public class SiriETLiteHttpTripUpdateSource implements EstimatedTimetableSource

private final SiriLoader siriLoader;

public SiriETLiteHttpTripUpdateSource(Parameters parameters) {
public SiriETLiteHttpTripUpdateSource(Parameters parameters, SiriLoader siriLoader) {
this.parameters = parameters;

this.siriLoader = createLoader(parameters);
this.siriLoader = siriLoader;
}

@Override
Expand Down Expand Up @@ -66,21 +64,6 @@ public String toString() {
.toString();
}

private static SiriLoader createLoader(Parameters parameters) {
// Load real-time updates from a file.
if (SiriFileLoader.matchesUrl(parameters.uri().toString())) {
return new SiriFileLoader(parameters.uri().toString());
}
// Fallback to default loader
else {
return new SiriLiteHttpLoader(
parameters.uri(),
parameters.timeout(),
parameters.httpRequestHeaders()
);
}
}

public interface Parameters {
URI uri();

Expand Down

0 comments on commit 790bdc6

Please sign in to comment.