001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.platform.attribute;
005    
006    import java.util.Collection;
007    import java.util.Vector;
008    
009    /**
010     * Default implementation for the NotifiableAttributeSet
011     *
012     * @see graphlab.platform.attribute.NotifiableAttributeSet
013     * @author Azin Azadi, Reza Mohammadi, Rouzbeh Ebrahimi
014     */
015    public class NotifiableAttributeSetImpl extends AttributeSetImpl implements NotifiableAttributeSet {
016    
017        Vector<AttributeListener> globalListeners = new Vector<AttributeListener>();
018    
019    
020        public void put(String name, Object value) {
021            if (name == null) {
022                throw new RuntimeException("key=null" + value);
023            }
024            Object old = atr.put(name, value);
025    //        Collection<AttributeListener> listeners = notifiableAttributeSet.getAttributeListeners(name);
026            fireAttributeChange(getAttributeListeners(), name, old, value);
027    
028        }
029    
030        public Object get(String name) {
031            return super.get(name);
032        }
033    
034        public void addAttributeListener(AttributeListener attributeListener) {
035            globalListeners.add(attributeListener);
036        }
037    
038        public Collection<AttributeListener> getAttributeListeners() {
039            return globalListeners;
040        }
041    
042        public void removeAttributeListener(AttributeListener x) {
043            globalListeners.remove(x);
044        }
045    
046        public void fireAttributeChange(Collection<AttributeListener> listeners, String name, Object oldVal, Object newVal) {
047            if (listeners != null) {
048                for (AttributeListener l : listeners) {
049                    l.attributeUpdated(name, oldVal, newVal);
050                }
051            }
052        }
053    }