001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: DxHashMap.java,v 1.1 2001/12/18 10:31:30 per_nyfelt Exp $
008:
009: package org.ozoneDB.DxLib;
010:
011: import java.util.Hashtable;
012: import java.io.Externalizable;
013:
014: /**
015: * A DxMap implementation based on java.util.Hashtable.
016: *
017: * @author <a href="http://www.softwarebuero.de/">SMB</a>
018: * @version $Revision: 1.1 $Date: 2001/12/18 10:31:30 $
019: *
020: *
021: * @author <a href="http://www.softwarebuero.de/">SMB</a>
022: * @version $Revision: 1.1 $Date: 2001/12/18 10:31:30 $
023: */
024: public class DxHashMap extends DxAbstractMap implements
025: DxHashCollection {
026:
027: final static long serialVersionUID = 1L;
028:
029: protected transient Hashtable ht;
030:
031: public DxHashMap() {
032: ht = new Hashtable();
033: }
034:
035: public DxHashMap(int size) {
036: ht = new Hashtable(size);
037: }
038:
039: public Object clone() {
040: DxMap newMap = new DxHashMap(Math.max(count(), 8));
041: return clone(newMap);
042: }
043:
044: public boolean addForKey(Object obj, Object key) {
045: synchronized (ht) {
046: Object old = ht.put(key, obj);
047: if (old != null) {
048: ht.put(key, old);
049: return false;
050: }
051: return true;
052: }
053: }
054:
055: public Object elementForKey(Object key) {
056: return ht.get(key);
057: }
058:
059: public Object keyForElement(Object obj) {
060: DxIterator it = iterator();
061: Object cursor;
062: while ((cursor = it.next()) != null) {
063: if (obj.equals(cursor)) {
064: return it.key();
065: }
066: }
067: return null;
068: }
069:
070: public Object removeForKey(Object key) {
071: return ht.remove(key);
072: }
073:
074: public synchronized boolean remove(Object obj) {
075: Object key = keyForElement(obj);
076: if (key != null) {
077: removeForKey(key);
078: return true;
079: }
080: return false;
081: }
082:
083: public int count() {
084: return ht.size();
085: }
086:
087: public boolean isEmpty() {
088: return ht.isEmpty();
089: }
090:
091: public boolean containsKey(Object key) {
092: return ht.containsKey(key);
093: }
094:
095: public DxIterator iterator() {
096: return new DxHashIterator(this );
097: }
098:
099: public void clear() {
100: ht.clear();
101: }
102:
103: public Hashtable internalHashtable() {
104: return ht;
105: }
106: }
|