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

AbstractCollection.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  * Modifications by Raja Vallee-Rai                                  *
00009  * Copyright (C) 1998 Raja Vallee-Rai.  All rights reserved.         *
00010  *                                                                   *
00011  * This work was done as a project of the Sable Research Group,      *
00012  * School of Computer Science, McGill University, Canada             *
00013  * (http://www.sable.mcgill.ca/).  It is understood that any         *
00014  * modification not identified as such is not covered by the         *
00015  * preceding statement.                                              *
00016  *                                                                   *
00017  * This work is free software; you can redistribute it and/or        *
00018  * modify it under the terms of the GNU Library General Public       *
00019  * License as published by the Free Software Foundation; either      *
00020  * version 2 of the License, or (at your option) any later version.  *
00021  *                                                                   *
00022  * This work is distributed in the hope that it will be useful,      *
00023  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00025  * Library General Public License for more details.                  *
00026  *                                                                   *
00027  * You should have received a copy of the GNU Library General Public *
00028  * License along with this library; if not, write to the             *
00029  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00030  * Boston, MA  02111-1307, USA.                                      *
00031  *                                                                   *
00032  * To submit a bug report, send a comment, or get the latest news on *
00033  * this project and other Sable Research Group projects, please      *
00034  * visit the web site: http://www.sable.mcgill.ca/                   *
00035  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00036 
00037 /*
00038  Reference Version
00039  -----------------
00040  This is the latest official version on which this file is based.
00041  The reference version is: $SableUtilVersion: 1.11 $
00042 
00043  Change History
00044  --------------
00045  A) Notes:
00046 
00047  Please use the following template.  Most recent changes should
00048  appear at the top of the list.
00049 
00050  - Modified on [date (March 1, 1900)] by [name]. [(*) if appropriate]
00051    [description of modification].
00052 
00053  Any Modification flagged with "(*)" was done as a project of the
00054  Sable Research Group, School of Computer Science,
00055  McGill University, Canada (http://www.sable.mcgill.ca/).
00056 
00057  You should add your copyright, using the following template, at
00058  the top of this file, along with other copyrights.
00059 
00060  *                                                                   *
00061  * Modifications by [name] are                                       *
00062  * Copyright (C) [year(s)] [your name (or company)].  All rights     *
00063  * reserved.                                                         *
00064  *                                                                   *
00065 
00066  B) Changes:
00067 
00068  - Modified on July 23, 1998 by Etienne Gagnon (gagnon@sable.mcgill.ca). (*)
00069    Added toArray(Object[]).
00070 
00071  - Modified on June 15, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*)
00072    Fixed a bug with the remove() method.  Was missing a next().
00073 
00074  - Modified on June 7, 1998 by Etienne Gagnon (gagnon@sable.mcgill.ca). (*)
00075    Changed the license.
00076 
00077 */
00078 
00079 public abstract class AbstractCollection implements Collection
00080 {
00081     public boolean add(Object o)
00082     {
00083         throw new UnsupportedOperationException();
00084     }
00085     public boolean addAll(Collection c)
00086     {
00087         boolean modified = false;
00088 
00089         for(Iterator i = c.iterator(); i.hasNext();)
00090         {
00091             modified |= add(i.next());
00092         }
00093 
00094         return modified;
00095     }
00096     public void clear()
00097     {
00098         for(Iterator i = iterator(); i.hasNext();)
00099         {
00100             i.next();
00101             i.remove();
00102         }
00103     }
00104     public boolean contains(Object o)
00105     {
00106         for(Iterator i = iterator(); i.hasNext();)
00107         {
00108             if((o==null) ? i.next()==null : o.equals(i.next()))
00109             {
00110                 return true;
00111             }
00112         }
00113 
00114         return false;
00115     }
00116     public boolean containsAll(Collection c)
00117     {
00118         for(Iterator i = c.iterator(); i.hasNext();)
00119         {
00120             if(!contains(i.next()))
00121             {
00122                 return false;
00123             }
00124         }
00125 
00126         return true;
00127     }
00128     public boolean isEmpty()
00129     {
00130         return size() == 0;
00131     }
00132     public boolean remove(Object o)
00133     {
00134         for(Iterator i = iterator(); i.hasNext();)
00135         {
00136             if(o==null ? i.next()==null : o.equals(i.next()))
00137             {
00138                 i.remove();
00139                 return true;
00140             }
00141         }
00142 
00143         return false;
00144     }
00145     public boolean removeAll(Collection c)
00146     {
00147         boolean modified = false;
00148 
00149         for(Iterator i = iterator(); i.hasNext();)
00150         {
00151             Object e = i.next();
00152 
00153             if(c.contains(e))
00154             {
00155                 modified = true;
00156                 i.remove();
00157             }
00158         }
00159 
00160         return modified;
00161     }
00162     public boolean retainAll(Collection c)
00163     {
00164         boolean modified = false;
00165 
00166         for(Iterator i = iterator(); i.hasNext();)
00167         {
00168             Object e = i.next();
00169 
00170             if(!c.contains(e))
00171             {
00172                 modified = true;
00173                 i.remove();
00174             }
00175         }
00176 
00177         return modified;
00178     }
00179     public Object[] toArray()
00180     {
00181         Object[] a = new Object[size()];
00182         int pos = 0;
00183 
00184         for(Iterator i = iterator(); i.hasNext();)
00185         {
00186             a[pos++] = i.next();
00187         }
00188 
00189         return a;
00190     }
00191     public void toArray(Object[] a)
00192     {
00193         int pos = 0;
00194 
00195         for(Iterator i = iterator(); i.hasNext();)
00196         {
00197             a[pos++] = i.next();
00198         }
00199     }
00200     public String toString()
00201     {
00202         StringBuffer s = new StringBuffer();
00203         boolean first = true;
00204 
00205         s.append("{");
00206         for(Iterator i = iterator(); i.hasNext();)
00207         {
00208             if(!first)
00209             {
00210                 s.append(",");
00211             }
00212             else
00213             {
00214                 first = false;
00215             }
00216 
00217             s.append(i.next());
00218         }
00219         s.append("}");
00220 
00221         return s.toString();
00222     }
00223 }

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