/** If the listener already exists, it is not added again. */ public void addListener(Listener listener) { if (listener == null) throw new IllegalArgumentException("listener cannot be null."); synchronized (listenerLock) { Listener[] listeners = this.listeners; int n = listeners.length; for (int i = 0; i < n; i++) if (listener == listeners[i]) return; Listener[] newListeners = new Listener[n + 1]; newListeners[0] = listener; System.arraycopy(listeners, 0, newListeners, 1, n); this.listeners = newListeners; } if (TRACE) trace("kryonet", "Connection listener added: " + listener.getClass().getName()); }
public void removeListener(Listener listener) { if (listener == null) throw new IllegalArgumentException("listener cannot be null."); synchronized (listenerLock) { Listener[] listeners = this.listeners; int n = listeners.length; if (n == 0) return; Listener[] newListeners = new Listener[n - 1]; for (int i = 0, ii = 0; i < n; i++) { Listener copyListener = listeners[i]; if (listener == copyListener) continue; if (ii == n - 1) return; newListeners[ii++] = copyListener; } this.listeners = newListeners; } if (TRACE) trace("kryonet", "Connection listener removed: " + listener.getClass().getName()); }