⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 observablesortedmap.java

📁 本程序是有IBM开发的一个基于数据表格的组件,里面有相关的例子和DOC,本站资料仅为大家学习之用
💻 JAVA
字号:

package com.ibm.j2x.util.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import com.ibm.j2x.event.CollectionEvent;
import com.ibm.j2x.event.CollectionListener;

/**
 * The ObservableSortedMap subclasses the TreeMap 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 ObservableSortedMap extends TreeMap implements ObservableCollection
{
	/** the observers of this collection */
	protected ArrayList listeners = new ArrayList();

	/**
	 * Constructs a new, empty map, sorted according to the keys' natural order. All keys inserted into the map must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a ClassCastException for any elements k1 and k2 in the map. If the user attempts to put a key into the map that violates this constraint (for example, the user attempts to put a string key into a map whose keys are integers), the put(Object key, Object value) call will throw a ClassCastException.
	 *
	 */
	public ObservableSortedMap() 
	{
		super();
	}

	/**
	 * Constructs a new, empty map, sorted according to the given comparator. All keys inserted into the map must be mutually comparable by the given comparator: comparator.compare(k1, k2) must not throw a ClassCastException for any keys k1 and k2 in the map. If the user attempts to put a key into the map that violates this constraint, the put(Object key, Object value) call will throw a ClassCastException.
	 * @param c the comparator that will be used to sort this map. A null value indicates that the keys' natural ordering should be used.
	 */
	public ObservableSortedMap(Comparator c) 
	{
		super(c);
	}

	/**
	 * Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order. All keys inserted into the new map must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a ClassCastException  for any elements k1 and k2 in the map. This method runs in n*log(n) time.
	 * @param m the map whose mappings are to be placed in this map.
	 */
	public ObservableSortedMap(Map m) 
	{
		super(m);
	}

	/**
	 * Constructs a new map containing the same mappings as the given SortedMap, sorted according to the same ordering. This method runs in linear time.
	 * @param m the sorted map whose mappings are to be placed in this map, and whose comparator is to be used to sort this map.
	 */
	public ObservableSortedMap(SortedMap 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 + -