KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > zeus > util > CapitalizationUtils


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  */

19 package org.enhydra.zeus.util;
20
21 /**
22  * <p>
23  * <code>Capitalization</code> is a Zeus utility class that provides
24  * for converting lowercase letters to uppercase ones, uppercase ones
25  * to lowercase ones, and more generally, converting Java
26  * <code>String</code>s from an unknown format to initial caps (where
27  * only the first letter is capitalized), making a suitable class or
28  * variable name.
29  * </p>
30  *
31  * @author Brett McLaughlin
32  */

33 public class CapitalizationUtils {
34
35     /**
36      * <p>
37      * This will take a <code>String</code> with unknown capitalization,
38      * and convert the first letter to a capital letter, while leaving
39      * all other letters the same. Note that if you want the rest of
40      * the letters converted to lowercase, then
41      * <code>{@link #justInitialUpper(String)}</code> should be used.
42      * </p>
43      *
44      * @param original <code>String</code> to convert.
45      * @return <code>String</code> - the converted <code>String</code>.
46      */

47     public static String JavaDoc initialUpper(String JavaDoc original) {
48         if (original == null) {
49             throw new IllegalArgumentException JavaDoc("A non-null String must be " +
50                 "supplied to CapitalizationUtils methods.");
51         }
52         if (original.length() == 0) {
53             return original;
54         }
55         
56         char[] originalChars = original.toCharArray();
57         char firstLetter =
58             Character.toUpperCase(original.charAt(0));
59         originalChars[0] = firstLetter;
60         
61         return new String JavaDoc(originalChars);
62     }
63     
64     /**
65      * <p>
66      * This will take a <code>String</code> with unknown capitalization,
67      * and convert the first letter to a capital letter, while converting
68      * all other letters to lowercase. Note that if you want the rest of
69      * the letters left alone, then
70      * <code>{@link #initialUpper(String)}</code> should be used.
71      * </p>
72      *
73      * @param original <code>String</code> to convert.
74      * @return <code>String</code> - the converted <code>String</code>.
75      */

76     public static String JavaDoc justInitialUpper(String JavaDoc original) {
77         if (original == null) {
78             throw new IllegalArgumentException JavaDoc("A non-null String must be " +
79                 "supplied to CapitalizationUtils methods.");
80         }
81         
82         String JavaDoc lowercaseOriginal = allLower(original);
83         return initialUpper(lowercaseOriginal);
84     }
85     
86     /**
87      * <p>
88      * This will take a <code>String</code> with unknown capitalization,
89      * and convert all the letters to uppercase letters.
90      * Note that if you want just the first
91      * letter converted to lowercase, then
92      * <code>{@link #initialUpper(String)}</code> should be used.
93      * </p><p>
94      * And for all you geniuses out there, yes, I know that
95      * <code>toUpperCase()</code> can be used directly; but
96      * placing the code within this method allows this class
97      * to be uniformly used for all capitalization purposes.
98      * </p>
99      *
100      * @param original <code>String</code> to convert.
101      * @return <code>String</code> - the converted <code>String</code>.
102      */

103     public static String JavaDoc allUpper(String JavaDoc original) {
104         if (original == null) {
105             throw new IllegalArgumentException JavaDoc("A non-null String must be " +
106                 "supplied to CapitalizationUtils methods.");
107         }
108         
109         return original.toUpperCase();
110     }
111     
112     /**
113      * <p>
114      * This will take a <code>String</code> with unknown capitalization,
115      * and convert the first letter to a lowercase letter, while leaving
116      * all other letters the same. Note that if you want the rest of
117      * the letters converted to lowercase, then
118      * <code>{@link #allLower(String)}</code> should be used.
119      * </p>
120      *
121      * @param original <code>String</code> to convert.
122      * @return <code>String</code> - the converted <code>String</code>.
123      */

124     public static String JavaDoc initialLower(String JavaDoc original) {
125         if (original == null) {
126             throw new IllegalArgumentException JavaDoc("A non-null String must be " +
127                 "supplied to CapitalizationUtils methods.");
128         }
129         if (original.length() == 0) {
130             return original;
131         }
132         
133         char[] originalChars = original.toCharArray();
134         char firstLetter =
135             Character.toLowerCase(original.charAt(0));
136         originalChars[0] = firstLetter;
137         
138         return new String JavaDoc(originalChars);
139     }
140     
141     /**
142      * <p>
143      * This will take a <code>String</code> with unknown capitalization,
144      * and convert all the letters to lowercase letters.
145      * Note that if you want just the first
146      * letter converted to lowercase, then
147      * <code>{@link #initialLower(String)}</code> should be used.
148      * </p><p>
149      * And for all you geniuses out there, yes, I know that
150      * <code>toLowerCase()</code> can be used directly; but
151      * placing the code within this method allows this class
152      * to be uniformly used for all capitalization purposes.
153      * </p>
154      *
155      * @param original <code>String</code> to convert.
156      * @return <code>String</code> - the converted <code>String</code>.
157      */

158     public static String JavaDoc allLower(String JavaDoc original) {
159         if (original == null) {
160             throw new IllegalArgumentException JavaDoc("A non-null String must be " +
161                 "supplied to CapitalizationUtils methods.");
162         }
163         
164         return original.toLowerCase();
165     }
166 }
167
Popular Tags