// -*- 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: AbstractList.jlc,v 1.9 2006-01-14 11:34:22 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/util/AbstractList.h" #include "jakelib2/util/Iterator.h" using namespace jakelib::lang; using namespace jakelib::util; JAKELIB_IMPLEMENT_CLASS_1("jakelib.util.AbstractList", AbstractList, AbstractCollection, List); /*****************************************************************************\ * AbstractList | *****************************************************************************/ AbstractList::AbstractList() : AbstractCollection(), List() {} /*****************************************************************************\ * add | *****************************************************************************/ void AbstractList::add(int index, Object* o) { throw new UnsupportedOperationException(); } jboolean AbstractList::add(Object* o) { add(size(), o); return true; } /*****************************************************************************\ * addAll | *****************************************************************************/ jboolean AbstractList::addAll(int index, Collection* c) { Iterator* itr = c->iterator(); int size = c->size(); for (int pos = size; pos > 0; pos--) { add(index++, itr->next()); } return (c->size() > 0); } jboolean AbstractList::addAll(Collection* c) { jboolean modified = false; for (Iterator* it = c->iterator(); it->hasNext(); ) { modified |= add(it->next()); } return modified; } /*****************************************************************************\ * clear | *****************************************************************************/ void AbstractList::clear() { removeRange(0, size()); } /*****************************************************************************\ * equals | *****************************************************************************/ jboolean AbstractList::equals(Object* o) { throw new UnsupportedOperationException(); // if (o == this) // return true; // // if (! (o->instanceOf("jakelib.util.List"))) // // return false; // int size = size(); // if (size != ((List*) o)->size()) // return false; // Iterator* itr1 = iterator(); // Iterator* itr2 = ((List*) o)->iterator(); // while (--size >= 0) // if (! equals(itr1->next(), itr2->next())) // return false; // return true; } /*****************************************************************************\ * hashCode | *****************************************************************************/ int AbstractList::hashCode() { int hc = 1; Iterator* itr = iterator(); int pos = size(); while (--pos >= 0) hc = 31 * hc + AbstractCollection::hashCode(itr->next()); return hc; } /*****************************************************************************\ * indexOf | *****************************************************************************/ int AbstractList::indexOf(Object* o) { throw new UnsupportedOperationException(); // ListIterator itr = listIterator(); // int size = size(); // for (int pos = 0; pos < size; pos++) // if (equals(o, itr.next())) // return pos; // return -1; } /*****************************************************************************\ * iterator | *****************************************************************************/ Iterator* AbstractList::iterator() { throw new UnsupportedOperationException(); // // Bah, Sun's implementation forbids using listIterator(0). // return new Iterator() // { // private int pos = 0; // private int size = size(); // private int last = -1; // private int knownMod = modCount; // // This will get inlined, since it is private. // private void checkMod() // { // if (knownMod != modCount) // throw new new ConcurrentModificationException(); // } // public boolean hasNext() // { // checkMod(); // return pos < size; // } // public Object next() // { // checkMod(); // if (pos == size) // throw new new NoSuchElementException(); // last = pos; // return get(pos++); // } // public void remove() // { // checkMod(); // if (last < 0) // throw new new IllegalStateException(); // AbstractList.this.remove(last); // pos--; // size--; // last = -1; // knownMod = modCount; // } // }; } /*****************************************************************************\ * lastIndexOf | *****************************************************************************/ int AbstractList::lastIndexOf(Object* o) { throw new UnsupportedOperationException(); // int pos = size(); // ListIterator itr = listIterator(pos); // while (--pos >= 0) // if (equals(o, itr.previous())) // return pos; // return -1; } /*****************************************************************************\ * listIterator | *****************************************************************************/ // ListIterator* AbstractList::listIterator() // { // return listIterator(0); // } /*****************************************************************************\ * listIterator | *****************************************************************************/ // ListIterator* AbstractList::listIterator(int index) // { // if (index < 0 || index > size()) // throw new IndexOutOfBoundsException("Index: " + index + ", Size:" // + size()); // return new ListIterator() // { // private int knownMod = modCount; // private int position = index; // private int lastReturned = -1; // private int size = size(); // // This will get inlined, since it is private. // private void checkMod() // { // if (knownMod != modCount) // throw new ConcurrentModificationException(); // } // public boolean hasNext() // { // checkMod(); // return position < size; // } // public boolean hasPrevious() // { // checkMod(); // return position > 0; // } // public Object next() // { // checkMod(); // if (position == size) // throw new NoSuchElementException(); // lastReturned = position; // return get(position++); // } // public Object previous() // { // checkMod(); // if (position == 0) // throw new NoSuchElementException(); // lastReturned = --position; // return get(lastReturned); // } // public int nextIndex() // { // checkMod(); // return position; // } // public int previousIndex() // { // checkMod(); // return position - 1; // } // public void remove() // { // checkMod(); // if (lastReturned < 0) // throw new IllegalStateException(); // AbstractList.this.remove(lastReturned); // size--; // position = lastReturned; // lastReturned = -1; // knownMod = modCount; // } // public void set(Object o) // { // checkMod(); // if (lastReturned < 0) // throw new IllegalStateException(); // AbstractList.this.set(lastReturned, o); // } // public void add(Object o) // { // checkMod(); // AbstractList.this.add(position++, o); // size++; // lastReturned = -1; // knownMod = modCount; // } // }; // } /*****************************************************************************\ * remove | *****************************************************************************/ Object* AbstractList::remove(int index) { throw new UnsupportedOperationException(); } /*****************************************************************************\ * removeRange | *****************************************************************************/ void AbstractList::removeRange(int fromIndex, int toIndex) { throw new UnsupportedOperationException(); // ListIterator itr = listIterator(fromIndex); // for (int index = fromIndex; index < toIndex; index++) // { // itr.next(); // itr.remove(); // } } /*****************************************************************************\ * set | *****************************************************************************/ Object* AbstractList::set(int index, Object* o) { throw new UnsupportedOperationException(); } /*****************************************************************************\ * subList | *****************************************************************************/ List* AbstractList::subList(int fromIndex, int toIndex) { throw new UnsupportedOperationException(); // // This follows the specification of AbstractList, but is inconsistent // // with the one in List. Don't you love Sun's inconsistencies? // if (fromIndex > toIndex) // throw new IllegalArgumentException(fromIndex + " > " + toIndex); // if (fromIndex < 0 || toIndex > size()) // throw new IndexOutOfBoundsException(); // if (this instanceof RandomAccess) // return new RandomAccessSubList(this, fromIndex, toIndex); // return new SubList(this, fromIndex, toIndex); }