Skip to content

Commit 73473af

Browse files
committed
feat(64982): integrate autoformatter
1 parent 5d78b6e commit 73473af

21 files changed

Lines changed: 490 additions & 479 deletions

.github/workflows/test-and-build.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ jobs:
1313
distribution: 'adopt'
1414
java-version: '11'
1515
cache: 'maven'
16+
- name: Run Spotless Apply
17+
run: mvn spotless:apply
18+
env:
19+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1620
- name: Build with Maven
1721
run: mvn --file pom.xml clean install
1822
env:

eclipse-java-formatter.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<profiles version="13">
3+
<profile kind="CodeFormatterProfile" name="Custom-4spaces-NoCommentSplit" version="13">
4+
5+
<setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="space"/>
6+
<setting id="org.eclipse.jdt.core.formatter.tabulation.size" value="4"/>
7+
<setting id="org.eclipse.jdt.core.formatter.indentation.size" value="4"/>
8+
<setting id="org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations" value="false"/>
9+
10+
<setting id="org.eclipse.jdt.core.formatter.lineSplit" value="120"/>
11+
12+
<setting id="org.eclipse.jdt.core.formatter.comment.line_length" value="9999"/>
13+
<setting id="org.eclipse.jdt.core.formatter.comment.format_line_comments" value="false"/>
14+
<setting id="org.eclipse.jdt.core.formatter.comment.format_block_comments" value="false"/>
15+
<setting id="org.eclipse.jdt.core.formatter.comment.format_javadoc_comments" value="false"/>
16+
17+
</profile>
18+
</profiles>

pom.xml

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
<maven.compiler.source>11</maven.compiler.source>
1818
<maven.compiler.target>11</maven.compiler.target>
19-
<common.version>2.0.3-RC6</common.version>
19+
<common.version>2.0.5</common.version>
2020
<testcontainers.version>1.17.6</testcontainers.version>
21+
<spotlessMavenPlugin.version>2.43.0</spotlessMavenPlugin.version>
22+
<googleJavaFormat.version>1.17.0</googleJavaFormat.version>
2123
</properties>
2224
<profiles>
2325
<!-- The Configuration of the unit-test profile -->
@@ -205,6 +207,32 @@
205207
</executions>
206208
</plugin>
207209

210+
<plugin>
211+
<groupId>com.diffplug.spotless</groupId>
212+
<artifactId>spotless-maven-plugin</artifactId>
213+
<version>${spotlessMavenPlugin.version}</version>
214+
<configuration>
215+
<java>
216+
<eclipse>
217+
<file>${project.basedir}/eclipse-java-formatter.xml</file>
218+
</eclipse>
219+
</java>
220+
</configuration>
221+
<executions>
222+
<execution>
223+
<id>spotless-check</id>
224+
<goals>
225+
<goal>check</goal>
226+
</goals>
227+
</execution>
228+
<execution>
229+
<id>spotless-apply</id>
230+
<goals>
231+
<goal>apply</goal>
232+
</goals>
233+
</execution>
234+
</executions>
235+
</plugin>
208236
</plugins>
209237

210238
</build>

src/main/java/fi/hsl/transitdata/tripupdate/application/MessageRouter.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,23 @@ private void registerHandlers(PulsarApplicationContext context) {
5757

5858
final boolean filterTrainData = config.getBoolean("validator.filterTrainData");
5959

60-
processors.put(ProtobufSchema.InternalMessagesStopEstimate, new StopEstimateProcessor(tripUpdateProcessor, filterTrainData));
61-
processors.put(ProtobufSchema.InternalMessagesTripCancellation, new TripCancellationProcessor(tripUpdateProcessor, filterTrainData));
60+
processors.put(ProtobufSchema.InternalMessagesStopEstimate,
61+
new StopEstimateProcessor(tripUpdateProcessor, filterTrainData));
62+
processors.put(ProtobufSchema.InternalMessagesTripCancellation,
63+
new TripCancellationProcessor(tripUpdateProcessor, filterTrainData));
6264
}
6365

6466
private List<ITripUpdateValidator> registerTripUpdateValidators() {
6567

6668
List<ITripUpdateValidator> tripUpdateValidators = new ArrayList<>();
6769

68-
tripUpdateValidators.add(new TripUpdateMaxAgeValidator(config.getDuration("validator.tripUpdateMaxAge", TimeUnit.SECONDS)));
69-
tripUpdateValidators.add(new PrematureDeparturesValidator(config.getDuration("validator.tripUpdateMinTimeBeforeDeparture", TimeUnit.SECONDS),
70+
tripUpdateValidators
71+
.add(new TripUpdateMaxAgeValidator(config.getDuration("validator.tripUpdateMaxAge", TimeUnit.SECONDS)));
72+
tripUpdateValidators.add(new PrematureDeparturesValidator(
73+
config.getDuration("validator.tripUpdateMinTimeBeforeDeparture", TimeUnit.SECONDS),
7074
config.getString("validator.timezone")));
71-
tripUpdateValidators.add(new MissingEstimatesValidator(config.getInt("validator.tripUpdateMaxMissingEstimates")));
75+
tripUpdateValidators
76+
.add(new MissingEstimatesValidator(config.getInt("validator.tripUpdateMaxMissingEstimates")));
7277

7378
return tripUpdateValidators;
7479

@@ -84,7 +89,8 @@ public void handleMessage(Message received) {
8489
if (processor != null) {
8590
if (processor.validateMessage(received.getData())) {
8691

87-
Optional<AbstractMessageProcessor.TripUpdateWithId> maybeTripUpdate = processor.processMessage(received);
92+
Optional<AbstractMessageProcessor.TripUpdateWithId> maybeTripUpdate = processor
93+
.processMessage(received);
8894
if (maybeTripUpdate.isPresent()) {
8995
final AbstractMessageProcessor.TripUpdateWithId pair = maybeTripUpdate.get();
9096
final GtfsRealtime.TripUpdate tripUpdate = pair.getTripUpdate();
@@ -93,9 +99,13 @@ public void handleMessage(Message received) {
9399
final boolean isValid = validator.validate(tripUpdate);
94100
if (!isValid) {
95101
final GtfsRealtime.TripDescriptor trip = tripUpdate.getTrip();
96-
log.debug("Trip update for {} / {} / {} / {} failed validation when validating with {}", trip.getRouteId(), trip.getDirectionId(), trip.getStartDate(), trip.getStartTime(), validator.getClass().getName());
102+
log.debug(
103+
"Trip update for {} / {} / {} / {} failed validation when validating with {}",
104+
trip.getRouteId(), trip.getDirectionId(), trip.getStartDate(),
105+
trip.getStartTime(), validator.getClass().getName());
97106

98-
messageStats.incrementInvalidTripUpdates("validator-" + validator.getClass().getSimpleName());
107+
messageStats.incrementInvalidTripUpdates(
108+
"validator-" + validator.getClass().getSimpleName());
99109
}
100110
return isValid;
101111
});
@@ -118,12 +128,11 @@ public void handleMessage(Message received) {
118128
}
119129
});
120130

121-
consumer.acknowledgeAsync(received)
122-
.exceptionally(throwable -> {
123-
log.error("Failed to ack Pulsar message", throwable);
124-
return null;
125-
})
126-
.thenRun(() -> {});
131+
consumer.acknowledgeAsync(received).exceptionally(throwable -> {
132+
log.error("Failed to ack Pulsar message", throwable);
133+
return null;
134+
}).thenRun(() -> {
135+
});
127136
} catch (Exception e) {
128137
log.error("Exception while handling message", e);
129138
}
@@ -133,22 +142,23 @@ public void handleMessage(Message received) {
133142
}
134143
}
135144

136-
private void sendTripUpdate(final AbstractMessageProcessor.TripUpdateWithId tuIdPair, final long pulsarEventTimestamp) {
145+
private void sendTripUpdate(final AbstractMessageProcessor.TripUpdateWithId tuIdPair,
146+
final long pulsarEventTimestamp) {
137147
messageStats.incrementMessagesSent();
138148

139149
final String tripId = tuIdPair.getTripId();
140150
final GtfsRealtime.TripUpdate tripUpdate = tuIdPair.getTripUpdate();
141151

142152
debouncer.debounce(tripId, () -> {
143-
GtfsRealtime.FeedMessage feedMessage = FeedMessageFactory.createDifferentialFeedMessage(tripId, tripUpdate, tripUpdate.getTimestamp());
144-
producer.newMessage()
145-
.key(tripId)
146-
.eventTime(pulsarEventTimestamp)
147-
.property(TransitdataProperties.KEY_PROTOBUF_SCHEMA, TransitdataProperties.ProtobufSchema.GTFS_TripUpdate.toString())
148-
.value(feedMessage.toByteArray())
149-
.sendAsync()
153+
GtfsRealtime.FeedMessage feedMessage = FeedMessageFactory.createDifferentialFeedMessage(tripId, tripUpdate,
154+
tripUpdate.getTimestamp());
155+
producer.newMessage().key(tripId).eventTime(pulsarEventTimestamp)
156+
.property(TransitdataProperties.KEY_PROTOBUF_SCHEMA,
157+
TransitdataProperties.ProtobufSchema.GTFS_TripUpdate.toString())
158+
.value(feedMessage.toByteArray()).sendAsync()
150159
.thenRun(() -> log.debug("Sending TripUpdate for tripId {} with {} StopTimeUpdates and status {}",
151-
tripId, tripUpdate.getStopTimeUpdateCount(), tripUpdate.getTrip().getScheduleRelationship()));
160+
tripId, tripUpdate.getStopTimeUpdateCount(),
161+
tripUpdate.getTrip().getScheduleRelationship()));
152162
});
153163
}
154164
}

src/main/java/fi/hsl/transitdata/tripupdate/application/MessageStats.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package fi.hsl.transitdata.tripupdate.application;
22

3-
43
import org.slf4j.Logger;
54

65
import java.util.HashMap;
@@ -71,12 +70,11 @@ public void logAndReset(Logger logger) {
7170

7271
@Override
7372
public String toString() {
74-
final String reasonsText = invalidTripUpdateReasons.entrySet().stream().map(entry -> entry.getKey() + ": " + entry.getValue()).collect(Collectors.joining(", "));
73+
final String reasonsText = invalidTripUpdateReasons.entrySet().stream()
74+
.map(entry -> entry.getKey() + ": " + entry.getValue()).collect(Collectors.joining(", "));
7575

76-
return "Message stats:\n"+
77-
"\tStart time: " + getDurationSecs() + " seconds ago\n" +
78-
"\tMessages received: " + messagesReceived + "\n" +
79-
"\tMessages sent: " + messagesSent + "\n" +
80-
"\tInvalid trip updates: " + invalidTripUpdates + "(" + reasonsText + ")";
76+
return "Message stats:\n" + "\tStart time: " + getDurationSecs() + " seconds ago\n" + "\tMessages received: "
77+
+ messagesReceived + "\n" + "\tMessages sent: " + messagesSent + "\n" + "\tInvalid trip updates: "
78+
+ invalidTripUpdates + "(" + reasonsText + ")";
8179
}
8280
}

src/main/java/fi/hsl/transitdata/tripupdate/gtfsrt/GtfsRtFactory.java

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,34 @@ public static GtfsRealtime.TripUpdate.StopTimeUpdate newStopTimeUpdate(InternalM
1818
}
1919

2020
public static GtfsRealtime.TripUpdate.StopTimeUpdate newStopTimeUpdateFromPrevious(
21-
final InternalMessages.StopEstimate stopEstimate,
22-
GtfsRealtime.TripUpdate.StopTimeUpdate previousUpdate) {
21+
final InternalMessages.StopEstimate stopEstimate, GtfsRealtime.TripUpdate.StopTimeUpdate previousUpdate) {
2322

2423
GtfsRealtime.TripUpdate.StopTimeUpdate.Builder stopTimeUpdateBuilder = null;
2524
if (previousUpdate != null) {
2625
stopTimeUpdateBuilder = previousUpdate.toBuilder();
2726
} else {
2827
String stopId = stopEstimate.getStopId();
2928
int stopSequence = stopEstimate.getStopSequence();
30-
stopTimeUpdateBuilder = GtfsRealtime.TripUpdate.StopTimeUpdate.newBuilder()
31-
.setStopId(stopId)
29+
stopTimeUpdateBuilder = GtfsRealtime.TripUpdate.StopTimeUpdate.newBuilder().setStopId(stopId)
3230
.setStopSequence(stopSequence);
3331
}
3432

3533
switch (stopEstimate.getStatus()) {
36-
case SKIPPED:
37-
stopTimeUpdateBuilder.setScheduleRelationship(GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.SKIPPED);
34+
case SKIPPED :
35+
stopTimeUpdateBuilder
36+
.setScheduleRelationship(GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.SKIPPED);
3837
break;
39-
case SCHEDULED:
40-
stopTimeUpdateBuilder.setScheduleRelationship(GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.SCHEDULED);
38+
case SCHEDULED :
39+
stopTimeUpdateBuilder
40+
.setScheduleRelationship(GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.SCHEDULED);
4141
break;
42-
case NO_DATA:
42+
case NO_DATA :
4343
//If there is no data for current or previous stop time update, set ScheduleRelationship to NO_DATA
44-
if (previousUpdate == null || previousUpdate.getScheduleRelationship() == GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.NO_DATA) {
45-
stopTimeUpdateBuilder.setScheduleRelationship(GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.NO_DATA);
46-
//Otherwise use ScheduleRelationship of previous stop time update
44+
if (previousUpdate == null || previousUpdate
45+
.getScheduleRelationship() == GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.NO_DATA) {
46+
stopTimeUpdateBuilder.setScheduleRelationship(
47+
GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.NO_DATA);
48+
//Otherwise use ScheduleRelationship of previous stop time update
4749
} else {
4850
stopTimeUpdateBuilder.setScheduleRelationship(previousUpdate.getScheduleRelationship());
4951
}
@@ -54,8 +56,8 @@ public static GtfsRealtime.TripUpdate.StopTimeUpdate newStopTimeUpdateFromPrevio
5456
// GTFS-RT treats times in seconds
5557
long stopEventTimeInSeconds = stopEstimate.getEstimatedTimeUtcMs() / 1000;
5658

57-
GtfsRealtime.TripUpdate.StopTimeEvent.Builder stopTimeEvent = GtfsRealtime.TripUpdate.StopTimeEvent.newBuilder()
58-
.setTime(stopEventTimeInSeconds);
59+
GtfsRealtime.TripUpdate.StopTimeEvent.Builder stopTimeEvent = GtfsRealtime.TripUpdate.StopTimeEvent
60+
.newBuilder().setTime(stopEventTimeInSeconds);
5961

6062
//Whether the event was observed in real world (i.e. not an estimate)
6163
final boolean observedTime = stopEstimate.hasObservedTime() && stopEstimate.getObservedTime();
@@ -64,10 +66,10 @@ public static GtfsRealtime.TripUpdate.StopTimeUpdate newStopTimeUpdateFromPrevio
6466
}
6567

6668
switch (stopEstimate.getType()) {
67-
case ARRIVAL:
69+
case ARRIVAL :
6870
stopTimeUpdateBuilder.setArrival(stopTimeEvent);
6971
break;
70-
case DEPARTURE:
72+
case DEPARTURE :
7173
stopTimeUpdateBuilder.setDeparture(stopTimeEvent);
7274
break;
7375
}
@@ -83,54 +85,50 @@ public static long lastModified(InternalMessages.StopEstimate estimate) {
8385
public static GtfsRealtime.TripUpdate newTripUpdate(InternalMessages.StopEstimate estimate) {
8486
final int direction = PubtransFactory.joreDirectionToGtfsDirection(estimate.getTripInfo().getDirectionId());
8587
String routeId = RouteIdUtils.normalizeRouteId(estimate.getTripInfo().getRouteId());
86-
87-
GtfsRealtime.TripDescriptor.ScheduleRelationship scheduleType = mapInternalScheduleTypeToGtfsRt(estimate.getTripInfo().getScheduleType());
88-
88+
89+
GtfsRealtime.TripDescriptor.ScheduleRelationship scheduleType = mapInternalScheduleTypeToGtfsRt(
90+
estimate.getTripInfo().getScheduleType());
91+
8992
GtfsRealtime.TripDescriptor.Builder tripDescriptor = GtfsRealtime.TripDescriptor.newBuilder()
90-
.setRouteId(routeId)
91-
.setDirectionId(direction)
92-
.setStartDate(estimate.getTripInfo().getOperatingDay()) // Local date as String
93+
.setRouteId(routeId).setDirectionId(direction).setStartDate(estimate.getTripInfo().getOperatingDay()) // Local date as String
9394
.setStartTime(estimate.getTripInfo().getStartTime()) // Local time as String
9495
.setScheduleRelationship(scheduleType);
95-
96+
9697
//Trips outside of static schedule need trip ID to be accepted by OTP
9798
if (scheduleType != GtfsRealtime.TripDescriptor.ScheduleRelationship.SCHEDULED) {
9899
tripDescriptor.setTripId(generateTripId(estimate.getTripInfo()));
99100
}
100101

101-
GtfsRealtime.TripUpdate.Builder tripUpdateBuilder = GtfsRealtime.TripUpdate.newBuilder()
102-
.setTrip(tripDescriptor)
102+
GtfsRealtime.TripUpdate.Builder tripUpdateBuilder = GtfsRealtime.TripUpdate.newBuilder().setTrip(tripDescriptor)
103103
.setTimestamp(lastModified(estimate));
104104

105105
return tripUpdateBuilder.build();
106106
}
107107

108-
private static GtfsRealtime.TripDescriptor.ScheduleRelationship mapInternalScheduleTypeToGtfsRt(InternalMessages.TripInfo.ScheduleType scheduleType) {
108+
private static GtfsRealtime.TripDescriptor.ScheduleRelationship mapInternalScheduleTypeToGtfsRt(
109+
InternalMessages.TripInfo.ScheduleType scheduleType) {
109110
switch (scheduleType) {
110-
case ADDED:
111+
case ADDED :
111112
return GtfsRealtime.TripDescriptor.ScheduleRelationship.ADDED;
112-
case UNSCHEDULED:
113+
case UNSCHEDULED :
113114
return GtfsRealtime.TripDescriptor.ScheduleRelationship.UNSCHEDULED;
114-
case SCHEDULED:
115-
default:
115+
case SCHEDULED :
116+
default :
116117
return GtfsRealtime.TripDescriptor.ScheduleRelationship.SCHEDULED;
117118
}
118119
}
119120

120-
public static GtfsRealtime.TripUpdate newTripUpdate(InternalMessages.TripCancellation cancellation, long timestampMs) {
121+
public static GtfsRealtime.TripUpdate newTripUpdate(InternalMessages.TripCancellation cancellation,
122+
long timestampMs) {
121123
final int gtfsRtDirection = PubtransFactory.joreDirectionToGtfsDirection(cancellation.getDirectionId());
122124
String routeId = RouteIdUtils.normalizeRouteId(cancellation.getRouteId());
123125

124-
GtfsRealtime.TripDescriptor tripDescriptor = GtfsRealtime.TripDescriptor.newBuilder()
125-
.setRouteId(routeId)
126-
.setDirectionId(gtfsRtDirection)
127-
.setStartDate(cancellation.getStartDate())
126+
GtfsRealtime.TripDescriptor tripDescriptor = GtfsRealtime.TripDescriptor.newBuilder().setRouteId(routeId)
127+
.setDirectionId(gtfsRtDirection).setStartDate(cancellation.getStartDate())
128128
.setStartTime(cancellation.getStartTime())
129-
.setScheduleRelationship(GtfsRealtime.TripDescriptor.ScheduleRelationship.CANCELED)
130-
.build();
129+
.setScheduleRelationship(GtfsRealtime.TripDescriptor.ScheduleRelationship.CANCELED).build();
131130

132-
GtfsRealtime.TripUpdate.Builder tripUpdateBuilder = GtfsRealtime.TripUpdate.newBuilder()
133-
.setTrip(tripDescriptor)
131+
GtfsRealtime.TripUpdate.Builder tripUpdateBuilder = GtfsRealtime.TripUpdate.newBuilder().setTrip(tripDescriptor)
134132
.setTimestamp(timestampMs / 1000);
135133

136134
return tripUpdateBuilder.build();
@@ -142,6 +140,7 @@ public static GtfsRealtime.TripUpdate newTripUpdate(InternalMessages.TripCancell
142140
* @return Trip ID
143141
*/
144142
private static String generateTripId(InternalMessages.TripInfo tripInfo) {
145-
return tripInfo.getRouteId()+"_"+tripInfo.getOperatingDay()+"_"+tripInfo.getStartTime()+"_"+tripInfo.getDirectionId();
143+
return tripInfo.getRouteId() + "_" + tripInfo.getOperatingDay() + "_" + tripInfo.getStartTime() + "_"
144+
+ tripInfo.getDirectionId();
146145
}
147146
}

0 commit comments

Comments
 (0)