Back to index

plt-scheme  4.2.1
AbstractCollection.java
Go to the documentation of this file.
00001 /* AbstractCollection.java -- Abstract implementation of most of Collection
00002    Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.
00003 
00004 This file is part of GNU Classpath.
00005 
00006 GNU Classpath is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU General Public License as published by
00008 the Free Software Foundation; either version 2, or (at your option)
00009 any later version.
00010 
00011 GNU Classpath is distributed in the hope that it will be useful, but
00012 WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with GNU Classpath; see the file COPYING.  If not, write to the
00018 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019 02111-1307 USA.
00020 
00021 Linking this library statically or dynamically with other modules is
00022 making a combined work based on this library.  Thus, the terms and
00023 conditions of the GNU General Public License cover the whole
00024 combination.
00025 
00026 As a special exception, the copyright holders of this library give you
00027 permission to link this library with independent modules to produce an
00028 executable, regardless of the license terms of these independent
00029 modules, and to copy and distribute the resulting executable under
00030 terms of your choice, provided that you also meet, for each linked
00031 independent module, the terms and conditions of the license of that
00032 module.  An independent module is a module which is not derived from
00033 or based on this library.  If you modify this library, you may extend
00034 this exception to your version of the library, but you are not
00035 obligated to do so.  If you do not wish to do so, delete this
00036 exception statement from your version. */
00037 
00038 
00039 package java.util;
00040 
00041 //import java.lang.reflect.Array;
00042 
00071 public abstract class AbstractCollection implements Collection
00072 {
00076   protected AbstractCollection()
00077   {
00078   }
00079 
00087   public abstract Iterator iterator();
00088 
00095   public abstract int size();
00096 
00113   public boolean add(Object o)
00114   {
00115     throw new UnsupportedOperationException();
00116   }
00117 
00139   public boolean addAll(Collection c)
00140   {
00141     Iterator itr = c.iterator();
00142     boolean modified = false;
00143     int pos = c.size();
00144     while (--pos >= 0)
00145       modified = modified | add(itr.next());
00146     return modified;
00147   }
00148 
00161   public void clear()
00162   {
00163     Iterator itr = iterator();
00164     int pos = size();
00165     while (--pos >= 0)
00166       {
00167         itr.next();
00168         itr.remove();
00169       }
00170   }
00171 
00183   public boolean contains(Object o)
00184   {
00185     Iterator itr = iterator();
00186     int pos = size();
00187     while (--pos >= 0)
00188       if (equals(o, itr.next()))
00189         return true;
00190     return false;
00191   }
00192 
00205   public boolean containsAll(Collection c)
00206   {
00207     Iterator itr = c.iterator();
00208     int pos = c.size();
00209     while (--pos >= 0)
00210       if (!contains(itr.next()))
00211         return false;
00212     return true;
00213   }
00214 
00222   public boolean isEmpty()
00223   {
00224     return size() == 0;
00225   }
00226 
00246   public boolean remove(Object o)
00247   {
00248     Iterator itr = iterator();
00249     int pos = size();
00250     while (--pos >= 0)
00251       if (equals(o, itr.next()))
00252         {
00253           itr.remove();
00254           return true;
00255         }
00256     return false;
00257   }
00258 
00273   public boolean removeAll(Collection c)
00274   {
00275     return removeAllInternal(c);
00276   }
00277 
00293   boolean removeAllInternal(Collection c)
00294   {
00295     Iterator itr = iterator();
00296     boolean modified = false;
00297     int pos = size();
00298     while (--pos >= 0)
00299       if (c.contains(itr.next()))
00300         {
00301           itr.remove();
00302           modified = true;
00303         }
00304     return modified;
00305   }
00306 
00321   public boolean retainAll(Collection c)
00322   {
00323     return retainAllInternal(c);
00324   }
00325 
00342   boolean retainAllInternal(Collection c)
00343   {
00344     Iterator itr = iterator();
00345     boolean modified = false;
00346     int pos = size();
00347     while (--pos >= 0)
00348       if (!c.contains(itr.next()))
00349         {
00350           itr.remove();
00351           modified = true;
00352         }
00353     return modified;
00354   }
00355 
00365   public Object[] toArray()
00366   {
00367     Iterator itr = iterator();
00368     int size = size();
00369     Object[] a = new Object[size];
00370     for (int pos = 0; pos < size; pos++)
00371       a[pos] = itr.next();
00372     return a;
00373   }
00374 
00397   public Object[] toArray(Object[] a)
00398   {
00399      throw new RuntimeException("toArray(Object[]) is unimplemented");
00400 //    int size = size();
00401 //    if (a.length < size)
00402 //      a = (Object[]) Array.newInstance(a.getClass().getComponentType(),
00403 //                                       size);
00404 //    else if (a.length > size)
00405 //      a[size] = null;
00406 
00407 //    Iterator itr = iterator();
00408 //    for (int pos = 0; pos < size; pos++)
00409 //      a[pos] = itr.next();
00410 //
00411 //    return a;
00412   }
00413 
00423   public String toString()
00424   { return super.toString();
00425     //Iterator itr = iterator();
00426     //StringBuffer r = new StringBuffer("[");
00427     //for (int pos = size(); pos > 0; pos--)
00428     //  {
00429     //    r.append(itr.next());
00430     //    if (pos > 1)
00431     //      r.append(", ");
00432     //  }
00433     //r.append("]");
00434     //return r.toString();
00435   }
00436 
00444   // Package visible for use throughout java.util.
00445   // It may be inlined since it is final.
00446   static final boolean equals(Object o1, Object o2)
00447   {
00448     return o1 == null ? o2 == null : o1.equals(o2);
00449   }
00450 
00457   // Package visible for use throughout java.util.
00458   // It may be inlined since it is final.
00459   static final int hashCode(Object o)
00460   {
00461     return o == null ? 0 : o.hashCode();
00462   }
00463 }