omgraphichash.java

来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 388 行 · 第 1/2 页

JAVA
388
字号
/* * OMGraphicHash.java * * Created on August 18, 2006, 9:29 AM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package com.bbn.openmap.omGraphics;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.Map;/** * This is an OMGraphicsList cub-class with a Map interface. Access is backed by * a HashMap. Insertion and removal of OMGraphics is through the Map interface. * <p> * The add(OMGraphic), addOMGraphic(OMGraphic), remove(OMGraphic), and * removeOMGraphicAt(int) method are disabled and will throw a RuntimeException * if called. * <p> * This class is suited for use as the top OMGraphicList in an OMHandlerLayer * that has a large number of OMGraphic objects and needs to access those * objects using a unique key. * <p> *  * @see com.bbn.openmap.omGraphics.OMGraphicList * @author David J. Ward */public class OMGraphicHash extends OMGraphicList implements Map {    private HashMap graphicHash = new HashMap();    /**     * Creates a new instance of OMGraphicHash     */    public OMGraphicHash() {        super();    }    /**     * Construct an OMGraphicList with an initial capacity.     *      * @param initialCapacity the initial capacity of the list     */    public OMGraphicHash(int initialCapacity) {        super(initialCapacity);    };    /**     * Returns the value to which this map maps the specified key. Returns     * <tt>null</tt> if the map contains no mapping for this key. A return     * value of <tt>null</tt> does not <i>necessarily</i> indicate that the     * map contains no mapping for the key; it's also possible that the map     * explicitly maps the key to <tt>null</tt>. The <tt>containsKey</tt>     * operation may be used to distinguish these two cases.     *      * <p>     * More formally, if this map contains a mapping from a key <tt>k</tt> to     * a value <tt>v</tt> such that <tt>(key==null ? k==null :     * key.equals(k))</tt>,     * then this method returns <tt>v</tt>; otherwise it returns     * <tt>null</tt>. (There can be at most one such mapping.)     *      * @param key key whose associated value is to be returned.     * @return the value to which this map maps the specified key, or     *         <tt>null</tt> if the map contains no mapping for this key.     *      * @throws ClassCastException if the key is of an inappropriate type for     *         this map (optional).     * @throws NullPointerException if the key is <tt>null</tt> and this map     *         does not permit <tt>null</tt> keys (optional).     *      * @see #containsKey(Object)     */    public Object get(Object obj) {        return graphicHash.get(obj);    }    /**     * Returns <tt>true</tt> if this map contains a mapping for the specified     * key. More formally, returns <tt>true</tt> if and only if this map     * contains a mapping for a key <tt>k</tt> such that     * <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be at most     * one such mapping.)     *      * @param key key whose presence in this map is to be tested.     * @return <tt>true</tt> if this map contains a mapping for the specified     *         key.     *      * @throws ClassCastException if the key is of an inappropriate type for     *         this map (optional).     * @throws NullPointerException if the key is <tt>null</tt> and this map     *         does not permit <tt>null</tt> keys (optional).     */    public boolean containsKey(Object obj) {        return graphicHash.containsKey(obj);    }    /**     * Returns <tt>true</tt> if this map maps one or more keys to the     * specified value. More formally, returns <tt>true</tt> if and only if     * this map contains at least one mapping to a value <tt>v</tt> such that     * <tt>(value==null ? v==null : value.equals(v))</tt>. This operation     * will probably require time linear in the map size for most     * implementations of the <tt>Map</tt> interface.     *      * @param value value whose presence in this map is to be tested.     * @return <tt>true</tt> if this map maps one or more keys to the     *         specified value.     * @throws ClassCastException if the value is of an inappropriate type for     *         this map (optional).     * @throws NullPointerException if the value is <tt>null</tt> and this map     *         does not permit <tt>null</tt> values (optional).     */    public boolean containsValue(Object obj) {        return graphicHash.containsValue(obj);    }    /**     * Returns a set view of the mappings contained in this map. Each element in     * the returned set is a {@link Map.Entry}. The set is backed by the map,     * so changes to the map are reflected in the set, and vice-versa. If the     * map is modified while an iteration over the set is in progress (except     * through the iterator's own <tt>remove</tt> operation, or through the     * <tt>setValue</tt> operation on a map entry returned by the iterator)     * the results of the iteration are undefined. The set supports element     * removal, which removes the corresponding mapping from the map, via the     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>,     * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not support     * the <tt>add</tt> or <tt>addAll</tt> operations.     *      * @return a set view of the mappings contained in this map.     */    public java.util.Set entrySet() {        return graphicHash.entrySet();    }    /**     * Returns a set view of the keys contained in this map. The set is backed     * by the map, so changes to the map are reflected in the set, and     * vice-versa. If the map is modified while an iteration over the set is in     * progress (except through the iterator's own <tt>remove</tt> operation),     * the results of the iteration are undefined. The set supports element     * removal, which removes the corresponding mapping from the map, via the     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,     * <tt>removeAll</tt> <tt>retainAll</tt>, and <tt>clear</tt>     * operations. It does not support the add or <tt>addAll</tt> operations.     *      * @return a set view of the keys contained in this map.     */    public java.util.Set keySet() {        return graphicHash.keySet();    }    /**     * Associates the specified value with the specified key in this map     * (optional operation). If the map previously contained a mapping for this     * key, the old value is replaced by the specified value. (A map <tt>m</tt>     * is said to contain a mapping for a key <tt>k</tt> if and only if     * {@link #containsKey(Object) m.containsKey(k)} would return <tt>true</tt>.))     *      * @param key key with which the specified value is to be associated.     * @param value value to be associated with the specified key.     * @return previous value associated with specified key, or <tt>null</tt>     *         if there was no mapping for key. A <tt>null</tt> return can     *         also indicate that the map previously associated <tt>null</tt>     *         with the specified key, if the implementation supports     *         <tt>null</tt> values.     *      * @throws UnsupportedOperationException if the <tt>put</tt> operation is     *         not supported by this map.     * @throws ClassCastException if the class of the specified key or value     *         prevents it from being stored in this map.     * @throws IllegalArgumentException if some aspect of this key or value     *         prevents it from being stored in this map.     * @throws NullPointerException if this map does not permit <tt>null</tt>     *         keys or values, and the specified key or value is <tt>null</tt>.     */    public Object put(Object key, Object graphic) {        if (!(graphic instanceof OMGraphic)) {            throw new ClassCastException("Value is not an OMGraphic");        }        // first remove the OMGraphic from the list        // Don't allow duplicate graphics in hash, that is multiple        // key resolving to a single graphic. This would be very bad.        if (graphicHash.containsValue(graphic)) {            // now we know that we have this graphic in the            // hash, now lets find the associated key.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?