KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > quickaccess > CamelUtil


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.internal.quickaccess;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * Utility methods for camel case style matching.
19  *
20  * @since 3.3
21  *
22  */

23 public class CamelUtil {
24
25     /**
26      * Returns a lowercase string consisting of all initials of the words in the
27      * given String. Words are separated by whitespace and other special
28      * characters, or by uppercase letters in a word like CamelCase.
29      *
30      * @param s
31      * the string
32      * @return a lowercase string containing the first character of every wordin
33      * the given string.
34      */

35     public static String JavaDoc getCamelCase(String JavaDoc s) {
36         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
37         if (s.length() > 0) {
38             int index = 0;
39             while (index != -1) {
40                 result.append(s.charAt(index));
41                 index = getNextCamelIndex(s, index + 1);
42             }
43         }
44         return result.toString().toLowerCase();
45     }
46
47     /**
48      * Return an array with start/end indices for the characters used for camel
49      * case matching, ignoring the first (start) many camel case characters.
50      * For example, getCamelCaseIndices("some CamelCase", 1, 2) will return
51      * {{5,5},{10,10}}.
52      *
53      * @param s the source string
54      * @param start how many characters of getCamelCase(s) should be ignored
55      * @param length for how many characters should indices be returned
56      * @return an array of length start
57      */

58     public static int[][] getCamelCaseIndices(String JavaDoc s, int start, int length) {
59         List JavaDoc result = new ArrayList JavaDoc();
60         int index = 0;
61         while (start > 0) {
62             index = getNextCamelIndex(s, index + 1);
63             start--;
64         }
65         while (length > 0) {
66             result.add(new int[] { index, index });
67             index = getNextCamelIndex(s, index + 1);
68             length--;
69         }
70         return (int[][]) result.toArray(new int[result.size()][]);
71     }
72
73     /**
74      * Returns the next index to be used for camel case matching.
75      *
76      * @param s the string
77      * @param index the index
78      * @return the next index, or -1 if not found
79      */

80     public static int getNextCamelIndex(String JavaDoc s, int index) {
81         char c;
82         while (index < s.length()
83                 && !(isSeparatorForCamelCase(c = s.charAt(index)))
84                 && Character.isLowerCase(c)) {
85             index++;
86         }
87         while (index < s.length() && isSeparatorForCamelCase(c = s.charAt(index))) {
88             index++;
89         }
90         if (index >= s.length()) {
91             index = -1;
92         }
93         return index;
94     }
95
96     /**
97      * Returns true if the given character is to be considered a separator
98      * for camel case matching purposes.
99      *
100      * @param c the character
101      * @return true if the character is a separator
102      */

103     public static boolean isSeparatorForCamelCase(char c) {
104         return !Character.isLetterOrDigit(c);
105     }
106
107 }
108
Popular Tags