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

AbstractMap.java

00001 package ca.mcgill.sable.util;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * SableUtil, a clean room implementation of the Collection API.     *
00005  * Copyright (C) 1997, 1998 Etienne Gagnon (gagnon@sable.mcgill.ca). *
00006  * 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 June 7, 1998 by Etienne Gagnon (gagnon@sable.mcgill.ca). (*)
00066    Changed the license.
00067 */
00068 
00069 import ca.mcgill.sable.util.Map.Entry;
00070 
00071 public abstract class AbstractMap implements Map
00072 {
00073     private Collection values;
00074     private Set keys;
00075 
00076     private class ValueCollection extends AbstractCollection
00077     {
00078         public int size()
00079         {
00080             return entries().size();
00081         }
00082 
00083         public Iterator iterator()
00084         {
00085             return new ValueIterator();
00086         }
00087     }
00088 
00089     private class ValueIterator implements Iterator
00090     {
00091         private Iterator iterator = entries().iterator();
00092 
00093         public boolean hasNext()
00094         {
00095             return iterator.hasNext();
00096         }
00097 
00098         public Object next()
00099         {
00100             return ((Entry) iterator.next()).getValue();
00101         }
00102 
00103         public void remove()
00104         {
00105             iterator.remove();
00106         }
00107     }
00108 
00109     private class KeySet extends AbstractSet
00110     {
00111         public int size()
00112         {
00113             return entries().size();
00114         }
00115 
00116         public Iterator iterator()
00117         {
00118             return new KeyIterator();
00119         }
00120     }
00121 
00122     private class KeyIterator implements Iterator
00123     {
00124         private Iterator iterator = entries().iterator();
00125 
00126         public boolean hasNext()
00127         {
00128             return iterator.hasNext();
00129         }
00130 
00131         public Object next()
00132         {
00133             return ((Entry) iterator.next()).getKey();
00134         }
00135 
00136         public void remove()
00137         {
00138             iterator.remove();
00139         }
00140     }
00141 
00142     public static abstract class AbstractEntry implements Entry
00143     {
00144         public boolean equals(Object o)
00145         {
00146             if(o == this)
00147             {
00148                 return true;
00149             }
00150 
00151             if(!(o instanceof Entry))
00152             {
00153                 return false;
00154             }
00155 
00156             Entry e = (Entry) o;
00157             Object k = getKey();
00158             Object v = getValue();
00159 
00160             if((k == null ? e.getKey() == null : k.equals(e.getKey())) &&
00161                 (v == null ? e.getValue() == null : v.equals(e.getValue())))
00162             {
00163                 return true;
00164             }
00165 
00166             return false;
00167         }
00168 
00169         public int hashCode()
00170         {
00171             Object k = getKey();
00172             Object v = getValue();
00173 
00174             return (k == null ? 0 : k.hashCode()) ^
00175                 (v == null ? 0 : v.hashCode());
00176         }
00177     }
00178     public void clear()
00179     {
00180         entries().clear();
00181     }
00182     public boolean containsKey(Object key)
00183     {
00184         for(Iterator i = entries().iterator(); i.hasNext();)
00185         {
00186             Object k = ((Entry) i.next()).getKey();
00187 
00188             if(key==null ? k==null : key.equals(k))
00189             {
00190                 return true;
00191             }
00192         }
00193 
00194         return false;
00195     }
00196     public boolean containsValue(Object value)
00197     {
00198         for(Iterator i = entries().iterator(); i.hasNext();)
00199         {
00200             Object v = ((Entry) i.next()).getValue();
00201 
00202             if(value==null ? v==null : value.equals(v))
00203             {
00204                 return true;
00205             }
00206         }
00207 
00208         return false;
00209     }
00210     public boolean equals(Object o)
00211     {
00212         if(o == this)
00213         {
00214             return true;
00215         }
00216 
00217         if(!(o instanceof Map))
00218         {
00219             return false;
00220         }
00221 
00222         Map m = (Map) o;
00223 
00224         if(m.size() != size())
00225         {
00226             return false;
00227         }
00228 
00229         for(Iterator i = entries().iterator(); i.hasNext();)
00230         {
00231             Entry e = (Entry) i.next();
00232             Object k = e.getKey();
00233 
00234             if(!m.containsKey(k))
00235             {
00236                 return false;
00237             }
00238 
00239             Object v = e.getValue();
00240 
00241             if(!(v==null ? m.get(k)==null : v.equals(m.get(k))))
00242             {
00243                 return false;
00244             }
00245         }
00246 
00247         return true;
00248     }
00249     public Object get(Object key)
00250     {
00251         for(Iterator i = entries().iterator(); i.hasNext();)
00252         {
00253             Entry e = (Entry) i.next();
00254 
00255             if(key==null ? e.getKey()==null : key.equals(e.getKey()))
00256             {
00257                 return e.getValue();
00258             }
00259         }
00260 
00261         return null;
00262     }
00263     public int hashCode()
00264     {
00265         int hashCode = 0;
00266 
00267         for(Iterator i = entries().iterator(); i.hasNext();)
00268         {
00269             hashCode += i.next().hashCode();
00270         }
00271 
00272         return hashCode;
00273     }
00274     public boolean isEmpty()
00275     {
00276         return size() == 0;
00277     }
00278     public Set keySet()
00279     {
00280         if(keys == null)
00281         {
00282             keys = new KeySet();
00283         }
00284 
00285         return keys;
00286     }
00287     public Object put(Object key, Object value)
00288     {
00289         throw new UnsupportedOperationException();
00290     }
00291     public void putAll(Map t)
00292     {
00293         for(Iterator i = t.entries().iterator(); i.hasNext();)
00294         {
00295             Entry e = (Entry) i.next();
00296 
00297             put(e.getKey(), e.getValue());
00298         }
00299     }
00300     public Object remove(Object key)
00301     {
00302         for(Iterator i = entries().iterator(); i.hasNext();)
00303         {
00304             Entry e = (Entry) i.next();
00305 
00306             if(key==null ? e.getKey()==null : key.equals(e.getKey()))
00307             {
00308                 Object v = e.getValue();
00309                 i.remove();
00310                 return v;
00311             }
00312         }
00313 
00314         return null;
00315     }
00316     public int size()
00317     {
00318         return entries().size();
00319     }
00320     public String toString()
00321     {
00322         StringBuffer s = new StringBuffer();
00323         boolean first = true;
00324 
00325         s.append("{");
00326         for(Iterator i = entries().iterator(); i.hasNext();)
00327         {
00328             Entry e = (Entry) i.next();
00329 
00330             if(!first)
00331             {
00332                 s.append(",");
00333             }
00334             else
00335             {
00336                 first = false;
00337             }
00338 
00339             s.append("(");
00340             s.append(e.getKey());
00341             s.append(",");
00342             s.append(e.getValue());
00343             s.append(")");
00344         }
00345         s.append("}");
00346 
00347         return s.toString();
00348     }
00349     public Collection values()
00350     {
00351         if(values == null)
00352         {
00353             values = new ValueCollection();
00354         }
00355 
00356         return values;
00357     }
00358 }

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