Skip to content

Commit

Permalink
review #83: stronger non-null on Piano.addListener.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jules Kerssemakers committed Jan 2, 2024
1 parent 5f891bc commit 1e42198
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions app/src/main/java/com/nicobrailo/pianoli/Piano.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.nicobrailo.pianoli;

import android.util.Log;
import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
* Backing model / state of our virtual piano keyboard.
Expand Down Expand Up @@ -140,9 +142,10 @@ private boolean isOutOfRange(int key_idx) {
* @return Per the {@link java.util.Collection#add(Object)} contract, <code>true</code> if the listener list changed as a result of this add,
* <code>false</code> 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
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
listeners.add(l);
return true;
}
Expand Down

0 comments on commit 1e42198

Please sign in to comment.