1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.database;
18 
19 /**
20  * A specialization of {@link Observable} for {@link DataSetObserver}
21  * that provides methods for sending notifications to a list of
22  * {@link DataSetObserver} objects.
23  */
24 public class DataSetObservable extends Observable<DataSetObserver> {
25     /**
26      * Invokes {@link DataSetObserver#onChanged} on each observer.
27      * Called when the contents of the data set have changed.  The recipient
28      * will obtain the new contents the next time it queries the data set.
29      */
notifyChanged()30     public void notifyChanged() {
31         synchronized(mObservers) {
32             // since onChanged() is implemented by the app, it could do anything, including
33             // removing itself from {@link mObservers} - and that could cause problems if
34             // an iterator is used on the ArrayList {@link mObservers}.
35             // to avoid such problems, just march thru the list in the reverse order.
36             for (int i = mObservers.size() - 1; i >= 0; i--) {
37                 mObservers.get(i).onChanged();
38             }
39         }
40     }
41 
42     /**
43      * Invokes {@link DataSetObserver#onInvalidated} on each observer.
44      * Called when the data set is no longer valid and cannot be queried again,
45      * such as when the data set has been closed.
46      */
notifyInvalidated()47     public void notifyInvalidated() {
48         synchronized (mObservers) {
49             for (int i = mObservers.size() - 1; i >= 0; i--) {
50                 mObservers.get(i).onInvalidated();
51             }
52         }
53     }
54 }
55