1 | /*
|
---|
2 | * module: pip/java/gdbm -- A Java interface to the GDBM library
|
---|
3 | * class: GdbmDictionary -- Wrap up a GDBM File for use as a Dictionary
|
---|
4 | *
|
---|
5 | * Copyright (C) 1997 Pharos IP Pty Ltd
|
---|
6 | * $Id: GdbmDictionary.java 10737 2005-10-19 03:06:40Z kjdon $
|
---|
7 | *
|
---|
8 | * This program is free software; you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation; either version 2 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This program is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with this program; if not, write to the Free Software
|
---|
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
21 | */
|
---|
22 |
|
---|
23 | package au.com.pharos.gdbm;
|
---|
24 |
|
---|
25 | import java.util.Dictionary;
|
---|
26 | import java.util.Enumeration;
|
---|
27 |
|
---|
28 | /** A GDBM database, presented as a Java dictionary.
|
---|
29 | *
|
---|
30 | * <P>This is a separate class which defers most operations to
|
---|
31 | * GdbmFile because GdbmFile can throw IO exceptions, and because the
|
---|
32 | * interface assumptions for Dictionary are not the same as those of
|
---|
33 | * the IO library. Perhaps they should be the same.
|
---|
34 | *
|
---|
35 | * <P>The Dictionary class will be partially replaced by the much
|
---|
36 | * more elegant
|
---|
37 | * <A HREF="http://www.javasoft.com/products/jdk/preview/docs/guide/collections/overview.html">Collections API</A>
|
---|
38 | * in JDK1.2, at which time a GDBM Collections interface will probably
|
---|
39 | * be available.
|
---|
40 | *
|
---|
41 | * @see java.util.Dictionary
|
---|
42 | * @see GdbmFile
|
---|
43 | *
|
---|
44 | * @author Martin Pool
|
---|
45 | * @version $Revision: 10737 $
|
---|
46 | **/
|
---|
47 | // TODO: I wonder if this should be an inner class of GdbmFile? It
|
---|
48 | // would bloat that source file a bit, but would (arguably) more
|
---|
49 | // accurately capture the intended design. -- mbp
|
---|
50 | public class GdbmDictionary extends Dictionary implements Closeable
|
---|
51 | {
|
---|
52 | // XXX: This should probably become a private field, but on the other
|
---|
53 | // hand it's probably useful to be able to apply GDBM-specific methods to
|
---|
54 | // the underlying DB. -- mbp
|
---|
55 | GdbmFile db;
|
---|
56 |
|
---|
57 | public GdbmDictionary(GdbmFile db) {
|
---|
58 | this.db = db;
|
---|
59 | }
|
---|
60 |
|
---|
61 | private Exception caughtException;
|
---|
62 |
|
---|
63 | public Exception checkError() {
|
---|
64 | return caughtException;
|
---|
65 | }
|
---|
66 |
|
---|
67 | public int size() {
|
---|
68 | try {
|
---|
69 | return db.size();
|
---|
70 | } catch (Exception e) {
|
---|
71 | caughtException = e;
|
---|
72 | return 0;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public boolean isEmpty() {
|
---|
77 | try {
|
---|
78 | return db.isEmpty();
|
---|
79 | } catch (Exception e) {
|
---|
80 | caughtException = e;
|
---|
81 | return false;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public Enumeration keys() {
|
---|
86 | try {
|
---|
87 | return db.keys();
|
---|
88 | } catch (Exception e) {
|
---|
89 | caughtException = e;
|
---|
90 | return null;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public Enumeration elements() {
|
---|
95 | // TODO: write an iterator class to do this
|
---|
96 | throw new NoSuchMethodError();
|
---|
97 | }
|
---|
98 |
|
---|
99 | public Object get(Object key) {
|
---|
100 | try {
|
---|
101 | return db.fetch(key);
|
---|
102 | } catch (Exception e) {
|
---|
103 | caughtException = e;
|
---|
104 | return null;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | public Object put(Object key, Object value) {
|
---|
109 | try {
|
---|
110 | Object oldValue = db.fetch(key);
|
---|
111 | db.store(key, value);
|
---|
112 | return oldValue;
|
---|
113 | } catch (Exception e) {
|
---|
114 | caughtException = e;
|
---|
115 | return null;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public Object remove(Object key) {
|
---|
120 | try {
|
---|
121 | Object oldValue = db.fetch(key);
|
---|
122 | db.delete(key);
|
---|
123 | return oldValue;
|
---|
124 | } catch (Exception e) {
|
---|
125 | caughtException = e;
|
---|
126 | return null;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | /** Write outstanding changes to the underlying database, and
|
---|
131 | * close the database.
|
---|
132 | *
|
---|
133 | * @see au.com.pharos.gdbm.Closeable
|
---|
134 | **/
|
---|
135 | public void close() {
|
---|
136 | try {
|
---|
137 | if (db != null && db.isOpen())
|
---|
138 | db.close();
|
---|
139 | db = null;
|
---|
140 | } catch (GdbmException e) {
|
---|
141 | ;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | /** Write outstanding changes to the underlying database, but
|
---|
146 | * leave the database open.
|
---|
147 | *
|
---|
148 | * @see au.com.pharos.gdbm.Closeable
|
---|
149 | **/
|
---|
150 | public void sync() {
|
---|
151 | try {
|
---|
152 | db.sync();
|
---|
153 | } catch (GdbmException e) {
|
---|
154 | ;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|