Skip to content

Commit 2087bfd

Browse files
committed
Removed now obsolete curlies and java:S1602
1 parent a96f447 commit 2087bfd

File tree

7 files changed

+78
-120
lines changed

7 files changed

+78
-120
lines changed

application/src/main/java/org/togetherjava/tjbot/commands/basic/DatabaseCommand.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ private void handleGetCommand(@NotNull CommandInteraction event) {
8585
// /db get hello
8686
String key = Objects.requireNonNull(event.getOption(KEY_OPTION)).getAsString();
8787
try {
88-
Optional<String> value = database.read(context -> {
89-
try (var select = context.selectFrom(Storage.STORAGE)) {
90-
return Optional.ofNullable(select.where(Storage.STORAGE.KEY.eq(key)).fetchOne())
91-
.map(StorageRecord::getValue);
92-
}
93-
});
88+
Optional<String> value = database.read(
89+
context -> Optional
90+
.ofNullable(context.selectFrom(Storage.STORAGE)
91+
.where(Storage.STORAGE.KEY.eq(key))
92+
.fetchOne())
93+
.map(StorageRecord::getValue));
9494
if (value.isEmpty()) {
9595
event.reply("Nothing found for the key '" + key + "'").setEphemeral(true).queue();
9696
return;

application/src/main/java/org/togetherjava/tjbot/commands/componentids/ComponentIdStore.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,13 @@ public void putOrThrow(@NotNull UUID uuid, @NotNull ComponentId componentId,
210210
}
211211
}
212212

213-
@SuppressWarnings({"resource", "java:S1602"})
214213
private @NotNull Optional<ComponentId> getFromDatabase(@NotNull UUID uuid) {
215-
return database.read(context -> {
216-
return Optional
217-
.ofNullable(context.selectFrom(ComponentIds.COMPONENT_IDS)
218-
.where(ComponentIds.COMPONENT_IDS.UUID.eq(uuid.toString()))
219-
.fetchOne())
220-
.map(ComponentIdsRecord::getComponentId)
221-
.map(ComponentIdStore::deserializeComponentId);
222-
});
214+
return database.read(context -> Optional
215+
.ofNullable(context.selectFrom(ComponentIds.COMPONENT_IDS)
216+
.where(ComponentIds.COMPONENT_IDS.UUID.eq(uuid.toString()))
217+
.fetchOne())
218+
.map(ComponentIdsRecord::getComponentId)
219+
.map(ComponentIdStore::deserializeComponentId));
223220
}
224221

225222
/**
@@ -230,16 +227,14 @@ public void putOrThrow(@NotNull UUID uuid, @NotNull ComponentId componentId,
230227
* @param uuid the uuid to heat
231228
* @throws IllegalArgumentException if there is no, or multiple, records associated to that UUID
232229
*/
233-
@SuppressWarnings({"resource", "java:S1602"})
234230
private void heatRecord(@NotNull UUID uuid) {
235231
int updatedRecords;
236232
synchronized (storeLock) {
237-
updatedRecords = database.writeAndProvide(context -> {
238-
return context.update(ComponentIds.COMPONENT_IDS)
239-
.set(ComponentIds.COMPONENT_IDS.LAST_USED, Instant.now())
240-
.where(ComponentIds.COMPONENT_IDS.UUID.eq(uuid.toString()))
241-
.execute();
242-
});
233+
updatedRecords =
234+
database.writeAndProvide(context -> context.update(ComponentIds.COMPONENT_IDS)
235+
.set(ComponentIds.COMPONENT_IDS.LAST_USED, Instant.now())
236+
.where(ComponentIds.COMPONENT_IDS.UUID.eq(uuid.toString()))
237+
.execute());
243238
}
244239

245240
// NOTE Case 0, where no records are updated, is ignored on purpose.
@@ -255,7 +250,6 @@ private void evictDatabase() {
255250
AtomicInteger evictedCounter = new AtomicInteger(0);
256251
synchronized (storeLock) {
257252
database.write(context -> {
258-
@SuppressWarnings("resource")
259253
Result<ComponentIdsRecord> oldRecords = context
260254
.selectFrom(ComponentIds.COMPONENT_IDS)
261255
.where(ComponentIds.COMPONENT_IDS.LIFESPAN.notEqual(Lifespan.PERMANENT.name())
@@ -317,13 +311,11 @@ private void logDebugSizeStatistics() {
317311
return;
318312
}
319313

320-
// Without curly braces on the lambda, the call would be ambiguous
321-
@SuppressWarnings("java:S1602")
322314
Map<Lifespan, Integer> lifespanToCount = Arrays.stream(Lifespan.values())
323-
.collect(Collectors.toMap(Function.identity(), lifespan -> database.read(context -> {
324-
return context.fetchCount(ComponentIds.COMPONENT_IDS,
325-
ComponentIds.COMPONENT_IDS.LIFESPAN.eq(lifespan.name()));
326-
})));
315+
.collect(Collectors.toMap(Function.identity(),
316+
lifespan -> database
317+
.read(context -> context.fetchCount(ComponentIds.COMPONENT_IDS,
318+
ComponentIds.COMPONENT_IDS.LIFESPAN.eq(lifespan.name())))));
327319
int recordsCount = lifespanToCount.values().stream().mapToInt(Integer::intValue).sum();
328320

329321
logger.debug("The component id store consists of {} records ({})", recordsCount,

application/src/main/java/org/togetherjava/tjbot/commands/tags/TagSystem.java

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,9 @@ boolean handleIsUnknownTag(@NotNull String id, @NotNull Interaction event) {
8080
* @return whether the tag is known to the tag system
8181
*/
8282
boolean hasTag(String id) {
83-
return database.readTransaction(context -> {
84-
try (var selectFrom = context.selectFrom(Tags.TAGS)) {
85-
return selectFrom.where(Tags.TAGS.ID.eq(id)).fetchOne() != null;
86-
}
87-
});
83+
return database.readTransaction(context -> context.selectFrom(Tags.TAGS)
84+
.where(Tags.TAGS.ID.eq(id))
85+
.fetchOne() != null);
8886
}
8987

9088
/**
@@ -94,12 +92,9 @@ boolean hasTag(String id) {
9492
* @throws IllegalArgumentException if the tag is unknown to the system, see
9593
* {@link #hasTag(String)}
9694
*/
97-
// Execute closes resources; without curly braces on the lambda, the call would be ambiguous
98-
@SuppressWarnings({"resource", "java:S1602"})
9995
void deleteTag(String id) {
100-
int deletedRecords = database.writeAndProvide(context -> {
101-
return context.deleteFrom(Tags.TAGS).where(Tags.TAGS.ID.eq(id)).execute();
102-
});
96+
int deletedRecords = database.writeAndProvide(
97+
context -> context.deleteFrom(Tags.TAGS).where(Tags.TAGS.ID.eq(id)).execute());
10398
if (deletedRecords == 0) {
10499
throw new IllegalArgumentException(
105100
"Unable to delete the tag '%s', it is unknown to the system".formatted(id));
@@ -113,15 +108,13 @@ void deleteTag(String id) {
113108
* @param content the content of the tag to put
114109
*/
115110
// Execute closes resources; without curly braces on the lambda, the call would be ambiguous
116-
@SuppressWarnings({"resource", "java:S1602"})
117111
void putTag(String id, String content) {
118-
database.writeTransaction(context -> {
119-
context.insertInto(Tags.TAGS, Tags.TAGS.ID, Tags.TAGS.CONTENT)
120-
.values(id, content)
121-
.onDuplicateKeyUpdate()
122-
.set(Tags.TAGS.CONTENT, content)
123-
.execute();
124-
});
112+
database.writeTransaction(
113+
context -> context.insertInto(Tags.TAGS, Tags.TAGS.ID, Tags.TAGS.CONTENT)
114+
.values(id, content)
115+
.onDuplicateKeyUpdate()
116+
.set(Tags.TAGS.CONTENT, content)
117+
.execute());
125118
}
126119

127120
/**
@@ -132,12 +125,9 @@ void putTag(String id, String content) {
132125
* @return the content of the tag, if the tag is known to the system
133126
*/
134127
Optional<String> getTag(String id) {
135-
return database.readTransaction(context -> {
136-
try (var selectFrom = context.selectFrom(Tags.TAGS)) {
137-
return Optional.ofNullable(selectFrom.where(Tags.TAGS.ID.eq(id)).fetchOne())
138-
.map(TagsRecord::getContent);
139-
}
140-
});
128+
return database.readTransaction(context -> Optional
129+
.ofNullable(context.selectFrom(Tags.TAGS).where(Tags.TAGS.ID.eq(id)).fetchOne())
130+
.map(TagsRecord::getContent));
141131
}
142132

143133
/**
@@ -146,14 +136,11 @@ Optional<String> getTag(String id) {
146136
* @return a set of all ids known to the system, not backed
147137
*/
148138
Set<String> getAllIds() {
149-
return database.readTransaction(context -> {
150-
try (var select = context.select(Tags.TAGS.ID)) {
151-
return select.from(Tags.TAGS)
152-
.fetch()
153-
.stream()
154-
.map(dbRecord -> dbRecord.getValue(Tags.TAGS.ID))
155-
.collect(Collectors.toSet());
156-
}
157-
});
139+
return database.readTransaction(context -> context.select(Tags.TAGS.ID)
140+
.from(Tags.TAGS)
141+
.fetch()
142+
.stream()
143+
.map(dbRecord -> dbRecord.getValue(Tags.TAGS.ID))
144+
.collect(Collectors.toSet()));
158145
}
159146
}

application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,15 @@ private void checkAuditLogsRoutine() {
259259
private void handleAuditLogs(@NotNull MessageChannel auditLogChannel,
260260
@NotNull PaginationAction<? extends AuditLogEntry, AuditLogPaginationAction> auditLogAction,
261261
long guildId) {
262-
@SuppressWarnings("squid:S1602")
263-
Instant lastAuditLogEntryTimestamp = database.read(context -> {
264-
return Optional
265-
.ofNullable(context.fetchOne(ModAuditLogGuildProcess.MOD_AUDIT_LOG_GUILD_PROCESS,
266-
ModAuditLogGuildProcess.MOD_AUDIT_LOG_GUILD_PROCESS.GUILD_ID.eq(guildId)))
267-
.map(entry -> entry.get(
268-
ModAuditLogGuildProcess.MOD_AUDIT_LOG_GUILD_PROCESS.LAST_PROCESSED_AUDIT_LOG_ENTRY))
269-
.orElse(Instant.now());
270-
});
262+
Instant lastAuditLogEntryTimestamp =
263+
database.read(context -> Optional
264+
.ofNullable(context.fetchOne(
265+
ModAuditLogGuildProcess.MOD_AUDIT_LOG_GUILD_PROCESS,
266+
ModAuditLogGuildProcess.MOD_AUDIT_LOG_GUILD_PROCESS.GUILD_ID
267+
.eq(guildId)))
268+
.map(entry -> entry.get(
269+
ModAuditLogGuildProcess.MOD_AUDIT_LOG_GUILD_PROCESS.LAST_PROCESSED_AUDIT_LOG_ENTRY))
270+
.orElse(Instant.now()));
271271
// NOTE This is a minor race condition. By taking the time before the actual lookup we
272272
// ensure that we do not miss anything but instead it is possible to receive an
273273
// action twice in such a rare case, which is okay.

application/src/test/java/org/togetherjava/tjbot/commands/basic/DatabaseCommandTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ private static SlashCommandEvent createPut(@NotNull String key, @NotNull String
4343
void setupDatabase() throws SQLException {
4444
// TODO This has to be done dynamically by the Flyway script, adjust gradle test settings
4545
database = new Database("jdbc:sqlite:");
46-
database.write(context -> {
47-
context.ddl(Storage.STORAGE).executeBatch();
48-
});
46+
database.write(context -> context.ddl(Storage.STORAGE).executeBatch());
4947
}
5048

5149
@Test
@@ -131,11 +129,11 @@ void getOrPutWithNoTable() throws SQLException {
131129
}
132130

133131
private void assertValueInDatabase(@NotNull String key, @NotNull String value) {
134-
Result<Record1<String>> results = database.read(context -> {
135-
try (var select = context.select(Storage.STORAGE.VALUE)) {
136-
return select.from(Storage.STORAGE).where(Storage.STORAGE.KEY.eq(key)).fetch();
137-
}
138-
});
132+
Result<Record1<String>> results =
133+
database.read(context -> context.select(Storage.STORAGE.VALUE)
134+
.from(Storage.STORAGE)
135+
.where(Storage.STORAGE.KEY.eq(key))
136+
.fetch());
139137
assertEquals(1, results.size());
140138
assertEquals(value, results.get(0).get(Storage.STORAGE.VALUE));
141139
}

logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/logs/LogRepositoryImpl.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,16 @@ public void save(Logevents event) {
4444
}
4545

4646
@Override
47-
@SuppressWarnings("java:S1602") // Curly Braces are necessary here
4847
public List<Logevents> findAll() {
49-
return this.db.read(ctx -> {
50-
return ctx.selectFrom(LOGEVENTS).fetch(this::recordToPojo);
51-
});
48+
return this.db.read(ctx -> ctx.selectFrom(LOGEVENTS).fetch(this::recordToPojo));
5249
}
5350

5451

5552
@Override
56-
@SuppressWarnings("java:S1602") // Curly Braces are necessary here
5753
public List<Logevents> findWithLevelMatching(Collection<String> logLevels) {
58-
return this.db.read(ctx -> {
59-
return ctx.selectFrom(LOGEVENTS)
60-
.where(LOGEVENTS.LEVEL.in(logLevels))
61-
.fetch(this::recordToPojo);
62-
});
54+
return this.db.read(ctx -> ctx.selectFrom(LOGEVENTS)
55+
.where(LOGEVENTS.LEVEL.in(logLevels))
56+
.fetch(this::recordToPojo));
6357
}
6458

6559
private Logevents recordToPojo(final LogeventsRecord logRecord) {

logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/users/UserRepositoryImpl.java

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import static org.togetherjava.tjbot.db.generated.tables.Users.USERS;
1616

1717
@Component
18-
@SuppressWarnings("java:S1602") // Curly Braces are necessary here
1918
public class UserRepositoryImpl implements UserRepository {
2019

2120
private final Database db;
@@ -26,60 +25,48 @@ public UserRepositoryImpl(Database db) {
2625

2726
@Override
2827
public Users findByDiscordID(long discordID) {
29-
return this.db.readTransaction(ctx -> {
30-
return ctx.selectFrom(USERS)
31-
.where(USERS.DISCORDID.eq(discordID))
32-
.fetchOne(this::recordToRole);
33-
});
28+
return this.db.readTransaction(ctx -> ctx.selectFrom(USERS)
29+
.where(USERS.DISCORDID.eq(discordID))
30+
.fetchOne(this::recordToRole));
3431
}
3532

3633
@Override
3734
public Users findByUsername(String username) {
38-
return this.db.readTransaction(ctx -> {
39-
return ctx.selectFrom(USERS)
40-
.where(USERS.USERNAME.eq(username))
41-
.fetchOne(this::recordToRole);
42-
});
35+
return this.db.readTransaction(ctx -> ctx.selectFrom(USERS)
36+
.where(USERS.USERNAME.eq(username))
37+
.fetchOne(this::recordToRole));
4338
}
4439

4540
@Override
4641
public List<Users> findAll() {
47-
return this.db.readTransaction(ctx -> {
48-
return ctx.selectFrom(USERS).fetch(this::recordToRole);
49-
});
42+
return this.db.readTransaction(ctx -> ctx.selectFrom(USERS).fetch(this::recordToRole));
5043
}
5144

5245
@Override
5346
public int count() {
54-
return this.db.readTransaction(ctx -> {
55-
return ctx.fetchCount(USERS);
56-
});
47+
return this.db.readTransaction(ctx -> ctx.fetchCount(USERS));
5748
}
5849

5950
@Override
6051
public void save(Users user) {
61-
this.db.writeTransaction(ctx -> {
62-
ctx.newRecord(USERS)
63-
.setDiscordid(user.getDiscordid())
64-
.setUsername(user.getUsername())
65-
.merge();
66-
});
52+
this.db.writeTransaction(ctx -> ctx.newRecord(USERS)
53+
.setDiscordid(user.getDiscordid())
54+
.setUsername(user.getUsername())
55+
.merge());
6756
}
6857

6958
@Override
7059
public void delete(Users user) {
71-
this.db.writeTransaction(ctx -> {
72-
ctx.deleteFrom(USERS).where(USERS.DISCORDID.eq(user.getDiscordid())).execute();
73-
});
60+
this.db.writeTransaction(ctx -> ctx.deleteFrom(USERS)
61+
.where(USERS.DISCORDID.eq(user.getDiscordid()))
62+
.execute());
7463
}
7564

7665
@Override
7766
public Set<Role> fetchRolesForUser(Users user) {
78-
return new HashSet<>(this.db.readTransaction(ctx -> {
79-
return ctx.selectFrom(Userroles.USERROLES)
80-
.where(Userroles.USERROLES.USERID.eq(user.getDiscordid()))
81-
.fetch(this::recordToRole);
82-
}));
67+
return new HashSet<>(this.db.readTransaction(ctx -> ctx.selectFrom(Userroles.USERROLES)
68+
.where(Userroles.USERROLES.USERID.eq(user.getDiscordid()))
69+
.fetch(this::recordToRole)));
8370
}
8471

8572
@Override

0 commit comments

Comments
 (0)