KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > search > DefaultSearchMethod


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.search;
8
9
10 /**
11  * <p>
12  * This class implements the SearchMethod interface and
13  * provides a default search method. This search method is
14  * as follows:
15  * <ul>
16  * <li>The name is tokenized using the '.' character.</li>
17  * <li>The second to last token is thrown out (including
18  * its '.')</li>
19  * <li>The resulting string is returned</li>
20  * </ul>
21  * </p>
22  *
23  * <p>
24  * An example would be:<br>
25  * <code>one.two.three.four.five</code><br>
26  * <code>one.two.three.five</code><br>
27  * <code>one.two.five</code><br>
28  * <code>one.five</code><br>
29  * <code>five</code><br>
30  * </p>
31  *
32  * @author Brian Pontarelli
33  */

34 public class DefaultSearchMethod implements SearchMethod {
35
36     /**
37      * The String to break on
38      */

39     public static final String JavaDoc DOT = ".";
40
41
42     /**
43      * Returns the next String based on the sequence described in the class
44      * comment.
45      *
46      * @param name The current search String
47      * @return The next search String in the sequence
48      */

49     public String JavaDoc nextSearchName(String JavaDoc name) {
50
51         int lastDot = name.lastIndexOf(DOT);
52
53         if (lastDot < 1) {
54             return null;
55         }
56
57         int secondDot = name.lastIndexOf(DOT, lastDot - 1);
58
59         if (secondDot == -1) {
60             return name.substring(lastDot + 1);
61         }
62
63         return name.substring(0, secondDot + 1) + name.substring(lastDot + 1);
64     }
65 }
66
Popular Tags