comparison common/src/main/java/digilib/util/HashTree.java @ 903:7779b37d1d05

refactored into maven modules per servlet type. can build servlet-api 2.3 and 3.0 via profile now!
author robcast
date Tue, 26 Apr 2011 20:24:31 +0200
parents servlet/src/main/java/digilib/util/HashTree.java@ba1eb2d821a2
children
comparison
equal deleted inserted replaced
902:89ba3ffcf552 903:7779b37d1d05
1 /* HashTree -- Tree in a Hashtable
2
3 Digital Image Library servlet components
4
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
6
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
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 */
20
21 package digilib.util;
22
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.StringTokenizer;
27
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 */
41 public class HashTree {
42
43 private Map<String, String> table;
44
45 private String twigSep = "/";
46
47 private String leafSep = ",";
48
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 */
59 public HashTree(Map<String, String> t, String twig_separator, String leaf_separator) {
60 table = t;
61 twigSep = twig_separator;
62 leafSep = leaf_separator;
63 optimizeTable();
64 }
65
66 private void optimizeTable() {
67 }
68
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 */
79 public List<String> match(String branch) {
80 String b = "";
81 String m;
82 LinkedList<String> matches = new LinkedList<String>();
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 }
93 m = table.get(b);
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;
109 } else {
110 return null;
111 }
112 }
113 }