diff --git a/app/src/main/java/com/nicobrailo/pianoli/AppConfigTrigger.java b/app/src/main/java/com/nicobrailo/pianoli/AppConfigTrigger.java index 12b88bf..4571aba 100644 --- a/app/src/main/java/com/nicobrailo/pianoli/AppConfigTrigger.java +++ b/app/src/main/java/com/nicobrailo/pianoli/AppConfigTrigger.java @@ -1,6 +1,7 @@ package com.nicobrailo.pianoli; import android.util.Log; +import androidx.annotation.NonNull; import java.util.Arrays; import java.util.HashSet; @@ -44,6 +45,13 @@ class AppConfigTrigger implements PianoListener { *
*/ private final Set
+ * Since this callback is required for this trigger to do anything at all, it would have been preferable
+ * to require this is provided in the constructor. Alas, the way we initialise our upstream PianoCanvas
+ * via XML definition precludes this.
+ *
true
if the listener list changed as a result of this add,
* false
if it was already subscribed.
*/
- public boolean addListener(PianoListener l) {
- if (l != null // avoid NullPointerExceptions on notify
- && !listeners.contains(l)) { // don't double-add listeners, to avoid double-triggers
- listeners.add(l);
- return true;
+ public boolean addListener(@NonNull PianoListener l) {
+ Objects.requireNonNull(l, "Listeners must not be null to avoid NullPointerExceptions on notify");
+
+ if (!listeners.contains(l)) { // don't double-add listeners, to avoid double-triggers
+ return listeners.add(l);
}
return false;
}
diff --git a/app/src/main/java/com/nicobrailo/pianoli/PianoCanvas.java b/app/src/main/java/com/nicobrailo/pianoli/PianoCanvas.java
index 9c6204a..c70a23a 100644
--- a/app/src/main/java/com/nicobrailo/pianoli/PianoCanvas.java
+++ b/app/src/main/java/com/nicobrailo/pianoli/PianoCanvas.java
@@ -126,7 +126,12 @@ public void reInitPiano(Context context, String prefSoundset) {
Log.i("PianOli::PianoCanvas", "re-initialising Piano - DONE");
}
- public void setConfigRequestCallback(AppConfigTrigger.AppConfigCallback cb) {
+ /**
+ * Dependency injection for context-handling stuff: switching to settings activity, and showing toasts.
+ *
+ * @see AppConfigTrigger#setConfigRequestCallback(AppConfigTrigger.AppConfigCallback)
+ */
+ public void setConfigRequestCallback(@NonNull AppConfigTrigger.AppConfigCallback cb) {
this.appConfigTrigger.setConfigRequestCallback(cb);
}
diff --git a/app/src/test/java/com/nicobrailo/pianoli/PianoListenerTest.java b/app/src/test/java/com/nicobrailo/pianoli/PianoListenerTest.java
index 7e1283a..d1d1525 100644
--- a/app/src/test/java/com/nicobrailo/pianoli/PianoListenerTest.java
+++ b/app/src/test/java/com/nicobrailo/pianoli/PianoListenerTest.java
@@ -77,12 +77,13 @@ public void doubleRemoval() {
@Test
public void nullSafeAdd() {
- assertFalse(piano.addListener(null), "null-listener should be silently accepted, but not do anything");
+ //noinspection DataFlowIssue // intentionally violating @NonNull to test handling.
+ assertThrows(NullPointerException.class, () -> piano.addListener(null), "null-listener should not be accepted");
assertDoesNotThrow(() -> piano.doKeyDown(0),
- "after adding a null-listener, notification should not explode");
+ "after adding a null-listener, listener notification should not explode");
assertDoesNotThrow(() -> piano.doKeyUp(0),
- "after adding a null-listener, notification should not explode");
+ "after adding a null-listener, listener notification should not explode");
}
@Test