Skip to content

Commit

Permalink
Merge pull request #364 from HSLdevcom/develop
Browse files Browse the repository at this point in the history
Update topic parsing
  • Loading branch information
thjarvin authored Oct 18, 2024
2 parents 3bc86e0 + ba9de36 commit af79c6c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fi.hsl</groupId>
<artifactId>transitdata-common</artifactId>
<version>1.6.1</version>
<version>1.6.4</version>
<packaging>jar</packaging>
<name>Common utilities for Transitdata projects</name>
<properties>
Expand Down
36 changes: 34 additions & 2 deletions src/main/java/fi/hsl/common/hfp/HfpParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
import java.sql.Timestamp;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import java.io.ByteArrayOutputStream;

public class HfpParser {
private static final Logger log = LoggerFactory.getLogger(HfpParser.class);

static final Pattern topicVersionRegex = Pattern.compile("(^v\\d+|dev)");

private static Set<String> vehiclesWithEmptyTransportMode = new HashSet<>();

// Let's use dsl-json (https://github.com/ngs-doo/dsl-json) for performance.
// Based on this benchmark: https://github.com/fabienrenaud/java-json-benchmark
Expand All @@ -32,6 +36,15 @@ public class HfpParser {

//Note! Apparently not thread safe, for per thread reuse use ThreadLocal pattern or create separate instances
final DslJson<Object> dslJson = new DslJson<>(Settings.withRuntime().allowArrayFormat(true).includeServiceLoader());

private static void foundVehicleWithEmptyTransportMode(String uniqueVehicleId) {
vehiclesWithEmptyTransportMode.add(uniqueVehicleId);

if (vehiclesWithEmptyTransportMode.size() > 100) {
log.warn("Vehicles with empty transport mode: " + vehiclesWithEmptyTransportMode);
vehiclesWithEmptyTransportMode.clear();
}
}

@NotNull
public static HfpParser newInstance() {
Expand Down Expand Up @@ -223,13 +236,32 @@ public static Hfp.Topic parseTopic(@NotNull String topic, long receivedAtMs) thr
}

final String strTransportMode = parts[index++];
boolean transportModeIsEmpty = false;
if (strTransportMode != null && !strTransportMode.isEmpty()) {
final Hfp.Topic.TransportMode transportMode = safeValueOf(Hfp.Topic.TransportMode.class, strTransportMode).orElseThrow(() -> new InvalidHfpTopicException("Unknown transport mode: " + topic));
builder.setTransportMode(transportMode);
} else {
transportModeIsEmpty = true;
}

final String operatorIdStr = parts[index++];
try {
builder.setOperatorId(Integer.parseInt(operatorIdStr));
} catch (NumberFormatException e) {
throw new InvalidHfpTopicException("Operator id is not number: " + operatorIdStr);
}

final String vehicleNumberStr = parts[index++];
try {
builder.setVehicleNumber(Integer.parseInt(vehicleNumberStr));
} catch (NumberFormatException e) {
throw new InvalidHfpTopicException("Vehicle number is not number: " + vehicleNumberStr);
}
builder.setOperatorId(Integer.parseInt(parts[index++]));
builder.setVehicleNumber(Integer.parseInt(parts[index++]));

builder.setUniqueVehicleId(createUniqueVehicleId(builder.getOperatorId(), builder.getVehicleNumber()));
if (transportModeIsEmpty) {
foundVehicleWithEmptyTransportMode(builder.getUniqueVehicleId());
}
if (index + 6 <= parts.length) {
HfpValidator.validateString(parts[index++]).ifPresent(builder::setRouteId);
safeParseInt(parts[index++]).ifPresent(builder::setDirectionId);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/fi/hsl/common/hfp/HfpParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,10 @@ public void parseGeohashWithOverloadedZeroLevel() throws Exception {

@Test
public void parseTopicWhenItemsMissing() throws Exception {
Hfp.Topic meta = parseAndValidateTopic("/hfp/v1/journey/ongoing//0022/00854//////////");
Hfp.Topic meta = parseAndValidateTopic("/hfp/v1/journey/ongoing/bus/0022/00854//////////");
assertEquals(Hfp.Topic.JourneyType.journey, meta.getJourneyType());
assertEquals(Hfp.Topic.TemporalType.ongoing, meta.getTemporalType());
assertFalse(meta.hasTransportMode());
assertEquals(Hfp.Topic.TransportMode.bus, meta.getTransportMode());

assertEquals(22, meta.getOperatorId());
assertEquals(854, meta.getVehicleNumber());
Expand Down

0 comments on commit af79c6c

Please sign in to comment.