// -*- 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: AbstractMap.jlc,v 1.6 2003/09/27 08:10:29 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/util/AbstractMap.h" #include "jakelib2/util/AbstractSet.h" #include "jakelib2/util/AbstractCollection.h" #include "jakelib2/util/Iterator.h" using namespace jakelib::lang; using namespace jakelib::util; JAKELIB_IMPLEMENT_CLASS("jakelib.util.AbstractMap", AbstractMap, Object); JAKELIB_IMPLEMENT_MEMBER_INTERFACE("jakelib.util.AbstractMap.BasicMapEntry", AbstractMap, BasicMapEntry); #pragma javasyntax const jint AbstractMap::KEYS = 0; const jint AbstractMap::VALUES = 1; const jint AbstractMap::ENTRIES = 2; /*****************************************************************************\ * AbstractMap | *****************************************************************************/ AbstractMap::AbstractMap() { } /*****************************************************************************\ * clear | *****************************************************************************/ void AbstractMap::clear() { entrySet()->clear(); } /*****************************************************************************\ * clone | *****************************************************************************/ jakelib::lang::Object* AbstractMap::clone() { AbstractMap* copy = (AbstractMap*) clone(); // Clear out the caches; they are stale. copy->keys = null; copy->values = null; return copy; } /*****************************************************************************\ * containsKey | *****************************************************************************/ jboolean AbstractMap::containsKey(Object* key) { Iterator* entries = entrySet()->iterator(); jint pos = size(); while (--pos >= 0) if (equals(key, ((Map::Entry*) entries->next())->getKey())) return true; return false; } /*****************************************************************************\ * containsValue | *****************************************************************************/ jboolean AbstractMap::containsValue(Object* value) { Iterator* entries = entrySet()->iterator(); int pos = size(); while (--pos >= 0) if (equals(value, ((Map::Entry*) entries->next())->getValue())) return true; return false; } /*****************************************************************************\ * equals | *****************************************************************************/ jboolean AbstractMap::equals(Object* o) { return (((o == this) || (o->instanceOf("jakelib.util.Map")) && entrySet()->equals((Object *) ((jakelib::util::Map*) o)->entrySet()))); } /*****************************************************************************\ * get | *****************************************************************************/ Object* AbstractMap::get(Object* key) { Iterator* entries = entrySet()->iterator(); int pos = size(); while (--pos >= 0) { Map::Entry* entry = (Map::Entry*) entries->next(); if (equals(key, entry->getKey())) return entry->getValue(); } return null; } /*****************************************************************************\ * hashCode | *****************************************************************************/ jint AbstractMap::hashCode() { return entrySet()->hashCode(); } /*****************************************************************************\ * isEmpty | *****************************************************************************/ jboolean AbstractMap::isEmpty() { return size() == 0; } /*****************************************************************************\ * keySet | *****************************************************************************/ jakelib::util::Set* AbstractMap::keySet() {/*FIXME: how to re-implement this in C++ is to be thought about Explanation: see AbstractMap::getValues() comment */ throw new UnsupportedOperationException(); #if 0 if (keys == null) keys = new AbstractSet() { jint size() { return this->size(); } jboolean contains(jakelib::lang::Object* key) { return containsKey(key); } jakelib::util::Iterator* iterator() { return new jakelib::util::Iterator() { private: jakelib::util::Iterator* map_iterator = entrySet()->iterator(); jboolean hasNext() { return map_iterator->hasNext(); } jakelib::lang::Object* next() { return (((Map::Entry*) map_iterator)->next())->getKey(); } void remove() { map_iterator->remove(); } }; } }; #endif//#if 0 return keys; } /*****************************************************************************\ * put | *****************************************************************************/ jakelib::lang::Object* AbstractMap::put(jakelib::lang::Object* key, jakelib::lang::Object* value) { throw new UnsupportedOperationException(); } /*****************************************************************************\ * putAll | *****************************************************************************/ void AbstractMap::putAll(jakelib::util::Map* m) { jakelib::util::Iterator* entries = (m->entrySet())->iterator(); int pos = m->size(); while (--pos >= 0) { Map::Entry* entry = (Map::Entry*) entries->next(); put(entry->getKey(), entry->getValue()); } } /*****************************************************************************\ * remove | *****************************************************************************/ jakelib::lang::Object* AbstractMap::remove(jakelib::lang::Object* key) { jakelib::util::Iterator* entries = entrySet()->iterator(); int pos = size(); while (--pos >= 0) { Map::Entry* entry = (Map::Entry*) entries->next(); if (equals(key, entry->getKey())) { // Must get the value before we remove it from iterator. jakelib::lang::Object* r = entry->getValue(); entries->remove(); return r; } } return null; } /*****************************************************************************\ * size | *****************************************************************************/ jint AbstractMap::size() { return entrySet()->size(); } /*****************************************************************************\ * toString | *****************************************************************************/ jakelib::lang::String* AbstractMap::toString() { jakelib::util::Iterator* entries = entrySet()->iterator(); jakelib::lang::StringBuffer* r = new StringBuffer("{"); for (jint pos = size(); pos > 0; pos--) { Map::Entry* entry = (Map::Entry*) entries->next(); r->append(entry->getKey()); r->append('='); r->append(entry->getValue()); if (pos > 1) r->append(", "); } r->append('}'); return r->toString(); } /*****************************************************************************\ * values | *****************************************************************************/ //AlFa: changed values()=>getValues() (can't have same member function and variable name in C++) //jakelib::util::Collection* AbstractMap::values() jakelib::util::Collection* AbstractMap::getValues() {/*//FIXME: how to re-implement this function in C++ ? // Explanation: // the problem is that AbstractCollection can not be instantiated here // because there are still some virtual functions that are not defined // (for example: Colection::equals(Object*)*/ throw new UnsupportedOperationException(); #if 0 if (values == null) values = new AbstractCollection() { jint size() { return AbstractMap::size(); } jakelib::util::Iterator* iterator() { return new jakelib::util::Iterator() { private: jakelib::util::Iterator* map_iterator = entrySet()->iterator(); jboolean hasNext() { return map_iterator->hasNext(); } jakelib::lang::Object* next() { return (((Map::Entry*) map_iterator)->next())->getValue(); } void remove() { map_iterator->remove(); } }; } }; #endif//#if 0 return values; } /*****************************************************************************\ * equals | *****************************************************************************/ jboolean AbstractMap::equals(jakelib::lang::Object* o1, jakelib::lang::Object* o2) { return o1 == null ? o2 == null : o1->equals(o2); } /*****************************************************************************\ * hashCode | *****************************************************************************/ jint AbstractMap::hashCode(jakelib::lang::Object* o) { return o == null ? 0 : o->hashCode(); } /*****************************************************************************\ * BasicMapEntry | *****************************************************************************/ AbstractMap::BasicMapEntry::BasicMapEntry(jakelib::lang::Object* newKey, jakelib::lang::Object* newValue) { key = newKey; value = newValue; } /*****************************************************************************\ * equals | *****************************************************************************/ jboolean AbstractMap::BasicMapEntry::equals(jakelib::lang::Object* o) { if (!(o->instanceOf("jakelib.util.Map.Entry"))) return false; // Optimize for our own entries. if (o->instanceOf("jakelib.util.AbstractMap.BasicMapEntry")) { BasicMapEntry* e = (BasicMapEntry*) o; return (AbstractMap::equals(key, e->key) && AbstractMap::equals(value, e->value)); } jakelib::util::Map::Entry* e = (jakelib::util::Map::Entry*) o; return (AbstractMap::equals(key, e->getKey()) && AbstractMap::equals(value, e->getValue())); } /*****************************************************************************\ * getKey | *****************************************************************************/ jakelib::lang::Object* AbstractMap::BasicMapEntry::getKey() { return key; } /*****************************************************************************\ * getValue | *****************************************************************************/ jakelib::lang::Object* AbstractMap::BasicMapEntry::getValue() { return value; } /*****************************************************************************\ * hashCode | *****************************************************************************/ jint AbstractMap::BasicMapEntry::hashCode() { return (AbstractMap::hashCode(key) ^ AbstractMap::hashCode(value)); } /*****************************************************************************\ * setValue | *****************************************************************************/ jakelib::lang::Object* AbstractMap::BasicMapEntry::setValue(jakelib::lang::Object* newVal) { Object* r = value; value = newVal; return r; } /*****************************************************************************\ * toString | *****************************************************************************/ jakelib::lang::String* AbstractMap::BasicMapEntry::toString() { return key->toString() .. "=" .. value->toString(); }