Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

HashMap.java

00001 package ca.mcgill.sable.util;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * SableUtil, a clean room implementation of the Collection API.     *
00005  * Copyright (C) 1997, 1998, 1999 Raja Vallee-Rai                    *
00006  * (kor@sable.mcgill.ca).  All rights reserved.                      *
00007  *                                                                   *
00008  * This work was done as a project of the Sable Research Group,      *
00009  * School of Computer Science, McGill University, Canada             *
00010  * (http://www.sable.mcgill.ca/).  It is understood that any         *
00011  * modification not identified as such is not covered by the         *
00012  * preceding statement.                                              *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this library; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * To submit a bug report, send a comment, or get the latest news on *
00030  * this project and other Sable Research Group projects, please      *
00031  * visit the web site: http://www.sable.mcgill.ca/                   *
00032  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00033 
00034 /*
00035  Reference Version
00036  -----------------
00037  This is the latest official version on which this file is based.
00038  The reference version is: $SableUtilVersion: 1.11 $
00039 
00040  Change History
00041  --------------
00042  A) Notes:
00043 
00044  Please use the following template.  Most recent changes should
00045  appear at the top of the list.
00046 
00047  - Modified on [date (March 1, 1900)] by [name]. [(*) if appropriate]
00048    [description of modification].
00049 
00050  Any Modification flagged with "(*)" was done as a project of the
00051  Sable Research Group, School of Computer Science,
00052  McGill University, Canada (http://www.sable.mcgill.ca/).
00053 
00054  You should add your copyright, using the following template, at
00055  the top of this file, along with other copyrights.
00056 
00057  *                                                                   *
00058  * Modifications by [name] are                                       *
00059  * Copyright (C) [year(s)] [your name (or company)].  All rights     *
00060  * reserved.                                                         *
00061  *                                                                   *
00062 
00063  B) Changes:
00064 
00065  - Modified on January 20, 1999 by Raja Vallee-Rai (rvalleerai@sable.mcgill.ca). (*)
00066    Changed the entries implementation to be linear.
00067 
00068 
00069  - Modified on July 21, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*)
00070    Renamed this class to HashMap to avoid conflicts.
00071 
00072  - Modified on June 15, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*)
00073    First release of this file.
00074 */
00075 
00076 public class HashMap extends AbstractMap
00077 {
00078     java.util.Hashtable table;
00079 
00080     private class HashEntry implements Map.Entry
00081     {
00082         private Object key;
00083 
00084         public HashEntry(Object key)
00085         {
00086             this.key = key;
00087         }
00088 
00089         public boolean equals(Object obj)
00090         {
00091             if(!(obj instanceof Map.Entry))
00092                 return false;
00093             else {
00094                 Map.Entry e = (Map.Entry) obj;
00095 
00096                 return e.getKey().equals(key) && getValue().equals(e.getValue());
00097             }
00098         }
00099 
00100         public Object getKey()
00101         {
00102             return key;
00103         }
00104 
00105         public Object getValue()
00106         {
00107             return get(key);
00108         }
00109 
00110         public Object setValue(Object obj)
00111         {
00112             return put(key, obj);
00113         }
00114 
00115         public int hashCode()
00116         {
00117             return (getKey()==null ? 0 : getKey().hashCode()) ^ (getValue()==null ? 0 :
00118                 getValue().hashCode());
00119         }
00120 
00121         public String toString()
00122         {
00123             return key.toString() + " -> " + get(key);
00124         }
00125     }
00126 
00127     public HashMap()
00128     {
00129         table = new java.util.Hashtable();
00130     }
00131     public HashMap(int initialCapacity)
00132     {
00133         table = new java.util.Hashtable(initialCapacity);
00134     }
00135     public HashMap(int initialCapacity, float loadFactor)
00136     {
00137         if(initialCapacity <= 0)
00138             throw new RuntimeException("initialCapacity is " + initialCapacity);
00139 
00140         if(loadFactor <= 0)
00141             throw new RuntimeException("loadFactor is " + initialCapacity);
00142 
00143 
00144         table = new java.util.Hashtable(initialCapacity, loadFactor);
00145     }
00146     public void clear()
00147     {
00148         table.clear();
00149     }
00150     public boolean contains(Object value)
00151     {
00152         return table.contains(value);
00153     }
00154     public boolean containsKey(Object key)
00155     {
00156         if(key == null)
00157             throw new RuntimeException("Hey hey!");
00158 
00159         return table.containsKey(key);
00160     }
00161     /**
00162      * This implementation does not correspond exactly to the 1.2 definition.  The collection
00163      * which is returned is not backed by the def'n.
00164      */
00165 
00166     public Collection entries()
00167     {
00168         java.util.Enumeration entries = table.keys();
00169         /*Set keySet = new VectorSet();
00170 
00171         while(entries.hasMoreElements())
00172             keySet.add(new HashEntry(entries.nextElement()));
00173           */
00174 
00175         List keyList = new ArrayList();
00176 
00177         while(entries.hasMoreElements())
00178             keyList.add(new HashEntry(entries.nextElement()));
00179 
00180         return keyList;
00181     }
00182     public Object get(Object entry)
00183     {
00184         return table.get(entry);
00185     }
00186     public boolean isEmpty()
00187     {
00188         return table.size() == 0;
00189     }
00190     public Set keySet()
00191     {
00192         java.util.Enumeration keys = table.keys();
00193         Set keySet = new HashSet();
00194 
00195         while(keys.hasMoreElements())
00196             keySet.add(keys.nextElement());
00197 
00198         return keySet;
00199     }
00200     public Object put(Object key, Object value)
00201     {
00202         return table.put(key, value);
00203 
00204     /*
00205         Object previousEntry = null;
00206 
00207         if(table.containsKey(key))
00208             previousEntry = table.get(key);
00209 
00210         table.put(key, value);
00211 
00212         return previousEntry; */
00213     }
00214     public Object remove(Object obj)
00215     {
00216         return table.remove(obj);
00217     }
00218     public int size()
00219     {
00220         return table.size();
00221     }
00222 }

Generated at Thu Feb 7 06:46:09 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001