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

AbstractList.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 public abstract class AbstractList extends AbstractCollection implements List
00070 {
00071     protected transient int modCount;
00072 
00073     private class AbstractListIterator implements ListIterator
00074     {
00075         private int index = 0;
00076         private int lastIndex = -1;
00077         private int localModCount = modCount;
00078 
00079         AbstractListIterator(int index)
00080         {
00081             this.index = index;
00082         }
00083 
00084         public void set(Object o)
00085         {
00086             if(localModCount != modCount)
00087             {
00088                 throw new ConcurrentModificationException();
00089             }
00090 
00091             if(lastIndex == -1)
00092             {
00093                 throw new java.util.NoSuchElementException();
00094             }
00095 
00096             AbstractList.this.set(lastIndex, o);
00097         }
00098 
00099         public void add(Object o)
00100         {
00101             if(localModCount != modCount)
00102             {
00103                 throw new ConcurrentModificationException();
00104             }
00105 
00106             AbstractList.this.add(index, o);
00107             localModCount = modCount;
00108         }
00109 
00110         public int nextIndex()
00111         {
00112             if(localModCount != modCount)
00113             {
00114                 throw new ConcurrentModificationException();
00115             }
00116 
00117             return index;
00118         }
00119 
00120         public int previousIndex()
00121         {
00122             if(localModCount != modCount)
00123             {
00124                 throw new ConcurrentModificationException();
00125             }
00126 
00127             return index - 1;
00128         }
00129 
00130         public boolean hasPrevious()
00131         {
00132             if(localModCount != modCount)
00133             {
00134                 throw new ConcurrentModificationException();
00135             }
00136 
00137             return index > 0;
00138         }
00139 
00140         public Object previous()
00141         {
00142             if(localModCount != modCount)
00143             {
00144                 throw new ConcurrentModificationException();
00145             }
00146 
00147             lastIndex = --index;
00148             return get(index);
00149         }
00150 
00151         public boolean hasNext()
00152         {
00153             if(localModCount != modCount)
00154             {
00155                 throw new ConcurrentModificationException();
00156             }
00157 
00158             return index < size();
00159         }
00160 
00161         public Object next()
00162         {
00163             if(localModCount != modCount)
00164             {
00165                 throw new ConcurrentModificationException();
00166             }
00167 
00168             lastIndex = index;
00169             return get(index++);
00170         }
00171 
00172         public void remove()
00173         {
00174             if(localModCount != modCount)
00175             {
00176                 throw new ConcurrentModificationException();
00177             }
00178 
00179             if(lastIndex == -1)
00180             {
00181                 throw new java.util.NoSuchElementException();
00182             }
00183 
00184             if(lastIndex != index)
00185             {
00186                 index = lastIndex;
00187             }
00188 
00189             AbstractList.this.remove(lastIndex);
00190             localModCount = modCount;
00191             lastIndex = -1;
00192         }
00193     }
00194     public void add(int index, Object element)
00195     {
00196         throw new UnsupportedOperationException();
00197     }
00198     public boolean add(Object o)
00199     {
00200         add(size(), o);
00201         return true;
00202     }
00203     public boolean addAll(int index, Collection c)
00204     {
00205         boolean modified = false;
00206 
00207         for(Iterator i = c.iterator(); i.hasNext();)
00208         {
00209             add(index++, i.next());
00210             modified = true;
00211         }
00212 
00213         return modified;
00214     }
00215     public boolean equals(Object o)
00216     {
00217         if(o == this)
00218         {
00219             return true;
00220         }
00221 
00222         if(!(o instanceof List))
00223         {
00224             return false;
00225         }
00226 
00227         List list = (List) o;
00228 
00229         Iterator j = list.iterator();
00230         for(Iterator i = iterator(); i.hasNext();)
00231         {
00232             if(!j.hasNext())
00233             {
00234                 return false;
00235             }
00236 
00237             if(!i.next().equals(j.next()))
00238             {
00239                 return false;
00240             }
00241         }
00242 
00243         if(j.hasNext())
00244         {
00245             return false;
00246         }
00247 
00248         return true;
00249     }
00250     public int hashCode()
00251     {
00252        int hashCode = 0;
00253 
00254        Iterator i = iterator();
00255 
00256        while(i.hasNext())
00257        {
00258            Object obj = i.next();
00259            hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
00260        }
00261 
00262        return hashCode;
00263     }
00264     public int indexOf(Object o)
00265     {
00266         if(size() == 0)
00267         {
00268             return -1;
00269         }
00270 
00271         return indexOf(o, 0);
00272     }
00273     public int indexOf(Object o, int index)
00274     {
00275         ListIterator i = listIterator(index);
00276         do
00277         {
00278             if(o == null ? i.next() == null : o.equals(i.next()))
00279             {
00280                 return i.previousIndex();
00281             }
00282         }
00283         while(i.hasNext());
00284 
00285         return -1;
00286     }
00287     public Iterator iterator()
00288     {
00289         return listIterator();
00290     }
00291     public int lastIndexOf(Object o)
00292     {
00293         int size = size();
00294 
00295         if(size == 0)
00296         {
00297             return -1;
00298         }
00299 
00300         return lastIndexOf(o, size - 1);
00301     }
00302     public int lastIndexOf(Object o, int index)
00303     {
00304         for(ListIterator i = listIterator(index + 1); i.hasPrevious();)
00305         {
00306             if(o == null ? i.previous() == null : o.equals(i.previous()))
00307             {
00308                 return i.nextIndex();
00309             }
00310         }
00311 
00312         return -1;
00313     }
00314     public ListIterator listIterator()
00315     {
00316         return listIterator(0);
00317     }
00318     public ListIterator listIterator(int index)
00319     {
00320         return new AbstractListIterator(index);
00321     }
00322     public Object remove(int index)
00323     {
00324         throw new UnsupportedOperationException();
00325     }
00326     public void removeRange(int fromIndex, int toIndex)
00327     {
00328         int size = size();
00329 
00330         if(fromIndex < 0 || fromIndex >= size || toIndex > size || toIndex < fromIndex)
00331         {
00332             throw new ArrayIndexOutOfBoundsException();
00333         }
00334 
00335         int count = toIndex - fromIndex;
00336 
00337         for(int i = 0; i < count; i++)
00338         {
00339             remove(fromIndex);
00340         }
00341     }
00342     public Object set(int index, Object element)
00343     {
00344         throw new UnsupportedOperationException();
00345     }
00346 }

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