KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > DxLib > DxHashMap


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: DxHashMap.java,v 1.1 2001/12/18 10:31:30 per_nyfelt Exp $
8

9 package org.ozoneDB.DxLib;
10
11 import java.util.Hashtable JavaDoc;
12
13
14 /**
15  * A DxMap implementation based on java.util.Hashtable.
16  *
17  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
18  * @version $Revision: 1.1 $Date: 2001/12/18 10:31:30 $
19  *
20  *
21  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
22  * @version $Revision: 1.1 $Date: 2001/12/18 10:31:30 $
23  */

24 public class DxHashMap extends DxAbstractMap implements DxHashCollection {
25     
26     final static long serialVersionUID = 1L;
27     
28     protected transient Hashtable JavaDoc ht;
29     
30     
31     public DxHashMap() {
32         ht = new Hashtable JavaDoc();
33     }
34     
35     
36     public DxHashMap( int size ) {
37         ht = new Hashtable JavaDoc( size );
38     }
39     
40     
41     public Object JavaDoc clone() {
42         DxMap newMap = new DxHashMap( Math.max( count(), 8 ) );
43         return clone( newMap );
44     }
45     
46     
47     public boolean addForKey( Object JavaDoc obj, Object JavaDoc key ) {
48         synchronized (ht) {
49             Object JavaDoc old = ht.put( key, obj );
50             if (old != null) {
51                 ht.put( key, old );
52                 return false;
53             }
54             return true;
55         }
56     }
57     
58     
59     public Object JavaDoc elementForKey( Object JavaDoc key ) {
60         return ht.get( key );
61     }
62     
63     
64     public Object JavaDoc keyForElement( Object JavaDoc obj ) {
65         DxIterator it = iterator();
66         Object JavaDoc cursor;
67         while ((cursor = it.next()) != null) {
68             if (obj.equals( cursor )) {
69                 return it.key();
70             }
71         }
72         return null;
73     }
74     
75     
76     public Object JavaDoc removeForKey( Object JavaDoc key ) {
77         return ht.remove( key );
78     }
79     
80     
81     public synchronized boolean remove( Object JavaDoc obj ) {
82         Object JavaDoc key = keyForElement( obj );
83         if (key != null) {
84             removeForKey( key );
85             return true;
86         }
87         return false;
88     }
89     
90     
91     public int count() {
92         return ht.size();
93     }
94     
95     
96     public boolean isEmpty() {
97         return ht.isEmpty();
98     }
99     
100     
101     public boolean containsKey( Object JavaDoc key ) {
102         return ht.containsKey( key );
103     }
104     
105     
106     public DxIterator iterator() {
107         return new DxHashIterator( this );
108     }
109     
110     
111     public void clear() {
112         ht.clear();
113     }
114     
115     
116     public Hashtable JavaDoc internalHashtable() {
117         return ht;
118     }
119 }
120
Popular Tags