| 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 | import java.util.ArrayList; |
| 20 | |
| 21 | /** |
| 22 | * Provides methods for registering or unregistering arbitrary observers in an {@link ArrayList}. |
| 23 | * |
| 24 | * This abstract class is intended to be subclassed and specialized to maintain |
| 25 | * a registry of observers of specific types and dispatch notifications to them. |
| 26 | * |
| 27 | * @param T The observer type. |
| 28 | */ |
| 29 | public abstract class Observable<T> { |
| 30 | /** |
| 31 | * The list of observers. An observer can be in the list at most |
| 32 | * once and will never be null. |
| 33 | */ |
| 34 | protected final ArrayList<T> mObservers = new ArrayList<T>(); |
| 35 | |
| 36 | /** |
| 37 | * Adds an observer to the list. The observer cannot be null and it must not already |
| 38 | * be registered. |
| 39 | * @param observer the observer to register |
| 40 | * @throws IllegalArgumentException the observer is null |
| 41 | * @throws IllegalStateException the observer is already registered |
| 42 | */ |
| 43 | public void registerObserver(T observer) { |
| 44 | if (observer == null) { |
| 45 | throw new IllegalArgumentException("The observer is null."); |
| 46 | } |
| 47 | synchronized(mObservers) { |
| 48 | if (mObservers.contains(observer)) { |
| 49 | throw new IllegalStateException("Observer " + observer + " is already registered."); |
| 50 | } |
| 51 | mObservers.add(observer); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Removes a previously registered observer. The observer must not be null and it |
| 57 | * must already have been registered. |
| 58 | * @param observer the observer to unregister |
| 59 | * @throws IllegalArgumentException the observer is null |
| 60 | * @throws IllegalStateException the observer is not yet registered |
| 61 | */ |
| 62 | public void unregisterObserver(T observer) { |
| 63 | if (observer == null) { |
| 64 | throw new IllegalArgumentException("The observer is null."); |
| 65 | } |
| 66 | synchronized(mObservers) { |
| 67 | int index = mObservers.indexOf(observer); |
| 68 | if (index == -1) { |
| 69 | throw new IllegalStateException("Observer " + observer + " was not registered."); |
| 70 | } |
| 71 | mObservers.remove(index); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Remove all registered observers. |
| 77 | */ |
| 78 | public void unregisterAll() { |
| 79 | synchronized(mObservers) { |
| 80 | mObservers.clear(); |
| 81 | } |
| 82 | } |
| 83 | } |