events: Enable compile-time warnings as errors
Also fix issues reported by SonarQube and other small nits.
Change-Id: I4f4a12302d5907b5a65bf38a67dd186195517f76
diff --git a/src/main/java/com/googlesource/gerrit/plugins/events/StreamEvents.java b/src/main/java/com/googlesource/gerrit/plugins/events/StreamEvents.java
index b424923..cccaf07 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/events/StreamEvents.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/events/StreamEvents.java
@@ -54,7 +54,7 @@
)
protected void parseId(String arg) throws IOException {
resume = 0;
- if (arg.equals("0")) {
+ if ("0".equals(arg)) {
return;
}
@@ -155,10 +155,8 @@
protected void startFlush() throws IOException {
synchronized (crossThreadlock) {
- if (flusherTask == null && !shuttingDown) {
- if (sent < events.getHead()) {
- flusherTask = threadPool.submit(flusherRunnable);
- }
+ if (!isFlushing() && !shuttingDown && !isUpToDate()) {
+ flusherTask = threadPool.submit(flusherRunnable);
}
}
}
@@ -178,7 +176,7 @@
synchronized (crossThreadlock) {
boolean alreadyShuttingDown = shuttingDown;
shuttingDown = true;
- if (flusherTask != null) {
+ if (isFlushing()) {
flusherTask.cancel(true);
} else if (!alreadyShuttingDown) {
onExit(0);
@@ -211,8 +209,7 @@
protected void flushBatch() throws IOException {
String uuid = events.getUuid().toString();
int processed = 0;
- long head = events.getHead();
- while (sent < head && processed < BATCH_SIZE) {
+ while (!isUpToDate() && processed < BATCH_SIZE) {
long sending = sent + 1;
String event = events.get(sending);
if (Thread.interrupted() || stdout.checkError()) {
@@ -248,4 +245,12 @@
stdout.flush();
}
}
+
+ protected boolean isUpToDate() throws IOException {
+ return sent >= events.getHead();
+ }
+
+ protected boolean isFlushing() {
+ return flusherTask != null;
+ }
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FileValue.java b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FileValue.java
index c291638..05c7770 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FileValue.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FileValue.java
@@ -43,9 +43,10 @@
/**
* Auto setup the serializer based on the type used to initialize the class.
*
- * <p>Must be called with a supported type before use if a serializer has been set manualy. Safe
+ * <p>Must be called with a supported type before use if a serializer has been set manually. Safe
* to call if the Serializer was already set.
*/
+ @SuppressWarnings("unchecked") // we check the type of init, so these casts are safe
protected void initSerializer(T init) {
if (serializer == null) {
if (init instanceof String) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/Fs.java b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/Fs.java
index f3f7444..1a21941 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/Fs.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/Fs.java
@@ -172,11 +172,11 @@
/** Read the contents of a UTF_8 encoded file as a String */
protected static String readUtf8Unsafe(Path file) throws IOException {
- StringBuffer buffer = new StringBuffer();
+ StringBuilder builder = new StringBuilder();
for (String line : Files.readAllLines(file, StandardCharsets.UTF_8)) {
- buffer.append(line);
+ builder.append(line);
}
- return buffer.toString();
+ return builder.toString();
}
/** Write the contents of a String as a UTF_8 encoded file */
diff --git a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FsTransaction.java b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FsTransaction.java
index dc7bc0e..9340bd0 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FsTransaction.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/FsTransaction.java
@@ -117,7 +117,7 @@
* Used to atomically delete a directory tree when the src directory name is guaranteed to be
* unique.
*/
- public static void renameAndDeleteUnique(Path src, Path del) throws IOException {
+ public static void renameAndDeleteUnique(Path src, Path del) {
Path reparented = Fs.reparent(src, del);
Fs.tryAtomicMove(src, reparented);
Fs.tryRecursiveDelete(reparented);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/UpdatableFileValue.java b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/UpdatableFileValue.java
index b3779be..67d51b0 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/UpdatableFileValue.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/events/fsstore/UpdatableFileValue.java
@@ -221,7 +221,7 @@
}
/** Contains phase 6 */
- protected void clean() throws IOException {
+ protected void clean() {
if (committed) {
FsTransaction.renameAndDeleteUnique(upaths.udir, updatable.paths.delete); // Phase 6
updatable.cleanPreserved();
@@ -273,7 +273,7 @@
if (shouldCompleteOngoing()) {
Path ongoing = Nfs.getFirstDirEntry(paths.update);
if (ongoing != null) {
- // Attempt to complete previous updates;
+ // Attempt to complete previous updates
return createUniqueUpdate(Fs.basename(ongoing).toString(), false, maxTries);
}
}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/events/fsstore/FsSequenceTest.java b/src/test/java/com/googlesource/gerrit/plugins/events/fsstore/FsSequenceTest.java
index 4d809c1..68c89cd 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/events/fsstore/FsSequenceTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/events/fsstore/FsSequenceTest.java
@@ -62,7 +62,7 @@
@Test
public void testSpinIncrement() throws IOException {
long next = seq.get() + (long) 1;
- assertEquals(next, (long) seq.spinIncrement(1));
+ assertEquals(next, seq.spinIncrement(1));
}
@Test