KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > util > PrefixStringMatcher


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.util;
5
6 import java.util.Collection JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 /**
10  * A class for efficiently matching <code>String</code>s against a set
11  * of prefixes.
12  */

13 public class PrefixStringMatcher extends TrieStringMatcher {
14
15   /**
16    * Creates a new <code>PrefixStringMatcher</code> which will match
17    * <code>String</code>s with any prefix in the supplied array.
18    * Zero-length <code>Strings</code> are ignored.
19    */

20   public PrefixStringMatcher(String JavaDoc[] prefixes) {
21     super();
22     for (int i= 0; i < prefixes.length; i++)
23       addPatternForward(prefixes[i]);
24   }
25
26   /**
27    * Creates a new <code>PrefixStringMatcher</code> which will match
28    * <code>String</code>s with any prefix in the supplied
29    * <code>Collection</code>.
30    *
31    * @throws ClassCastException if any <code>Object</code>s in the
32    * collection are not <code>String</code>s
33    */

34   public PrefixStringMatcher(Collection JavaDoc prefixes) {
35     super();
36     Iterator JavaDoc iter= prefixes.iterator();
37     while (iter.hasNext())
38       addPatternForward((String JavaDoc)iter.next());
39   }
40
41   /**
42    * Returns true if the given <code>String</code> is matched by a
43    * prefix in the trie
44    */

45   public boolean matches(String JavaDoc input) {
46     TrieNode node= root;
47     for (int i= 0; i < input.length(); i++) {
48       node= node.getChild(input.charAt(i));
49       if (node == null)
50         return false;
51       if (node.isTerminal())
52         return true;
53     }
54     return false;
55   }
56
57   /**
58    * Returns the shortest prefix of <code>input<code> that is matched,
59    * or <code>null<code> if no match exists.
60    */

61   public String JavaDoc shortestMatch(String JavaDoc input) {
62     TrieNode node= root;
63     for (int i= 0; i < input.length(); i++) {
64       node= node.getChild(input.charAt(i));
65       if (node == null)
66         return null;
67       if (node.isTerminal())
68         return input.substring(0, i+1);
69     }
70     return null;
71   }
72
73   /**
74    * Returns the longest prefix of <code>input<code> that is matched,
75    * or <code>null<code> if no match exists.
76    */

77   public String JavaDoc longestMatch(String JavaDoc input) {
78     TrieNode node= root;
79     String JavaDoc result= null;
80     for (int i= 0; i < input.length(); i++) {
81       node= node.getChild(input.charAt(i));
82       if (node == null)
83         break;
84       if (node.isTerminal())
85         result= input.substring(0, i+1);
86     }
87     return result;
88   }
89
90   public static final void main(String JavaDoc[] argv) {
91     PrefixStringMatcher matcher=
92       new PrefixStringMatcher(
93         new String JavaDoc[]
94         {"abcd", "abc", "aac", "baz", "foo", "foobar"} );
95
96     String JavaDoc[] tests= {"a", "ab", "abc", "abcdefg", "apple", "aa", "aac",
97                      "aaccca", "abaz", "baz", "bazooka", "fo", "foobar",
98                      "kite", };
99
100     for (int i= 0; i < tests.length; i++) {
101       System.out.println("testing: " + tests[i]);
102       System.out.println(" matches: " + matcher.matches(tests[i]));
103       System.out.println(" shortest: " + matcher.shortestMatch(tests[i]));
104       System.out.println(" longest: " + matcher.longestMatch(tests[i]));
105     }
106   }
107 }
108
Popular Tags