source: other-projects/the-macronizer/trunk/src/java/util/StringUtil.java@ 29855

Last change on this file since 29855 was 29855, checked in by davidb, 9 years ago

John's code after refactoring by Tom over the summer of 2014/2015

File size: 8.0 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package util;
7
8import static java.lang.Character.isLowerCase;
9import static java.lang.Character.toLowerCase;
10import static java.lang.Character.toUpperCase;
11
12/**
13 *
14 * @author OEM
15 */
16public class StringUtil {
17
18 public static boolean containsAccents(String str) {
19 if (str == null) {
20 return false;
21 }
22 for (char c : str.toCharArray()) {
23 if (isAccented(c)) {
24 return true;
25 }
26 }
27 return false;
28 }
29
30 public static String removeAccents(String str) {
31 if (str == null || !containsAccents(str)) {
32 return str;
33 }
34 final StringBuilder buffer = new StringBuilder(str.length());
35 for (char c : str.toCharArray()) {
36 buffer.append(removeAccent(c));
37 }
38 return buffer.toString();
39 }
40
41 public static String toDoubleVowel(String str) {
42 if (str == null || !containsAccents(str)) {
43 return str;
44 }
45 final StringBuilder buffer = new StringBuilder();
46 for (char c : str.toCharArray()) {
47 buffer.append(toDoubleVowel(c));
48 }
49 return buffer.toString();
50 }
51
52 public static String copyCapitalization(String source, String destination)
53 throws IllegalArgumentException {
54 checkNotNull(source);
55 checkNotNull(destination);
56 checkEqualLength(source, destination);
57 final StringBuilder buffer = new StringBuilder(source.length());
58 for (int i = 0; i < source.length(); i++) {
59 buffer.append(isLowerCase(source.charAt(i)) ? toLowerCase(destination.charAt(i)) : toUpperCase(destination.charAt(i)));
60 }
61 return buffer.toString();
62 }
63
64 public static String copyDVowelCapitalization(String dvowel, String destination) {
65 checkNotNull(dvowel);
66 checkNotNull(destination);
67 final StringBuilder buffer = new StringBuilder(dvowel.length());
68 for (int i=0, j=0; i < destination.length(); i++, j++) {
69 buffer.append(isLowerCase(dvowel.charAt(j)) ? toLowerCase(destination.charAt(i)) : toUpperCase(destination.charAt(i)));
70 if (isAccented(destination.charAt(i))) {
71 j++;
72 }
73 }
74 return buffer.toString();
75 }
76
77 private static void checkNotNull(String str) throws IllegalArgumentException {
78 if (str == null) {
79 throw new IllegalArgumentException("Required a non null String, found null String.");
80 }
81 }
82
83 private static void checkEqualLength(String s1, String s2) throws IllegalArgumentException {
84 if (s1.length() != s2.length()) {
85 throw new IllegalArgumentException("Required equal String lengths, found unequal String lengths: s1=" + s1.length() + ", s2=" + s2.length());
86 }
87 }
88
89 public static boolean isAccented(char c) {
90 switch (c) {
91 //Set A
92 case 'ā':
93 return true;
94 case 'ē':
95 return true;
96 case 'Ä«':
97 return true;
98 case 'ō':
99 return true;
100 case 'Å«':
101 return true;
102 case 'Ā':
103 return true;
104 case 'Ē':
105 return true;
106 case 'Ī':
107 return true;
108 case 'Ō':
109 return true;
110 case 'Ū':
111 return true;
112 //Set B
113 case 'À':
114 return true;
115 case 'ë':
116 return true;
117 case 'ï':
118 return true;
119 case 'ö':
120 return true;
121 case 'Ì':
122 return true;
123 case 'Ä':
124 return true;
125 case 'Ë':
126 return true;
127 case 'Ï':
128 return true;
129 case 'Ö':
130 return true;
131 case 'Ü':
132 return true;
133 //Set C
134 case 'à':
135 return true;
136 case 'Ú':
137 return true;
138 case 'ì':
139 return true;
140 case 'ò':
141 return true;
142 case 'ù':
143 return true;
144 case 'À':
145 return true;
146 case 'È':
147 return true;
148 case 'Ì':
149 return true;
150 case 'Ò':
151 return true;
152 case 'Ù':
153 return true;
154 //Set D
155 default:
156 return false;
157 }
158 }
159
160 public static char removeAccent(char c) {
161 switch (c) {
162 //Set A
163 case 'ā':
164 return 'a';
165 case 'ē':
166 return 'e';
167 case 'Ä«':
168 return 'i';
169 case 'ō':
170 return 'o';
171 case 'Å«':
172 return 'u';
173 case 'Ā':
174 return 'A';
175 case 'Ē':
176 return 'E';
177 case 'Ī':
178 return 'I';
179 case 'Ō':
180 return 'O';
181 case 'Ū':
182 return 'U';
183 //Set B
184 case 'À':
185 return 'a';
186 case 'ë':
187 return 'e';
188 case 'ï':
189 return 'i';
190 case 'ö':
191 return 'o';
192 case 'Ì':
193 return 'u';
194 case 'Ä':
195 return 'A';
196 case 'Ë':
197 return 'E';
198 case 'Ï':
199 return 'I';
200 case 'Ö':
201 return 'O';
202 case 'Ü':
203 return 'U';
204 //Set C
205 case 'à':
206 return 'a';
207 case 'Ú':
208 return 'e';
209 case 'ì':
210 return 'i';
211 case 'ò':
212 return 'o';
213 case 'ù':
214 return 'u';
215 case 'À':
216 return 'A';
217 case 'È':
218 return 'E';
219 case 'Ì':
220 return 'I';
221 case 'Ò':
222 return 'O';
223 case 'Ù':
224 return 'U';
225 //Set D
226 default:
227 return c;
228 }
229 }
230
231 public static String toDoubleVowel(char c) {
232 switch (c) {
233 //Set A
234 case 'ā':
235 return "aa";
236 case 'ē':
237 return "ee";
238 case 'Ä«':
239 return "ii";
240 case 'ō':
241 return "oo";
242 case 'Å«':
243 return "uu";
244 case 'Ā':
245 return "Aa";
246 case 'Ē':
247 return "Ee";
248 case 'Ī':
249 return "Ii";
250 case 'Ō':
251 return "Oo";
252 case 'Ū':
253 return "Uu";
254 //Set B
255 case 'À':
256 return "aa";
257 case 'ë':
258 return "ee";
259 case 'ï':
260 return "ii";
261 case 'ö':
262 return "oo";
263 case 'Ì':
264 return "uu";
265 case 'Ä':
266 return "Aa";
267 case 'Ë':
268 return "Ee";
269 case 'Ï':
270 return "Ii";
271 case 'Ö':
272 return "Oo";
273 case 'Ü':
274 return "Uu";
275 //Set C
276 case 'à':
277 return "aa";
278 case 'Ú':
279 return "ee";
280 case 'ì':
281 return "ii";
282 case 'ò':
283 return "oo";
284 case 'ù':
285 return "uu";
286 case 'À':
287 return "Aa";
288 case 'È':
289 return "Ee";
290 case 'Ì':
291 return "Ii";
292 case 'Ò':
293 return "Oo";
294 case 'Ù':
295 return "Uu";
296 //Set D
297 default:
298 return Character.toString(c);
299 }
300 }
301}
Note: See TracBrowser for help on using the repository browser.