Skip to content

Commit 9e4ee73

Browse files
authored
Merge pull request #63 from HSLdevcom/feature/64979-use-autoformatting
feat(64979): integrate autoformatter
2 parents 238d9a4 + ed7c0c0 commit 9e4ee73

10 files changed

Lines changed: 146 additions & 96 deletions

File tree

.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: 'temurin'
1414
java-version: '24'
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<maven.compiler.source>24</maven.compiler.source>
1818
<maven.compiler.target>24</maven.compiler.target>
1919
<common.version>2.0.7</common.version>
20+
<spotlessMavenPlugin.version>2.43.0</spotlessMavenPlugin.version>
21+
<googleJavaFormat.version>1.17.0</googleJavaFormat.version>
2022
</properties>
2123

2224
<dependencies>
@@ -107,6 +109,32 @@
107109
<target>24</target>
108110
</configuration>
109111
</plugin>
112+
<plugin>
113+
<groupId>com.diffplug.spotless</groupId>
114+
<artifactId>spotless-maven-plugin</artifactId>
115+
<version>${spotlessMavenPlugin.version}</version>
116+
<configuration>
117+
<java>
118+
<eclipse>
119+
<file>${project.basedir}/eclipse-java-formatter.xml</file>
120+
</eclipse>
121+
</java>
122+
</configuration>
123+
<executions>
124+
<execution>
125+
<id>spotless-check</id>
126+
<goals>
127+
<goal>check</goal>
128+
</goals>
129+
</execution>
130+
<execution>
131+
<id>spotless-apply</id>
132+
<goals>
133+
<goal>apply</goal>
134+
</goals>
135+
</execution>
136+
</executions>
137+
</plugin>
110138

111139
</plugins>
112140

src/main/java/fi/hsl/transitdata/pulsarpubtransconnect/ArrivalHandler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public class ArrivalHandler extends PubtransTableHandler {
1515
static final TransitdataSchema schema;
1616
static {
1717
int defaultVersion = PubtransTableProtos.ROIArrival.newBuilder().getSchemaVersion();
18-
schema = new TransitdataSchema(TransitdataProperties.ProtobufSchema.PubtransRoiArrival, Optional.of(defaultVersion));
18+
schema = new TransitdataSchema(TransitdataProperties.ProtobufSchema.PubtransRoiArrival,
19+
Optional.of(defaultVersion));
1920
}
2021

2122
public ArrivalHandler(PulsarApplicationContext context) {
@@ -31,15 +32,15 @@ protected String getTimetabledDateTimeColumnName() {
3132
protected TransitdataSchema getSchema() {
3233
return schema;
3334
}
34-
35+
3536
@Override
3637
protected Map<String, Long> getTableColumnToIdMap(ResultSet resultSet) throws SQLException {
3738
return Map.of();
3839
}
3940

4041
@Override
41-
protected byte[] createPayload(PubtransTableProtos.Common common, Map<String,
42-
Long> columnToIdMap, PubtransTableProtos.DOITripInfo tripInfo) throws SQLException {
42+
protected byte[] createPayload(PubtransTableProtos.Common common, Map<String, Long> columnToIdMap,
43+
PubtransTableProtos.DOITripInfo tripInfo) throws SQLException {
4344
PubtransTableProtos.ROIArrival.Builder arrivalBuilder = PubtransTableProtos.ROIArrival.newBuilder();
4445
arrivalBuilder.setSchemaVersion(arrivalBuilder.getSchemaVersion());
4546
arrivalBuilder.setCommon(common);

src/main/java/fi/hsl/transitdata/pulsarpubtransconnect/DepartureHandler.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public class DepartureHandler extends PubtransTableHandler {
1717
private static final String COLUMN_HAS_SERVICE_REQUIREMENT_ID = "HasServiceRequirementId";
1818
static {
1919
int defaultVersion = PubtransTableProtos.ROIDeparture.newBuilder().getSchemaVersion();
20-
schema = new TransitdataSchema(TransitdataProperties.ProtobufSchema.PubtransRoiDeparture, Optional.of(defaultVersion));
20+
schema = new TransitdataSchema(TransitdataProperties.ProtobufSchema.PubtransRoiDeparture,
21+
Optional.of(defaultVersion));
2122
}
2223

2324
public DepartureHandler(PulsarApplicationContext context) {
@@ -33,15 +34,16 @@ protected String getTimetabledDateTimeColumnName() {
3334
protected TransitdataSchema getSchema() {
3435
return schema;
3536
}
36-
37+
3738
@Override
3839
protected Map<String, Long> getTableColumnToIdMap(ResultSet resultSet) throws SQLException {
3940
Map<String, Long> columnToIdMap = new HashMap<>();
4041
if (resultSet.getBytes(COLUMN_HAS_DESTINATION_DISPLAY_ID) != null) {
4142
columnToIdMap.put(COLUMN_HAS_DESTINATION_DISPLAY_ID, resultSet.getLong(COLUMN_HAS_DESTINATION_DISPLAY_ID));
4243
}
4344
if (resultSet.getBytes(COLUMN_HAS_DESTINATION_STOP_AREA_GID) != null) {
44-
columnToIdMap.put(COLUMN_HAS_DESTINATION_STOP_AREA_GID, resultSet.getLong(COLUMN_HAS_DESTINATION_STOP_AREA_GID));
45+
columnToIdMap.put(COLUMN_HAS_DESTINATION_STOP_AREA_GID,
46+
resultSet.getLong(COLUMN_HAS_DESTINATION_STOP_AREA_GID));
4547
}
4648
if (resultSet.getBytes(COLUMN_HAS_SERVICE_REQUIREMENT_ID) != null) {
4749
columnToIdMap.put(COLUMN_HAS_SERVICE_REQUIREMENT_ID, resultSet.getLong(COLUMN_HAS_SERVICE_REQUIREMENT_ID));
@@ -51,7 +53,7 @@ protected Map<String, Long> getTableColumnToIdMap(ResultSet resultSet) throws SQ
5153

5254
@Override
5355
protected byte[] createPayload(PubtransTableProtos.Common common, Map<String, Long> columnToIdMap,
54-
PubtransTableProtos.DOITripInfo tripInfo) throws SQLException {
56+
PubtransTableProtos.DOITripInfo tripInfo) throws SQLException {
5557
PubtransTableProtos.ROIDeparture.Builder departureBuilder = PubtransTableProtos.ROIDeparture.newBuilder();
5658
departureBuilder.setSchemaVersion(departureBuilder.getSchemaVersion());
5759
departureBuilder.setCommon(common);

src/main/java/fi/hsl/transitdata/pulsarpubtransconnect/Main.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,9 @@ public static void main(String[] args) {
6666
Config config = null;
6767
if (type == PubtransTableType.ROI_ARRIVAL) {
6868
config = ConfigParser.createConfig("arrival.conf");
69-
}
70-
else if (type == PubtransTableType.ROI_DEPARTURE) {
69+
} else if (type == PubtransTableType.ROI_DEPARTURE) {
7170
config = ConfigParser.createConfig("departure.conf");
72-
}
73-
else {
71+
} else {
7472
log.error("Failed to get table name from PT_TABLE-env variable, exiting application");
7573
System.exit(1);
7674
}
@@ -90,37 +88,32 @@ else if (type == PubtransTableType.ROI_DEPARTURE) {
9088

9189
scheduler.scheduleAtFixedRate(() -> {
9290
try {
93-
if(connector.checkPrecondition()) {
91+
if (connector.checkPrecondition()) {
9492
connector.queryAndProcessResults();
95-
}
96-
else {
93+
} else {
9794
log.error("Pubtrans poller precondition failed, skipping the current poll cycle.");
9895
}
99-
}
100-
catch (SQLServerException sqlServerException) {
96+
} catch (SQLServerException sqlServerException) {
10197
// Occasionally (once every 2 hours?) the driver throws us out as a deadlock victim.
10298
// There's no easy way to fix the root problem so lets just convert it to warning.
10399
// More info: https://stackoverflow.com/questions/8390322/cause-of-a-process-being-a-deadlock-victim
104100
if (sqlServerException.getErrorCode() == SQL_SERVER_ERROR_DEADLOCK_VICTIM) {
105-
log.warn("SQL Server evicted us as deadlock victim. ignoring this for now...", sqlServerException);
106-
}
107-
else {
101+
log.warn("SQL Server evicted us as deadlock victim. ignoring this for now...",
102+
sqlServerException);
103+
} else {
108104
log.error("SQL Server Unexpected error code, shutting down", sqlServerException);
109105
log.warn("Driver Error code: {}", sqlServerException.getErrorCode());
110106
closeApplication(app, scheduler);
111107
}
112-
}
113-
catch (JedisException | SQLException | PulsarClientException connectionException) {
108+
} catch (JedisException | SQLException | PulsarClientException connectionException) {
114109
log.error("Connection problem, cannot recover so shutting down", connectionException);
115110
closeApplication(app, scheduler);
116-
}
117-
catch (Exception e) {
111+
} catch (Exception e) {
118112
log.error("Unknown error at Pubtrans scheduler, shutting down", e);
119113
closeApplication(app, scheduler);
120114
}
121115
}, 0, 1, TimeUnit.SECONDS);
122-
}
123-
catch (Exception e) {
116+
} catch (Exception e) {
124117
log.error("Exception at Main", e);
125118
}
126119
}

src/main/java/fi/hsl/transitdata/pulsarpubtransconnect/PubtransConnector.java

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ public class PubtransConnector {
3838
private RedisStore redisStore;
3939
private Producer<byte[]> producer;
4040

41-
private PubtransConnector() {}
41+
private PubtransConnector() {
42+
}
4243

43-
public static PubtransConnector newInstance(Connection connection,
44-
PulsarApplicationContext context,
45-
PubtransTableType tableType) throws RuntimeException {
44+
public static PubtransConnector newInstance(Connection connection, PulsarApplicationContext context,
45+
PubtransTableType tableType) throws RuntimeException {
4646
PubtransConnector connector = new PubtransConnector();
4747

4848
connector.connection = connection;
@@ -53,19 +53,20 @@ public static PubtransConnector newInstance(Connection connection,
5353
connector.queryString = queryString(config);
5454
connector.enableCacheCheck = config.getBoolean("application.enableCacheTimestampCheck");
5555
connector.cacheMaxAgeInMins = config.getInt("application.cacheMaxAgeInMinutes");
56-
connector.queryTimeoutSecs = (int)config.getDuration("pubtrans.queryTimeout", TimeUnit.SECONDS);
56+
connector.queryTimeoutSecs = (int) config.getDuration("pubtrans.queryTimeout", TimeUnit.SECONDS);
5757

58-
log.info("Cache pre-condition enabled: {} with max age {}", connector.enableCacheCheck, connector.cacheMaxAgeInMins);
58+
log.info("Cache pre-condition enabled: {} with max age {}", connector.enableCacheCheck,
59+
connector.cacheMaxAgeInMins);
5960

6061
log.info("TableType: " + tableType);
6162
switch (tableType) {
62-
case ROI_ARRIVAL:
63+
case ROI_ARRIVAL :
6364
connector.handler = new ArrivalHandler(context);
6465
break;
65-
case ROI_DEPARTURE:
66+
case ROI_DEPARTURE :
6667
connector.handler = new DepartureHandler(context);
6768
break;
68-
default:
69+
default :
6970
throw new IllegalArgumentException("Table type not supported");
7071
}
7172
return connector;
@@ -75,16 +76,9 @@ private static String queryString(Config config) {
7576
String longName = config.getString("pubtrans.longName");
7677
String shortName = config.getString("pubtrans.shortName");
7778

78-
return "SELECT * FROM " +
79-
longName +
80-
" AS " +
81-
shortName +
82-
" WHERE " +
83-
shortName + ".LastModifiedUTCDateTime > ? " +
84-
" ORDER BY " +
85-
shortName + ".LastModifiedUTCDateTime, " +
86-
shortName + ".IsOnDatedVehicleJourneyId, " +
87-
shortName + ".JourneyPatternSequenceNumber DESC";
79+
return "SELECT * FROM " + longName + " AS " + shortName + " WHERE " + shortName
80+
+ ".LastModifiedUTCDateTime > ? " + " ORDER BY " + shortName + ".LastModifiedUTCDateTime, " + shortName
81+
+ ".IsOnDatedVehicleJourneyId, " + shortName + ".JourneyPatternSequenceNumber DESC";
8882
}
8983

9084
public boolean checkPrecondition() {
@@ -95,10 +89,11 @@ public boolean checkPrecondition() {
9589
log.info("Cache last known update: {}", lastUpdate);
9690
if (lastUpdate.isPresent()) {
9791
OffsetDateTime dt = OffsetDateTime.parse(lastUpdate.get(), DateTimeFormatter.ISO_DATE_TIME);
98-
return isCacheValid(dt, cacheMaxAgeInMins);
99-
} else {
100-
log.error("Could not find last cache update timestamp from redis");
101-
return false;
92+
return isCacheValid(dt, cacheMaxAgeInMins);
93+
} else {
94+
log.error("Could not find last cache update timestamp from redis");
95+
return false;
96+
10297
}
10398
}
10499

@@ -109,10 +104,11 @@ static boolean isCacheValid(OffsetDateTime lastCacheUpdate, final int cacheMaxAg
109104
final long secondsSinceUpdate = Duration.between(lastCacheUpdate, now).get(ChronoUnit.SECONDS);
110105
final long minutesSinceUpdate = Math.floorDiv(secondsSinceUpdate, 60);
111106
log.info("Minutes since last cache update: {}", minutesSinceUpdate);
112-
log.info("Current time {}, last update {}} => mins from prev update: {}", now, lastCacheUpdate, minutesSinceUpdate);
107+
log.info("Current time {}, last update {}} => mins from prev update: {}", now, lastCacheUpdate,
108+
minutesSinceUpdate);
113109
return minutesSinceUpdate <= cacheMaxAgeInMins;
114110
}
115-
111+
116112
static void closeQuery(final ResultSet resultSet, final Statement statement) {
117113
if (resultSet != null) {
118114
try {
@@ -147,7 +143,7 @@ public void queryAndProcessResults() throws SQLException, PulsarClientException
147143
statement.setQueryTimeout(queryTimeoutSecs);
148144

149145
resultSet = statement.executeQuery();
150-
146+
151147
produceMessages(handler.handleResultSet(resultSet, statement, queryStartTime));
152148
} catch (PulsarClientException | SQLException e) {
153149
closeQuery(resultSet, statement);
@@ -161,17 +157,16 @@ private void produceMessages(Collection<TypedMessageBuilder<byte[]>> messages) t
161157
}
162158

163159
for (TypedMessageBuilder<byte[]> msg : messages) {
164-
msg.sendAsync()
165-
.exceptionally(throwable -> {
166-
log.error("Failed to send Pulsar message", throwable);
167-
return null;
168-
});
160+
msg.sendAsync().exceptionally(throwable -> {
161+
log.error("Failed to send Pulsar message", throwable);
162+
return null;
163+
});
169164

170165
}
171166
//If we want to get Pulsar Exceptions to bubble up into this thread we need to do a sync flush for all pending messages.
172167
producer.flush();
173168

174-
log.info("{} messages written. Latest timestamp: {} Total query and processing time: {} ms", messages.size(), handler.getLastModifiedTimeStamp(), System.currentTimeMillis() - this.queryStartTime);
169+
log.info("{} messages written. Latest timestamp: {} Total query and processing time: {} ms", messages.size(),
170+
handler.getLastModifiedTimeStamp(), System.currentTimeMillis() - this.queryStartTime);
175171
}
176172
}
177-

0 commit comments

Comments
 (0)