1 /* 2 * Copyright (C) 2007-2008 Esmertec AG. 3 * Copyright (C) 2007-2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mms.transaction; 19 20 import java.util.ArrayList; 21 import java.util.Iterator; 22 23 24 /** 25 * An interface to represent the state of an observable Transaction. 26 */ 27 public abstract class Observable { 28 private final ArrayList<Observer> mObservers; 29 private Iterator<Observer> mIterator; 30 31 public Observable() { 32 mObservers = new ArrayList<Observer>(); 33 } 34 35 /** 36 * This method is implemented by the observable to represent its 37 * current state. 38 * 39 * @return A TransactionState object. 40 */ 41 abstract public TransactionState getState(); 42 43 /** 44 * Attach an observer to this object. 45 * 46 * @param observer The observer object to be attached to. 47 */ 48 public void attach(Observer observer) { 49 mObservers.add(observer); 50 } 51 52 /** 53 * Detach an observer from this object. 54 * 55 * @param observer The observer object to be detached from. 56 */ 57 public void detach(Observer observer) { 58 if (mIterator != null) { 59 mIterator.remove(); 60 } else { 61 mObservers.remove(observer); 62 } 63 } 64 65 /** 66 * Notify all observers that a status change has occurred. 67 */ 68 public void notifyObservers() { 69 mIterator = mObservers.iterator(); 70 try { 71 while (mIterator.hasNext()) { 72 mIterator.next().update(this); 73 } 74 } finally { 75 mIterator = null; 76 } 77 } 78 } 79