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 Observable for DataSetObserver that provides methods for 21 * invoking the various callback methods of DataSetObserver. 22 */ 23 public class DataSetObservable extends Observable<DataSetObserver> { 24 /** 25 * Invokes onChanged on each observer. Called when the data set being observed has 26 * changed, and which when read contains the new state of the data. 27 */ 28 public void notifyChanged() { 29 synchronized(mObservers) { 30 // since onChanged() is implemented by the app, it could do anything, including 31 // removing itself from {@link mObservers} - and that could cause problems if 32 // an iterator is used on the ArrayList {@link mObservers}. 33 // to avoid such problems, just march thru the list in the reverse order. 34 for (int i = mObservers.size() - 1; i >= 0; i--) { 35 mObservers.get(i).onChanged(); 36 } 37 } 38 } 39 40 /** 41 * Invokes onInvalidated on each observer. Called when the data set being monitored 42 * has changed such that it is no longer valid. 43 */ 44 public void notifyInvalidated() { 45 synchronized (mObservers) { 46 for (int i = mObservers.size() - 1; i >= 0; i--) { 47 mObservers.get(i).onInvalidated(); 48 } 49 } 50 } 51 } 52