1 | /*
|
---|
2 | * DBInfo.java
|
---|
3 | * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 | package org.greenstone.gsdl3.util;
|
---|
20 |
|
---|
21 | import java.util.Vector;
|
---|
22 | import java.util.HashMap;
|
---|
23 | import java.util.Set;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * class to hold info from a gdbm (or equiv) query maps a String key to a list
|
---|
27 | * of Strings (values) at the moment, the user must know if something has a
|
---|
28 | * single value or not
|
---|
29 | */
|
---|
30 | public class DBInfo
|
---|
31 | {
|
---|
32 | protected HashMap<String, Vector<String>> info_map_;
|
---|
33 |
|
---|
34 | public DBInfo()
|
---|
35 | {
|
---|
36 | info_map_ = new HashMap<String, Vector<String>>();
|
---|
37 | }
|
---|
38 |
|
---|
39 | // methods for keys that can have a single value
|
---|
40 |
|
---|
41 | /** set the value for a key - replaces any existing value */
|
---|
42 | public void setInfo(String key, String value)
|
---|
43 | {
|
---|
44 | Vector<String> v = new Vector<String>();
|
---|
45 | v.add(value);
|
---|
46 | info_map_.put(key, v);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * get the value - used for keys that have one value, otherwise just returns
|
---|
51 | * the first one
|
---|
52 | */
|
---|
53 | public String getInfo(String key)
|
---|
54 | {
|
---|
55 | Vector<String> items = info_map_.get(key);
|
---|
56 | if (items == null)
|
---|
57 | {
|
---|
58 | return "";
|
---|
59 | }
|
---|
60 | return items.firstElement();
|
---|
61 | }
|
---|
62 |
|
---|
63 | // methods for keys that can have multiple values
|
---|
64 |
|
---|
65 | /** add a value to a key - for keys that can have multiple values */
|
---|
66 | public void addInfo(String key, String value)
|
---|
67 | {
|
---|
68 | Vector<String> v = info_map_.get(key);
|
---|
69 | if (v == null)
|
---|
70 | {
|
---|
71 | v = new Vector<String>();
|
---|
72 | }
|
---|
73 | v.add(value);
|
---|
74 | info_map_.put(key, v);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * return a vector of all values associated with a key
|
---|
79 | *
|
---|
80 | * @return Vector of Strings
|
---|
81 | */
|
---|
82 | public Vector<String> getMultiInfo(String key)
|
---|
83 | {
|
---|
84 | return info_map_.get(key);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** returns a list of all the keys in the info */
|
---|
88 | public Set<String> getKeys()
|
---|
89 | {
|
---|
90 | return info_map_.keySet();
|
---|
91 | }
|
---|
92 |
|
---|
93 | /** returns the Info as a string */
|
---|
94 | public String toString()
|
---|
95 | {
|
---|
96 | return info_map_.toString();
|
---|
97 |
|
---|
98 | }
|
---|
99 | }
|
---|