Mercurial > projects > dwt-mac
comparison dwt/internal/Compatibility.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 34bfdb096054 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2006 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 * Port to the D Programming language: | |
12 * Jacob Carlborg <jacob.carlborg@gmail.com> | |
13 *******************************************************************************/ | |
14 module dwt.internal.Compatibility; | |
15 | |
16 import Math = tango.math.Math; | |
17 import Character = tango.text.Unicode; | |
18 | |
19 import dwt.DWT; | |
20 | |
21 import dwt.dwthelper.BufferedInputStream; | |
22 import dwt.dwthelper.File; | |
23 import dwt.dwthelper.FileInputStream; | |
24 import dwt.dwthelper.FileOutputStream; | |
25 import dwt.dwthelper.InputStream; | |
26 import dwt.dwthelper.OutputStream; | |
27 import java.text.MessageFormat; | |
28 import dwt.dwthelper.MissingResourceException; | |
29 import dwt.dwthelper.ResourceBundle; | |
30 import dwt.dwthelper.utils; | |
31 import java.util.zip.DeflaterOutputStream; | |
32 import java.util.zip.InflaterInputStream; | |
33 | |
34 | |
35 | |
36 /** | |
37 * This class is a placeholder for utility methods commonly | |
38 * used on J2SE platforms but not supported on some J2ME | |
39 * profiles. | |
40 * <p> | |
41 * It is part of our effort to provide support for both J2SE | |
42 * and J2ME platforms. | |
43 * </p> | |
44 * <p> | |
45 * IMPORTANT: some of the methods have been modified from their | |
46 * J2SE parents. Refer to the description of each method for | |
47 * specific changes. | |
48 * </p> | |
49 * <ul> | |
50 * <li>Exceptions thrown may differ since J2ME's set of | |
51 * exceptions is a subset of J2SE's one. | |
52 * </li> | |
53 * <li>The range of the mathematic functions is subject to | |
54 * change. | |
55 * </li> | |
56 * </ul> | |
57 */ | |
58 public final class Compatibility | |
59 { | |
60 | |
61 /** | |
62 * Returns the PI constant as a double. | |
63 */ | |
64 public static double PI = Math.PI; | |
65 | |
66 static double toRadians = PI / 180; | |
67 | |
68 /** | |
69 * Answers the length of the side adjacent to the given angle | |
70 * of a right triangle. In other words, it returns the integer | |
71 * conversion of length * cos (angle). | |
72 * <p> | |
73 * IMPORTANT: the j2me version has an additional restriction on | |
74 * the argument. length must be between -32767 and 32767 (inclusive). | |
75 * </p> | |
76 * | |
77 * @param angle the angle in degrees | |
78 * @param length the length of the triangle's hypotenuse | |
79 * @return the integer conversion of length * cos (angle) | |
80 */ | |
81 public static int cos (int angle, int length) | |
82 { | |
83 return cast(int) (Math.cos(angle * toRadians) * length); | |
84 } | |
85 | |
86 /** | |
87 * Answers the length of the side opposite to the given angle | |
88 * of a right triangle. In other words, it returns the integer | |
89 * conversion of length * sin (angle). | |
90 * <p> | |
91 * IMPORTANT: the j2me version has an additional restriction on | |
92 * the argument. length must be between -32767 and 32767 (inclusive). | |
93 * </p> | |
94 * | |
95 * @param angle the angle in degrees | |
96 * @param length the length of the triangle's hypotenuse | |
97 * @return the integer conversion of length * sin (angle) | |
98 */ | |
99 public static int sin (int angle, int length) | |
100 { | |
101 return cast(int) (Math.sin(angle * toRadians) * length); | |
102 } | |
103 | |
104 /** | |
105 * Answers the most negative (i.e. closest to negative infinity) | |
106 * integer value which is greater than the number obtained by dividing | |
107 * the first argument p by the second argument q. | |
108 * | |
109 * @param p numerator | |
110 * @param q denominator (must be different from zero) | |
111 * @return the ceiling of the rational number p / q. | |
112 */ | |
113 public static int ceil (int p, int q) | |
114 { | |
115 return cast(int) Math.ceil(cast(float) p / q); | |
116 } | |
117 | |
118 /** | |
119 * Answers whether the indicated file exists or not. | |
120 * | |
121 * @param parent the file's parent directory | |
122 * @param child the file's name | |
123 * @return true if the file exists | |
124 */ | |
125 public static bool fileExists (String parent, String child) | |
126 { | |
127 return (new File(parent, child)).exists(); | |
128 } | |
129 | |
130 /** | |
131 * Answers the most positive (i.e. closest to positive infinity) | |
132 * integer value which is less than the number obtained by dividing | |
133 * the first argument p by the second argument q. | |
134 * | |
135 * @param p numerator | |
136 * @param q denominator (must be different from zero) | |
137 * @return the floor of the rational number p / q. | |
138 */ | |
139 public static int floor (int p, int q) | |
140 { | |
141 return cast(int) Math.floor(cast(double) p / q); | |
142 } | |
143 | |
144 /** | |
145 * Answers the result of rounding to the closest integer the number obtained | |
146 * by dividing the first argument p by the second argument q. | |
147 * <p> | |
148 * IMPORTANT: the j2me version has an additional restriction on | |
149 * the arguments. p must be within the range 0 - 32767 (inclusive). | |
150 * q must be within the range 1 - 32767 (inclusive). | |
151 * </p> | |
152 * | |
153 * @param p numerator | |
154 * @param q denominator (must be different from zero) | |
155 * @return the closest integer to the rational number p / q | |
156 */ | |
157 public static int round (int p, int q) | |
158 { | |
159 return Math.round(cast(float) p / q); | |
160 } | |
161 | |
162 /** | |
163 * Returns 2 raised to the power of the argument. | |
164 * | |
165 * @param n an int value between 0 and 30 (inclusive) | |
166 * @return 2 raised to the power of the argument | |
167 * | |
168 * @exception IllegalArgumentException <ul> | |
169 * <li>ERROR_INVALID_RANGE - if the argument is not between 0 and 30 (inclusive)</li> | |
170 * </ul> | |
171 */ | |
172 public static int pow2 (int n) | |
173 { | |
174 if (n >= 1 && n <= 30) | |
175 return 2 << (n - 1); | |
176 else if (n !is 0) | |
177 { | |
178 DWT.error(DWT.ERROR_INVALID_RANGE); | |
179 } | |
180 return 1; | |
181 } | |
182 | |
183 /** | |
184 * Create an DeflaterOutputStream if such things are supported. | |
185 * | |
186 * @param stream the output stream | |
187 * @return a deflater stream or <code>null</code> | |
188 * @exception IOException | |
189 * | |
190 * @since 3.4 | |
191 */ | |
192 public static OutputStream newDeflaterOutputStream (OutputStream stream) | |
193 { | |
194 return new DeflaterOutputStream(stream); | |
195 } | |
196 | |
197 /** | |
198 * Open a file if such things are supported. | |
199 * | |
200 * @param filename the name of the file to open | |
201 * @return a stream on the file if it could be opened. | |
202 * @exception IOException | |
203 */ | |
204 public static InputStream newFileInputStream (String filename) | |
205 { | |
206 return new FileInputStream(filename); | |
207 } | |
208 | |
209 /** | |
210 * Open a file if such things are supported. | |
211 * | |
212 * @param filename the name of the file to open | |
213 * @return a stream on the file if it could be opened. | |
214 * @exception IOException | |
215 */ | |
216 public static OutputStream newFileOutputStream (String filename) | |
217 { | |
218 return new FileOutputStream(filename); | |
219 } | |
220 | |
221 /** | |
222 * Create an InflaterInputStream if such things are supported. | |
223 * | |
224 * @param stream the input stream | |
225 * @return a inflater stream or <code>null</code> | |
226 * @exception IOException | |
227 * | |
228 * @since 3.3 | |
229 */ | |
230 public static InputStream newInflaterInputStream (InputStream stream) | |
231 { | |
232 return new BufferedInputStream(new InflaterInputStream(stream)); | |
233 } | |
234 | |
235 /** | |
236 * Answers whether the character is a letter. | |
237 * | |
238 * @param c the character | |
239 * @return true when the character is a letter | |
240 */ | |
241 public static bool isLetter (char c) | |
242 { | |
243 return Character.isLetter(c); | |
244 } | |
245 | |
246 /** | |
247 * Answers whether the character is a letter or a digit. | |
248 * | |
249 * @param c the character | |
250 * @return true when the character is a letter or a digit | |
251 */ | |
252 public static bool isLetterOrDigit (char c) | |
253 { | |
254 return Character.isLetterOrDigit(c); | |
255 } | |
256 | |
257 /** | |
258 * Answers whether the character is a Unicode space character. | |
259 * | |
260 * @param c the character | |
261 * @return true when the character is a Unicode space character | |
262 */ | |
263 public static bool isSpaceChar (char c) | |
264 { | |
265 return Character.isSpace(c); | |
266 } | |
267 | |
268 /** | |
269 * Answers whether the character is a whitespace character. | |
270 * | |
271 * @param c the character to test | |
272 * @return true if the character is whitespace | |
273 */ | |
274 public static bool isWhitespace (char c) | |
275 { | |
276 return Character.isWhitespace(c); | |
277 } | |
278 | |
279 /** | |
280 * Execute a program in a separate platform process if the | |
281 * underlying platform support this. | |
282 * <p> | |
283 * The new process inherits the environment of the caller. | |
284 * </p> | |
285 * | |
286 * @param prog the name of the program to execute | |
287 * | |
288 * @exception IOException | |
289 * if the program cannot be executed | |
290 * @exception SecurityException | |
291 * if the current SecurityManager disallows program execution | |
292 */ | |
293 public static void exec (String prog) | |
294 { | |
295 //Runtime.getRuntime().exec(prog); | |
296 } | |
297 | |
298 /** | |
299 * Execute progArray[0] in a separate platform process if the | |
300 * underlying platform support this. | |
301 * <p> | |
302 * The new process inherits the environment of the caller. | |
303 * <p> | |
304 * | |
305 * @param progArray array containing the program to execute and its arguments | |
306 * | |
307 * @exception IOException | |
308 * if the program cannot be executed | |
309 * @exception SecurityException | |
310 * if the current SecurityManager disallows program execution | |
311 */ | |
312 public static void exec (String[] progArray) | |
313 { | |
314 Runtime.getRuntime().exec(progArray); | |
315 } | |
316 | |
317 private static ResourceBundle msgs = null; | |
318 | |
319 /** | |
320 * Returns the NLS'ed message for the given argument. This is only being | |
321 * called from DWT. | |
322 * | |
323 * @param key the key to look up | |
324 * @return the message for the given key | |
325 * | |
326 * @see DWT#getMessage(String) | |
327 */ | |
328 public static String getMessage (String key) | |
329 { | |
330 String answer = key; | |
331 | |
332 if (key is null) | |
333 { | |
334 DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
335 } | |
336 if (msgs is null) | |
337 { | |
338 try | |
339 { | |
340 msgs = ResourceBundle.getBundle("dwt.internal.SWTMessages"); //$NON-NLS-1$ | |
341 } | |
342 catch (MissingResourceException ex) | |
343 { | |
344 answer = key + " (no resource bundle)"; //$NON-NLS-1$ | |
345 } | |
346 } | |
347 if (msgs !is null) | |
348 { | |
349 try | |
350 { | |
351 answer = msgs.getString(key); | |
352 } | |
353 catch (MissingResourceException ex2) | |
354 { | |
355 } | |
356 } | |
357 return answer; | |
358 } | |
359 | |
360 public static String getMessage (String key, Object[] args) | |
361 { | |
362 String answer = key; | |
363 | |
364 if (key is null || args is null) | |
365 { | |
366 DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
367 } | |
368 if (msgs is null) | |
369 { | |
370 try | |
371 { | |
372 msgs = ResourceBundle.getBundle("dwt.internal.SWTMessages"); //$NON-NLS-1$ | |
373 } | |
374 catch (MissingResourceException ex) | |
375 { | |
376 answer = key + " (no resource bundle)"; //$NON-NLS-1$ | |
377 } | |
378 } | |
379 if (msgs !is null) | |
380 { | |
381 try | |
382 { | |
383 MessageFormat formatter = new MessageFormat(""); | |
384 formatter.applyPattern(msgs.getString(key)); | |
385 answer = formatter.format(args); | |
386 } | |
387 catch (MissingResourceException ex2) | |
388 { | |
389 } | |
390 } | |
391 return answer; | |
392 } | |
393 | |
394 /** | |
395 * Interrupt the current thread. | |
396 * <p> | |
397 * Note that this is not available on CLDC. | |
398 * </p> | |
399 */ | |
400 public static void interrupt () | |
401 { | |
402 Thread.currentThread().interrupt(); | |
403 } | |
404 | |
405 /** | |
406 * Compares two instances of class String ignoring the case of the | |
407 * characters and answers if they are equal. | |
408 * | |
409 * @param s1 string | |
410 * @param s2 string | |
411 * @return true if the two instances of class String are equal | |
412 */ | |
413 public static bool equalsIgnoreCase (String s1, String s2) | |
414 { | |
415 return s1.equalsIgnoreCase(s2); | |
416 } | |
417 | |
418 } |