// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: AbstractCollection.jlc,v 1.11 2006-01-14 11:34:22 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/util/AbstractCollection.h" #include "jakelib2/util/Iterator.h" using namespace jakelib::lang; using namespace jakelib::util; JAKELIB_IMPLEMENT_CLASS_1("jakelib.util.AbstractCollection", AbstractCollection, Object, Collection); /*****************************************************************************\ * AbstractCollection | *****************************************************************************/ AbstractCollection::AbstractCollection() {} /*****************************************************************************\ * add | *****************************************************************************/ jboolean AbstractCollection::add(Object* o) { throw new UnsupportedOperationException(); } /*****************************************************************************\ * addAll | *****************************************************************************/ jboolean AbstractCollection::addAll(Collection* c) { jboolean modified = false; for (Iterator* it = c->iterator(); it->hasNext(); ) { modified |= add(it->next()); } return modified; } /*****************************************************************************\ * clear | *****************************************************************************/ void AbstractCollection::clear() { Iterator* itr = iterator(); int pos = size(); while (--pos >= 0) { itr->next(); itr->remove(); } } /*****************************************************************************\ * contains | *****************************************************************************/ jboolean AbstractCollection::contains(Object* o) { Iterator* itr = iterator(); int pos = size(); while (--pos >= 0) if (equals(o, itr->next())) return true; return false; } /*****************************************************************************\ * containsAll | *****************************************************************************/ jboolean AbstractCollection::containsAll(Collection* c) { Iterator* itr = c->iterator(); int pos = c->size(); while (--pos >= 0) if (!contains(itr->next())) return false; return true; } /*****************************************************************************\ * isEmpty | *****************************************************************************/ jboolean AbstractCollection::isEmpty() { return size() == 0; } /*****************************************************************************\ * remove | *****************************************************************************/ jboolean AbstractCollection::remove(Object* o) { Iterator* itr = iterator(); int pos = size(); while (--pos >= 0) if (equals(o, itr->next())) { itr->remove(); return true; } return false; } /*****************************************************************************\ * removeAll | *****************************************************************************/ jboolean AbstractCollection::removeAll(Collection* c) { return removeAllInternal(c); } /*****************************************************************************\ * removeAllInternal | *****************************************************************************/ jboolean AbstractCollection::removeAllInternal(Collection* c) { Iterator* itr = iterator(); jboolean modified = false; int pos = size(); while (--pos >= 0) if (c->contains(itr->next())) { itr->remove(); modified = true; } return modified; } /*****************************************************************************\ * retainAll | *****************************************************************************/ jboolean AbstractCollection::retainAll(Collection* c) { return retainAllInternal(c); } /*****************************************************************************\ * retainAllInternal | *****************************************************************************/ jboolean AbstractCollection::retainAllInternal(Collection* c) { Iterator* itr = iterator(); jboolean modified = false; int pos = size(); while (--pos >= 0) if (!c->contains(itr->next())) { itr->remove(); modified = true; } return modified; } /*****************************************************************************\ * toArray | *****************************************************************************/ // Objects* AbstractCollection::toArray() // { // Iterator* itr = iterator(); // int s = size(); // Objects* a = new Objects(s); // for (int pos = 0; pos < s; pos++) // a->set(pos, itr->next()); // return a; // } // Objects* AbstractCollection::toArray(Objects* a) // { // int s = size(); // if (a->length < s) // a = (Objects*) Array.newInstance(a.getClass().getComponentType(), // size); // else if (a.length > size) // a[size] = null; // Iterator itr = iterator(); // for (int pos = 0; pos < size; pos++) // a[pos] = itr.next(); // return a; // } /*****************************************************************************\ * toString | *****************************************************************************/ String* AbstractCollection::toString() { Iterator* itr = iterator(); StringBuffer r; r.append('['); for (int pos = size(); pos > 0; pos--) { r.append(itr->next()); if (pos > 1) r.append(", "); } r.append(']'); return r.toString(); } /*****************************************************************************\ * equals | *****************************************************************************/ jboolean AbstractCollection::equals(Object* o1, Object* o2) { return o1 == null ? o2 == null : o1->equals(o2); } /*****************************************************************************\ * hashCode | *****************************************************************************/ int AbstractCollection::hashCode(Object* o) { return o == null ? 0 : o->hashCode(); }