📄 observablemap.java
字号:
package com.ibm.j2x.util.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.ibm.j2x.event.CollectionEvent;
import com.ibm.j2x.event.CollectionListener;
/**
* The ObservableMap subclasses the HashMap and
* overrides all the functions that change the internally
* stored collection. In the overridden function, it simply
* calls the super class's function and fires a
* CollectionEvent to let observers know the collection
* has changed.
* @author MAbernethy
*/
public class ObservableMap extends HashMap implements ObservableCollection
{
/** the observers of this collection */
protected ArrayList listeners = new ArrayList();
/**
* Constructs an empty HashMap with the specified initial capacity and load factor.
* @param initialCapacity The initial capacity.
* @param loadFactor The load factor.
*/
public ObservableMap(int initialCapacity, float loadFactor)
{
super(initialCapacity, loadFactor);
}
/**
* Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).
* @param initialCapacity the initial capacity.
*/
public ObservableMap(int initialCapacity)
{
super(initialCapacity);
}
/**
* Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
*/
public ObservableMap()
{
super();
}
/**
* Constructs a new HashMap with the same mappings as the specified Map. The HashMap is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified Map.
* @param m the map whose mappings are to be placed in this map.
*/
public ObservableMap(Map m)
{
super(m);
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public Object put(Object key, Object value)
{
Object o = super.put(key, value);
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.ADD));
return o;
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public Object remove(Object key)
{
Object o = super.remove(key);
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.REMOVE));
return o;
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public void putAll(Map t)
{
super.putAll(t);
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.ADD));
}
/**
* Calls the super class's corresponding method
* and fires a CollectionEvent.
*/
public void clear()
{
super.clear();
fireCollectionEvent(new CollectionEvent(this, CollectionEvent.REMOVE));
}
/**
* Adds a listener to this collection.
* @param l the CollectionListener
*/
public void addCollectionListener(CollectionListener l)
{
if ( ! listeners.contains(l))
listeners.add(l);
}
/**
* Removes the listener from this collection.
* @param l the CollectionListener
*/
public void removeCollectionListener(CollectionListener l)
{
listeners.remove(l);
}
/**
* Fires a CollectionEvent to all listeners.
* @param e the CollectionEvent
*/
protected void fireCollectionEvent(CollectionEvent e)
{
for (Iterator i=listeners.iterator(); i.hasNext();)
((CollectionListener)i.next()).collectionChanged(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -