1
|
1 /* HashTree -- Tree in a Hashtable
|
|
2
|
205
|
3 Digital Image Library servlet components
|
1
|
4
|
205
|
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
|
1
|
6
|
205
|
7 This program is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2 of the License, or (at your
|
|
10 option) any later version.
|
|
11
|
|
12 Please read license.txt for the full details. A copy of the GPL
|
|
13 may be found at http://www.gnu.org/copyleft/lgpl.html
|
1
|
14
|
205
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
1
|
18
|
205
|
19 */
|
1
|
20
|
590
|
21 package digilib.util;
|
1
|
22
|
531
|
23 import java.util.LinkedList;
|
|
24 import java.util.List;
|
|
25 import java.util.Map;
|
|
26 import java.util.StringTokenizer;
|
1
|
27
|
205
|
28 /**
|
|
29 * Tree representation wrapper for a HashMap.
|
|
30 *
|
|
31 * The HashTree is constructed from a HashMap filled with 'branches' with
|
|
32 * 'leaves'. The branches are stored as String keys in the HashMap. The String
|
|
33 * values are leaves.
|
|
34 *
|
|
35 * Branches are matched in 'twigs' separated by 'twig separator' Strings. The
|
|
36 * return values for a match are leaf values separated by 'leaf separator'
|
|
37 * Strings.
|
|
38 *
|
|
39 * @author casties
|
|
40 */
|
1
|
41 public class HashTree {
|
|
42
|
531
|
43 private Map<String, String> table;
|
1
|
44
|
205
|
45 private String twigSep = "/";
|
|
46
|
|
47 private String leafSep = ",";
|
1
|
48
|
205
|
49 /**
|
|
50 * Constructor of a HashTree.
|
|
51 *
|
|
52 * Creates a HashTree wrapper around a given HashMap, using the given twig
|
|
53 * separator and leaf separator.
|
|
54 *
|
|
55 * @param t
|
|
56 * @param twig_separator
|
|
57 * @param leaf_separator
|
|
58 */
|
531
|
59 public HashTree(Map<String, String> t, String twig_separator, String leaf_separator) {
|
205
|
60 table = t;
|
|
61 twigSep = twig_separator;
|
|
62 leafSep = leaf_separator;
|
|
63 optimizeTable();
|
|
64 }
|
1
|
65
|
590
|
66 private void optimizeTable() {
|
205
|
67 }
|
1
|
68
|
205
|
69 /**
|
|
70 * Matches the given branch against the HashTree.
|
|
71 *
|
|
72 * Returns a LinkedList of all leaves on all matching branches in the tree.
|
|
73 * Branches in the tree match if they are substrings starting at the same
|
|
74 * root.
|
|
75 *
|
|
76 * @param branch
|
|
77 * @return
|
|
78 */
|
590
|
79 public List<String> match(String branch) {
|
205
|
80 String b = "";
|
|
81 String m;
|
531
|
82 LinkedList<String> matches = new LinkedList<String>();
|
205
|
83
|
|
84 // split branch
|
|
85 StringTokenizer twig = new StringTokenizer(branch, twigSep);
|
|
86 // walk branch and check with tree
|
|
87 while (twig.hasMoreTokens()) {
|
|
88 if (b.length() == 0) {
|
|
89 b = twig.nextToken();
|
|
90 } else {
|
|
91 b += twigSep + twig.nextToken();
|
|
92 }
|
531
|
93 m = table.get(b);
|
205
|
94 if (m != null) {
|
|
95 if (m.indexOf(leafSep) < 0) {
|
|
96 // single leaf
|
|
97 matches.add(m);
|
|
98 } else {
|
|
99 // split leaves
|
|
100 StringTokenizer leaf = new StringTokenizer(m, leafSep);
|
|
101 while (leaf.hasMoreTokens()) {
|
|
102 matches.add(leaf.nextToken());
|
|
103 }
|
|
104 }
|
|
105 }
|
|
106 }
|
|
107 if (matches.size() > 0) {
|
|
108 return matches;
|
1
|
109 } else {
|
205
|
110 return null;
|
1
|
111 }
|
|
112 }
|
|
113 }
|