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 @android.ravenwood.annotation.RavenwoodKeepWholeClass
25 public class DataSetObservable extends Observable<DataSetObserver> {
26     /**
27      * Invokes {@link DataSetObserver#onChanged} on each observer.
28      * Called when the contents of the data set have changed.  The recipient
29      * will obtain the new contents the next time it queries the data set.
30      */
notifyChanged()31     public void notifyChanged() {
32         synchronized(mObservers) {
33             // since onChanged() is implemented by the app, it could do anything, including
34             // removing itself from {@link mObservers} - and that could cause problems if
35             // an iterator is used on the ArrayList {@link mObservers}.
36             // to avoid such problems, just march thru the list in the reverse order.
37             for (int i = mObservers.size() - 1; i >= 0; i--) {
38                 mObservers.get(i).onChanged();
39             }
40         }
41     }
42 
43     /**
44      * Invokes {@link DataSetObserver#onInvalidated} on each observer.
45      * Called when the data set is no longer valid and cannot be queried again,
46      * such as when the data set has been closed.
47      */
notifyInvalidated()48     public void notifyInvalidated() {
49         synchronized (mObservers) {
50             for (int i = mObservers.size() - 1; i >= 0; i--) {
51                 mObservers.get(i).onInvalidated();
52             }
53         }
54     }
55 }
56