home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / util / Map.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  13.5 KB  |  349 lines

  1. /*
  2.  * @(#)Map.java    1.22 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.util;
  16.  
  17. /**
  18.  * An object that maps keys to values.  A Map cannot contain duplicate keys;
  19.  * each key can map to at most one value.
  20.  * <p>
  21.  * This interface takes the place of Dictionary, which was a totally abstract
  22.  * class rather than an interface.  This Interface is implemented by HashMap,
  23.  * ArrayMap, TreeMap and Hashtable.
  24.  * <p>
  25.  * The Map interface provides three <em>Collection views</em>, which allow a
  26.  * Map's contents to be viewed as a Set of keys, Collection of values, or Set
  27.  * of key-value mappings.  The <em>order</em> of a Map is defined as the order
  28.  * in which the iterators on the Map's Collection views return their elements.
  29.  * Some Map implementations, like TreeMap and ArrayMap, make specific
  30.  * guarantees as to their order; others, like HashMap, do not.
  31.  * <p>
  32.  * All general-purpose Map implementation classes should provide two
  33.  * "standard" constructors: A void (no arguments) constructor which creates
  34.  * an empty Map, and a constructor with a single argument of type Map,
  35.  * which creates a new Map with the same key-value mappings as its
  36.  * argument.  In effect, the latter constructor allows the user to copy any
  37.  * Map, producing an equivalent Map of the desired class.  There is no way
  38.  * to enforce this recommendation (as interfaces cannot contain constructors)
  39.  * but all of the general-purpose Map implementations in the JDK comply.
  40.  *
  41.  * @author  Josh Bloch
  42.  * @version 1.22 03/18/98
  43.  * @see HashMap
  44.  * @see ArrayMap
  45.  * @see TreeMap
  46.  * @see Hashtable
  47.  * @see Collection
  48.  * @see Set
  49.  * @since JDK1.2
  50.  */
  51. public interface Map {
  52.     // Query Operations
  53.  
  54.     /**
  55.      * Returns the number of key-value mappings in this Map.
  56.      *
  57.      * @since JDK1.2
  58.      */
  59.     int size();
  60.  
  61.     /**
  62.      * Returns true if this Map contains no key-value mappings.
  63.      *
  64.      * @since JDK1.2
  65.      */
  66.     boolean isEmpty();
  67.  
  68.     /**
  69.      * Returns true if this Map contains a mapping for the specified key.
  70.      *
  71.      * @param key key whose presence in this Map is to be tested.
  72.      * @exception ClassCastException key is of an inappropriate type for
  73.      *           this Map.
  74.      * @exception NullPointerException key is null and this Map does not
  75.      *          not permit null keys.
  76.      * @since JDK1.2
  77.      */
  78.     boolean containsKey(Object key);
  79.  
  80.     /**
  81.      * Returns true if this Map maps one or more keys to the specified value.
  82.      * More formally, returns true if and only if this Map contains at
  83.      * least one mapping to a value <code>v</code> such that
  84.      * <code>(value==null ? v==null : value.equals(v))</code>.
  85.      * This operation will probably require time linear in the Map size for
  86.      * most implementations of Map.
  87.      *
  88.      * @param value value whose presence in this Map is to be tested.
  89.      * @since JDK1.2
  90.      */
  91.     boolean containsValue(Object value);
  92.  
  93.     /**
  94.      * Returns the value to which this Map maps the specified key.
  95.      * Returns null if the Map contains no mapping for this key.  A return
  96.      * value of null does not <em>necessarily</em> indicate that the Map
  97.      * contains no mapping for the key; it's also possible that the Map
  98.      * explicitly maps the key to null.  The containsKey operation may be
  99.      * used to distinguish these two cases. 
  100.      *
  101.      * @param key key whose associated value is to be returned.
  102.      * @exception ClassCastException key is of an inappropriate type for
  103.      *           this Map.
  104.      * @exception NullPointerException key is null and this Map does not
  105.      *          not permit null keys.
  106.      * @see #containsKey(Object)
  107.      * @since JDK1.2
  108.      */
  109.     Object get(Object key);
  110.  
  111.     // Modification Operations
  112.  
  113.     /**
  114.      * Associates the specified value with the specified key in this Map
  115.      * (optional operation).  If the Map previously contained a mapping for
  116.      * this key, the old value is replaced.
  117.      *
  118.      * @param key key with which the specified value is to be associated.
  119.      * @param value value to be associated with the specified key.
  120.      * @return previous value associated with specified key, or null if there
  121.      *           was no mapping for key.  A null return can also indicate that
  122.      *           the Map previously associated null with the specified key,
  123.      *           if the implementation supports null values.
  124.      * @exception UnsupportedOperationException put operation is not
  125.      *              supported by this Map.
  126.      * @exception ClassCastException class of the specified key or value
  127.      *               prevents it from being stored in this Map.
  128.      * @exception IllegalArgumentException some aspect of this key or value
  129.      *              prevents it from being stored in this Map.
  130.      * @exception NullPointerException this Map does not permit null keys
  131.      *          or values, and the specified key or value is null.
  132.      * @since JDK1.2
  133.      */
  134.     Object put(Object key, Object value);
  135.  
  136.     /**
  137.      * Removes the mapping for this key from this Map if present (optional
  138.      * operation).
  139.      *
  140.      * @param key key whose mapping is to be removed from the Map.
  141.      * @return previous value associated with specified key, or null if there
  142.      *           was no mapping for key.  A null return can also indicate that
  143.      *           the Map previously associated null with the specified key,
  144.      *           if the implementation supports null values.
  145.      * @exception UnsupportedOperationException remove is not supported
  146.      *           by this Map.
  147.      * @since JDK1.2
  148.      */
  149.     Object remove(Object key);
  150.  
  151.  
  152.     // Bulk Operations
  153.  
  154.     /**
  155.      * Copies all of the mappings from the specified Map to this Map
  156.      * (optional operation).  These mappings will replace any mappings that
  157.      * this Map had for any of the keys currently in the specified Map.
  158.      *
  159.      * @param t Mappings to be stored in this Map.
  160.      * @exception UnsupportedOperationException putAll is not supported
  161.      *           by this Map.
  162.      * @exception ClassCastException class of a key or value in the specified
  163.      *               Map prevents it from being stored in this Map.
  164.      * @exception IllegalArgumentException some aspect of a key or value in the
  165.      *              specified Map prevents it from being stored in this Map.
  166.      * @exception NullPointerException this Map does not permit null keys
  167.      *          or values, and the specified key or value is null.
  168.      * @since JDK1.2
  169.      */
  170.     void putAll(Map t);
  171.  
  172.     /**
  173.      * Removes all mappings from this Map (optional operation).
  174.      *
  175.      * @exception UnsupportedOperationException clear is not supported
  176.      *           by this Map.
  177.      * @since JDK1.2
  178.      */
  179.     void clear();
  180.  
  181.  
  182.     // Views
  183.  
  184.     /**
  185.      * Returns a Set view of the keys contained in this Map.  The Set is
  186.      * backed by the Map, so changes to the Map are reflected in the Set,
  187.      * and vice-versa.  If the Map is modified while while an iteration over
  188.      * the Set is in progress, the results of the iteration are undefined.
  189.      * The Set supports element removal, which removes the corresponding
  190.      * mapping from the Map, via the Iterator.remove, Set.remove, removeAll
  191.      * retainAll, and clear operations.  It does not support the add or
  192.      * addAll operations.
  193.      *
  194.      * @since JDK1.2
  195.      */
  196.     public Set keySet();
  197.  
  198.     /**
  199.      * Returns a Collection view of the values contained in this Map.  The
  200.      * Collection is backed by the Map, so changes to the Map are
  201.      * reflected in the Collection, and vice-versa.  If the Map is
  202.      * modified while while an iteration over the Collection is in progress,
  203.      * the results of the iteration are undefined.  The Collection supports
  204.      * element removal, which removes the corresponding mapping from the
  205.      * Map, via the Iterator.remove, Collection.remove, removeAll,
  206.      * retainAll and clear operations.  It does not support the add or
  207.      * addAll operations.
  208.      *
  209.      * @since JDK1.2
  210.      */
  211.     public Collection values();
  212.  
  213.     /**
  214.      * Returns a Set view of the mappings contained in this Map.  Each element
  215.      * in the returned Set is a Map.Entry.  The Set is backed by the Map, so
  216.      * changes to the Map are reflected in the Set, and vice-versa.  If the
  217.      * Map is modified while while an iteration over the Set is in progress,
  218.      * the results of the iteration are undefined.  The Set supports element
  219.      * removal, which removes the corresponding mapping from the Map, via the
  220.      * Iterator.remove, Set.remove, removeAll, retainAll and clear operations.
  221.      * It does not support the add or addAll operations.
  222.      *
  223.      * @since JDK1.2
  224.      */
  225.     public Set entries();
  226.  
  227.     /**
  228.      * A Map entry (key-value pair).  The Map.entries method returns a
  229.      * Collection-view of the Map, whose elements are of this class.  The
  230.      * <em>only</em> way to obtain a reference to a Map.Entry is from the
  231.      * iterator of this Collection-view.  These Map.Entry objects are valid
  232.      * <em>only</em> for the duration of the iteration; more formally, the
  233.      * behavior of a Map.Entry is undefined if the backing Map has been
  234.      * modified after the Entry was returned by the Iterator, except through
  235.      * the Iterator's own remove operation, or through the setValue operation
  236.      * of Map.Entries returned by the Iterator.
  237.      *
  238.      * @see Map#entries()
  239.      * @since JDK1.2
  240.      */
  241.     public interface Entry {
  242.         /**
  243.      * Returns the key corresponding to this Entry.
  244.      *
  245.      * @since JDK1.2
  246.      */
  247.     Object getKey();
  248.  
  249.         /**
  250.      * Returns the value corresponding to this Entry.  If the mapping
  251.      * has been removed from the backing Map (by the Iterator's
  252.      * remove operation), the results of this call are undefined.
  253.      *
  254.      * @since JDK1.2
  255.      */
  256.     Object getValue();
  257.  
  258.         /**
  259.      * Replaces the value corresponding to this Entry with the specified
  260.      * value (optional operation).  (Writes through to the Map.)  The
  261.      * behavior of this call is undefined if the mapping has already been
  262.      * removed from the Map (by the Iterator's remove operation).
  263.      *
  264.      * @param New value to be stored in this Entry.
  265.      * @return old value corresponding to the Entry.
  266.      * @exception UnsupportedOperationException put operation is not
  267.      *          supported by the backing Map.
  268.      * @exception ClassCastException class of the specified value
  269.      *           prevents it from being stored in the backing Map.
  270.      * @exception IllegalArgumentException some aspect of this value
  271.      *          prevents it from being stored in the backing Map.
  272.      * @exception NullPointerException the backing Map does not permit
  273.      *          null values, and the specified value is null.
  274.      *
  275.      * @since JDK1.2
  276.      */
  277.     Object setValue(Object value);
  278.  
  279.     /**
  280.      * Compares the specified Object with this Entry for equality.
  281.      * Returns true if the given object is also a Map.Entry and the two
  282.      * Entries represent the same mapping.  More formally, two
  283.      * Entries <code>e1</code> and <code>e2</code> represent 
  284.      * the same mapping if <code> (e1.getKey()==null ? e2.getKey()==null :
  285.      * e1.getKey().equals(e2.getKey())) && (e1.getValue()==null ?
  286.      * e2.getValue()==null : e1.getValue().equals(e2.getValue())) </code>.
  287.      * This ensures that the equals method works properly across different
  288.      * implementations of the Map.Entry interface.
  289.      *
  290.      * @param o Object to be compared for equality with this Map.Entry.
  291.      * @return true if the specified Object is equal to this Map.Entry.
  292.      * @since JDK1.2
  293.      */
  294.     boolean equals(Object o);
  295.  
  296.     /**
  297.      * Returns the hash code value for this Map.Entry.  The hash code
  298.      * of a Map.Entry <code>e</code> is defined to be: <code>
  299.      * (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
  300.      * (e.getValue()==null ? 0 : e.getValue().hashCode()) </code>
  301.      * This ensures that <code>e1.equals(e2)</code> implies that
  302.      * <code>e1.hashCode()==e2.hashCode()</code> for any two Entries
  303.      * <code>e1</code> and <code>e2</code>, as required by the general
  304.      * contract of Object.hashCode.
  305.      *
  306.      * @see Object#hashCode()
  307.      * @see Object#equals(Object)
  308.      * @see Map#equals(Object)
  309.      * @since JDK1.2
  310.      */
  311.     int hashCode();
  312.     }
  313.  
  314.     // Comparison and hashing
  315.  
  316.     /**
  317.      * Compares the specified Object with this Map for equality.
  318.      * Returns true if the given object is also a Map and the two
  319.      * Maps represent the same mappings.  More formally, two
  320.      * Maps <code>t1</code> and <code>t2</code> represent the same mappings
  321.      * if <code>t1.keySet().equals(t2.keySet())</code> and for every 
  322.      * key <code>k</code> in <code>t1.keySet()</code>, <code>
  323.      * (t1.get(k)==null ? t2.get(k)==null : t1.get(k).equals(t2.get(k)))
  324.      * </code>.  This ensures that the equals method works properly across
  325.      * different implementations of the Map interface.
  326.      *
  327.      * @param o Object to be compared for equality with this Map.
  328.      * @return true if the specified Object is equal to this Map.
  329.      * @since JDK1.2
  330.      */
  331.     boolean equals(Object o);
  332.  
  333.     /**
  334.      * Returns the hash code value for this Map.  The hash code of a Map
  335.      * is defined to be the sum of the hashCodes of each Entry in the Map's
  336.      * entries view.  This ensures that <code>t1.equals(t2)</code> implies
  337.      * that <code>t1.hashCode()==t2.hashCode()</code> for any two Maps
  338.      * <code>t1</code> and <code>t2</code>, as required by the general
  339.      * contract of Object.hashCode.
  340.      *
  341.      * @see Entry#hashCode()
  342.      * @see Object#hashCode()
  343.      * @see Object#equals(Object)
  344.      * @see Map#equals()
  345.      * @since JDK1.2
  346.      */
  347.     int hashCode();
  348. }
  349.