Package androidx.databinding
Class CallbackRegistry<C,T,A>
- java.lang.Object
-
- androidx.databinding.CallbackRegistry<C,T,A>
-
- Type Parameters:
C- The callback type.T- The notification sender type. Typically this is the containing class.A- Opaque argument used to pass additional data beyond an int.
- All Implemented Interfaces:
java.lang.Cloneable
public class CallbackRegistry<C,T,A> extends java.lang.Object implements java.lang.CloneableA utility for storing and notifying callbacks. This class supports reentrant modification of the callbacks during notification without adversely disrupting notifications. A common pattern for callbacks is to receive a notification and then remove themselves. This class handles this behavior with constant memory under most circumstances.A subclass of
CallbackRegistry.NotifierCallbackmust be passed to the constructor to define how notifications should be called. That implementation does the actual notification on the listener. It is typically a static instance that can be reused for all similar CallbackRegistries.This class supports only callbacks with at most three parameters. Typically, these are the notification originator and a parameter, with another to indicate which method to call, but these may be used as required. If more than three parameters are required or primitive types other than the single int provided must be used,
Ashould be some kind of containing structure that the subclass may reuse between notifications.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCallbackRegistry.NotifierCallback<C,T,A>Class used to notify events from CallbackRegistry.
-
Constructor Summary
Constructors Constructor Description CallbackRegistry(CallbackRegistry.NotifierCallback<C,T,A> notifier)Creates an EventRegistry that notifies the event with notifier.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(C callback)Add a callback to be notified.voidclear()Removes all callbacks from the list.CallbackRegistry<C,T,A>clone()java.util.ArrayList<C>copyCallbacks()Makes a copy of the registered callbacks and returns it.voidcopyCallbacks(java.util.List<C> callbacks)Modifiescallbacksto contain all callbacks in the CallbackRegistry.booleanisEmpty()Returns true if there are no registered callbacks or false otherwise.voidnotifyCallbacks(T sender, int arg, A arg2)Notify all callbacks.voidremove(C callback)Remove a callback.
-
-
-
Constructor Detail
-
CallbackRegistry
public CallbackRegistry(CallbackRegistry.NotifierCallback<C,T,A> notifier)
Creates an EventRegistry that notifies the event with notifier.- Parameters:
notifier- The class to use to notify events.
-
-
Method Detail
-
notifyCallbacks
public void notifyCallbacks(T sender, int arg, A arg2)
Notify all callbacks.- Parameters:
sender- The originator. This is an opaque parameter passed toCallbackRegistry.NotifierCallback.onNotifyCallback(Object, Object, int, Object)arg- An opaque parameter passed toCallbackRegistry.NotifierCallback.onNotifyCallback(Object, Object, int, Object)arg2- An opaque parameter passed toCallbackRegistry.NotifierCallback.onNotifyCallback(Object, Object, int, Object)
-
add
public void add(C callback)
Add a callback to be notified. If the callback is already in the list, another won't be added. This does not affect current notifications.- Parameters:
callback- The callback to add.
-
remove
public void remove(C callback)
Remove a callback. This callback won't be notified after this call completes.- Parameters:
callback- The callback to remove.
-
copyCallbacks
public java.util.ArrayList<C> copyCallbacks()
Makes a copy of the registered callbacks and returns it.- Returns:
- a copy of the registered callbacks.
-
copyCallbacks
public void copyCallbacks(java.util.List<C> callbacks)
Modifiescallbacksto contain all callbacks in the CallbackRegistry.- Parameters:
callbacks- modified to contain all callbacks registered to receive events.
-
isEmpty
public boolean isEmpty()
Returns true if there are no registered callbacks or false otherwise.- Returns:
- true if there are no registered callbacks or false otherwise.
-
clear
public void clear()
Removes all callbacks from the list.
-
clone
public CallbackRegistry<C,T,A> clone()
- Overrides:
clonein classjava.lang.Object- Returns:
- A copy of the CallbackRegistry with all callbacks listening to both instances.
-
-