1 /* 2 * Copyright (C) 2010 Unreal Visualizer Authors 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 package nl.tudelft.goal.ut2004.visualizer.data; 18 19 import java.util.ArrayList; 20 21 import nl.tudelft.goal.ut2004.visualizer.events.DataUpdateListener; 22 23 import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server; 24 25 /** 26 * Abstract class for data that is gathered from the UnrealServer. 27 * 28 * @author Lennard de Rijk 29 * @author M.P. Korstanje 30 * 31 */ 32 public abstract class AbstractData { 33 34 private final ArrayList<DataUpdateListener> listeners; 35 36 public AbstractData() { 37 listeners = new ArrayList<DataUpdateListener>(); 38 } 39 40 /** 41 * Called when ever the server is updated. 42 * 43 * @param server 44 * the new server or null if the connection was lost. 45 */ 46 public abstract void serverChanged(IUT2004Server server); 47 48 /** 49 * Add the specified {@link DataUpdateListener} to the listeners for this {@link AbstractData} object. 50 * 51 * @param listener 52 * The listener that needs to be subscribed 53 */ 54 public void subscribe(DataUpdateListener listener) { 55 listeners.add(listener); 56 } 57 58 /** 59 * Removes the specified {@link DataUpdateListener} from the listeners to this {@link AbstractData} object. 60 * 61 * @param listener 62 * The listener that needs to be removed. 63 */ 64 public void unsubscribe(DataUpdateListener listener) { 65 listeners.remove(listener); 66 } 67 68 /** 69 * Notifies all the listeners that this {@link AbstractData} has been updated. 70 * 71 * @param type 72 * The Type of {@link DataUpdateEvent}. 73 */ 74 public void notifyListeners() { 75 for (DataUpdateListener listener : listeners) { 76 listener.update(this); 77 } 78 } 79 80 }