KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ml > SplitCamelCaseIdentifier


1 /*
2  * Machine Learning support for FindBugs
3  * Copyright (C) 2005, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package edu.umd.cs.findbugs.ml;
20
21 import java.util.Collection JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * Split a camel case identifier into individual words.
28  *
29  * @author David Hovemeyer
30  */

31 public class SplitCamelCaseIdentifier {
32     private String JavaDoc ident;
33     
34     /**
35      * Constructor.
36      *
37      * @param ident the identifier to split into words
38      */

39     public SplitCamelCaseIdentifier(String JavaDoc ident) {
40         this.ident = ident;
41     }
42     
43     /**
44      * Split the identifier into words.
45      *
46      * @return Collection of words in the identifier
47      */

48     public Collection JavaDoc<String JavaDoc> split() {
49         String JavaDoc s = ident;
50         Set JavaDoc<String JavaDoc> result = new HashSet JavaDoc<String JavaDoc>();
51         
52         while (s.length() > 0) {
53             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
54
55             char first = s.charAt(0);
56             buf.append(first);
57             int i = 1;
58             
59             if (s.length() > 1) {
60                 boolean camelWord;
61                 if (Character.isLowerCase(first)) {
62                     camelWord = true;
63                 } else {
64                     char next = s.charAt(i++);
65                     buf.append(next);
66                     camelWord = Character.isLowerCase(next);
67                 }
68                 
69                 while (i < s.length()) {
70                     char c = s.charAt(i);
71                     if (Character.isUpperCase(c)) {
72                         if (camelWord)
73                             break;
74                     } else if (!camelWord) {
75                         break;
76                     }
77                     buf.append(c);
78                     ++i;
79                 }
80                 
81                 if (!camelWord && i < s.length()) {
82                     buf.deleteCharAt(buf.length() - 1);
83                     --i;
84                 }
85             }
86             
87             result.add(buf.toString().toLowerCase(Locale.US));
88             s = s.substring(i);
89         }
90         
91         return result;
92     }
93 }
94
Popular Tags