KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > engine > Util


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

18 package sync4j.framework.engine;
19
20 import java.util.ArrayList JavaDoc;
21
22 /**
23  * @author Stefano Fornari @ Funambol
24  *
25  * @version $Id: Util.java,v 1.4 2005/03/02 20:57:37 harrie Exp $
26  */

27 public final class Util {
28     /**
29      * Creates an array of Boolean starting from an array of strings
30      *
31      * @param string_values array of value to be converted
32      *
33      * @return an array of Boolean
34      */

35     public static boolean[] createArrayOfBooleans(String JavaDoc[] string_values) {
36         boolean[] array = new boolean[string_values.length];
37
38         for(int i=0; i<string_values.length; ++i) {
39             array[i] = Boolean.getBoolean(string_values[i]);
40         } // next i
41

42         return array;
43     } // createArrayOfBooleans
44

45     /**
46      * Creates an array of Character starting from an array of strings
47      *
48      * @param string_values array of value to be converted
49      *
50      * @return an array of Character
51      */

52     public static char[] createArrayOfCharacters(String JavaDoc[] string_values) {
53         char[] array = new char[string_values.length];
54
55         for(int i=0; i<string_values.length; ++i) {
56             array[i] = (string_values[i].length()>0) ? string_values[i].charAt(0) : ' ';
57         } // next i
58

59         return array;
60     } // createArrayOfCharacters
61

62     /**
63      * Creates an array of Double starting from an array of strings
64      *
65      * @param string_values array of value to be converted
66      *
67      * @return an array of Double
68      */

69     public static double[] createArrayOfDoubles(String JavaDoc[] string_values) {
70         double[] array = new double[string_values.length];
71
72         for(int i=0; i<string_values.length; ++i) {
73             array[i] = Double.parseDouble(string_values[i]);
74         } // next i
75

76         return array;
77     } // createArrayOfDoubles
78

79     /**
80      * Creates an array of Float starting from an array of strings
81      *
82      * @param string_values array of value to be converted
83      *
84      * @return an array of Float
85      */

86     public static float[] createArrayOfFloats(String JavaDoc[] string_values) {
87         float[] array = new float[string_values.length];
88
89         for(int i=0; i<string_values.length; ++i) {
90             array[i] = Float.parseFloat(string_values[i]);
91         } // next i
92

93         return array;
94     } // createArrayOfFloats
95

96     /**
97      * Creates an array of Integer starting from an array of strings
98      *
99      * @param string_values array of value to be converted
100      *
101      * @return an array of int
102      */

103     public static int[] createArrayOfIntegers(String JavaDoc[] string_values) {
104         int[] array = new int[string_values.length];
105
106         for(int i=0; i<string_values.length; ++i) {
107             array[i] = Integer.parseInt(string_values[i]);
108         } // next i
109

110         return array;
111     } // createArrayOfIntegers
112

113     /**
114      * Creates an array of Long starting from an array of strings
115      *
116      * @param string_values array of value to be converted
117      *
118      * @return an array of Long
119      */

120     public static long[] createArrayOfLongs(String JavaDoc[] string_values) {
121         long[] array = new long[string_values.length];
122
123         for(int i=0; i<string_values.length; ++i) {
124             array[i] = Long.parseLong(string_values[i]);
125         } // next i
126

127         return array;
128     } // createArrayOfLongs
129

130     // =========================================================================
131

132     /**
133      * Makes a string concatenating the capitalized version of the given words.<p>
134      *
135      * For example:
136      * <pre>
137      * Util.makeCapitalizedString(new String[] { "bean", "blue" });
138      * </pre>
139      * will return <pre>BeanBlue</pre>.<p>
140      *
141      * null strings in the <i>words</i> array will be silenty ignored.
142      *
143      * @param words the strings to capitalize and concatenate
144      *
145      * @return the capitalized string
146      *
147      */

148     public static String JavaDoc makeCapitalizedString(String JavaDoc[] words) {
149         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
150
151         for(int i=0; i<words.length; ++i) {
152             if (words[i] != null) sb.append(capitalizeWord(words[i]));
153         } // next i
154

155         return sb.toString();
156     }
157
158     /**
159      * Capitalizes a word.<p>
160      *
161      * For example:
162      * <pre>
163      * Util.capitalizeWord("bean");
164      * </pre>
165      * will return <pre>Bean</pre>.
166      *
167      * @param word the word to capitalize
168      *
169      * @return the capitalized word
170      */

171     public static String JavaDoc capitalizeWord(String JavaDoc word) {
172         int len = word.length();
173
174         if (len == 1) return word.toUpperCase();
175
176         return word.substring(0, 1).toUpperCase() +
177                word.substring(1 ) ;
178     }
179     
180     /**
181      * Returns a string representation of an array of Objects. It translates
182      * the array in an ArrayList and then invokes toString().
183      *
184      * @param array the array
185      *
186      * @return the string representation of the given array
187      */

188     public static String JavaDoc arrayToString(Object JavaDoc[] array) {
189         ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
190         
191         for (int i=0; i<array.length; ++i) {
192             arrayList.add(array[i]);
193         }
194         
195         return arrayList.toString();
196     }
197 }
Popular Tags