Most visited

Recently visited

CallbackRegistry

public class CallbackRegistry
extends Object implements Cloneable

java.lang.Object
   ↳ android.databinding.CallbackRegistry<C, T, A>
Known Direct Subclasses


A 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.NotifierCallback must 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, A should be some kind of containing structure that the subclass may reuse between notifications.

Summary

Nested classes

class CallbackRegistry.NotifierCallback<C, T, A>

Class used to notify events from CallbackRegistry. 

Public constructors

CallbackRegistry(NotifierCallback<C, T, A> notifier)

Creates an EventRegistry that notifies the event with notifier.

Public methods

void add(C callback)

Add a callback to be notified.

void clear()

Removes all callbacks from the list.

CallbackRegistry<C, T, A> clone()

Creates and returns a copy of this object.

ArrayList<C> copyCallbacks()

Makes a copy of the registered callbacks and returns it.

void copyCallbacks(List<C> callbacks)

Modifies callbacks to contain all callbacks in the CallbackRegistry.

boolean isEmpty()

Returns true if there are no registered callbacks or false otherwise.

void notifyCallbacks(T sender, int arg, A arg2)

Notify all callbacks.

void remove(C callback)

Remove a callback.

Inherited methods

From class java.lang.Object

Public constructors

CallbackRegistry

CallbackRegistry (NotifierCallback<C, T, A> notifier)

Creates an EventRegistry that notifies the event with notifier.

Parameters
notifier NotifierCallback: The class to use to notify events.

Public methods

add

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 C: The callback to add.

clear

void clear ()

Removes all callbacks from the list.

clone

CallbackRegistry<C, T, A> clone ()

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:

 x.clone() != x
will be true, and that the expression:
 x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
 x.clone().equals(x)
will be true, this is not an absolute requirement.

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable and that the return type of the clone method of an array type T[] is T[] where T is any reference or primitive type. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.

Returns
CallbackRegistry<C, T, A> A copy of the CallbackRegistry with all callbacks listening to both instances.

copyCallbacks

ArrayList<C> copyCallbacks ()

Makes a copy of the registered callbacks and returns it.

Returns
ArrayList<C> a copy of the registered callbacks.

copyCallbacks

void copyCallbacks (List<C> callbacks)

Modifies callbacks to contain all callbacks in the CallbackRegistry.

Parameters
callbacks List: modified to contain all callbacks registered to receive events.

isEmpty

boolean isEmpty ()

Returns true if there are no registered callbacks or false otherwise.

Returns
boolean true if there are no registered callbacks or false otherwise.

notifyCallbacks

void notifyCallbacks (T sender, 
                int arg, 
                A arg2)

Notify all callbacks.

Parameters
sender T: The originator. This is an opaque parameter passed to onNotifyCallback(Object, Object, int, Object)
arg int: An opaque parameter passed to onNotifyCallback(Object, Object, int, Object)
arg2 A: An opaque parameter passed to onNotifyCallback(Object, Object, int, Object)

remove

void remove (C callback)

Remove a callback. This callback won't be notified after this call completes.

Parameters
callback C: The callback to remove.

Hooray!