comparison dwt/internal/Compatibility.d @ 12:34bfdb096054

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