KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > internal > Compatibility


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 package org.eclipse.swt.internal;
12
13  
14 import java.io.*;
15 import java.text.MessageFormat JavaDoc;
16 import java.util.MissingResourceException JavaDoc;
17 import java.util.ResourceBundle JavaDoc;
18 import java.util.zip.InflaterInputStream JavaDoc;
19
20 import org.eclipse.swt.SWT;
21
22 /**
23  * This class is a placeholder for utility methods commonly
24  * used on J2SE platforms but not supported on some J2ME
25  * profiles.
26  * <p>
27  * It is part of our effort to provide support for both J2SE
28  * and J2ME platforms.
29  * </p>
30  * <p>
31  * IMPORTANT: some of the methods have been modified from their
32  * J2SE parents. Refer to the description of each method for
33  * specific changes.
34  * </p>
35  * <ul>
36  * <li>Exceptions thrown may differ since J2ME's set of
37  * exceptions is a subset of J2SE's one.
38  * </li>
39  * <li>The range of the mathematic functions is subject to
40  * change.
41  * </li>
42  * </ul>
43  */

44 public final class Compatibility {
45
46 /**
47  * Returns the PI constant as a double.
48  */

49 public static double PI = Math.PI;
50
51 static double toRadians = PI / 180;
52
53 /**
54  * Answers the length of the side adjacent to the given angle
55  * of a right triangle. In other words, it returns the integer
56  * conversion of length * cos (angle).
57  * <p>
58  * IMPORTANT: the j2me version has an additional restriction on
59  * the argument. length must be between -32767 and 32767 (inclusive).
60  * </p>
61  *
62  * @param angle the angle in degrees
63  * @param length the length of the triangle's hypotenuse
64  * @return the integer conversion of length * cos (angle)
65  */

66 public static int cos(int angle, int length) {
67     return (int)(Math.cos(angle * toRadians) * length);
68 }
69
70 /**
71  * Answers the length of the side opposite to the given angle
72  * of a right triangle. In other words, it returns the integer
73  * conversion of length * sin (angle).
74  * <p>
75  * IMPORTANT: the j2me version has an additional restriction on
76  * the argument. length must be between -32767 and 32767 (inclusive).
77  * </p>
78  *
79  * @param angle the angle in degrees
80  * @param length the length of the triangle's hypotenuse
81  * @return the integer conversion of length * sin (angle)
82  */

83 public static int sin(int angle, int length) {
84     return (int)(Math.sin(angle * toRadians) * length);
85 }
86
87 /**
88  * Answers the most negative (i.e. closest to negative infinity)
89  * integer value which is greater than the number obtained by dividing
90  * the first argument p by the second argument q.
91  *
92  * @param p numerator
93  * @param q denominator (must be different from zero)
94  * @return the ceiling of the rational number p / q.
95  */

96 public static int ceil(int p, int q) {
97     return (int)Math.ceil((float)p / q);
98 }
99
100 /**
101  * Answers the most positive (i.e. closest to positive infinity)
102  * integer value which is less than the number obtained by dividing
103  * the first argument p by the second argument q.
104  *
105  * @param p numerator
106  * @param q denominator (must be different from zero)
107  * @return the floor of the rational number p / q.
108  */

109 public static int floor(int p, int q) {
110     return (int)Math.floor((double)p / q);
111 }
112
113 /**
114  * Answers the result of rounding to the closest integer the number obtained
115  * by dividing the first argument p by the second argument q.
116  * <p>
117  * IMPORTANT: the j2me version has an additional restriction on
118  * the arguments. p must be within the range 0 - 32767 (inclusive).
119  * q must be within the range 1 - 32767 (inclusive).
120  * </p>
121  *
122  * @param p numerator
123  * @param q denominator (must be different from zero)
124  * @return the closest integer to the rational number p / q
125  */

126 public static int round(int p, int q) {
127     return Math.round((float)p / q);
128 }
129
130 /**
131  * Returns 2 raised to the power of the argument.
132  *
133  * @param n an int value between 0 and 30 (inclusive)
134  * @return 2 raised to the power of the argument
135  *
136  * @exception IllegalArgumentException <ul>
137  * <li>ERROR_INVALID_RANGE - if the argument is not between 0 and 30 (inclusive)</li>
138  * </ul>
139  */

140 public static int pow2(int n) {
141     if (n >= 1 && n <= 30)
142         return 2 << (n - 1);
143     else if (n != 0) {
144         SWT.error(SWT.ERROR_INVALID_RANGE);
145     }
146     return 1;
147 }
148
149 /**
150  * Open a file if such things are supported.
151  *
152  * @param filename the name of the file to open
153  * @return a stream on the file if it could be opened.
154  * @exception IOException
155  */

156 public static InputStream JavaDoc newFileInputStream(String JavaDoc filename) throws IOException {
157     return new FileInputStream(filename);
158 }
159
160 /**
161  * Open a file if such things are supported.
162  *
163  * @param filename the name of the file to open
164  * @return a stream on the file if it could be opened.
165  * @exception IOException
166  */

167 public static OutputStream newFileOutputStream(String JavaDoc filename) throws IOException {
168     return new FileOutputStream(filename);
169 }
170
171 /**
172  * Create an InflaterInputStream if such things are supported.
173  *
174  * @param stream the input stream
175  * @return a inflater stream or <code>null</code>
176  * @exception IOException
177  *
178  * @since 3.3
179  */

180 public static InputStream JavaDoc newInflaterInputStream(InputStream JavaDoc stream) throws IOException {
181     return new InflaterInputStream JavaDoc(stream);
182 }
183
184 /**
185  * Answers whether the character is a letter.
186  *
187  * @param c the character
188  * @return true when the character is a letter
189  */

190 public static boolean isLetter(char c) {
191     return Character.isLetter(c);
192 }
193
194 /**
195  * Answers whether the character is a letter or a digit.
196  *
197  * @param c the character
198  * @return true when the character is a letter or a digit
199  */

200 public static boolean isLetterOrDigit(char c) {
201     return Character.isLetterOrDigit(c);
202 }
203
204 /**
205  * Answers whether the character is a Unicode space character.
206  *
207  * @param c the character
208  * @return true when the character is a Unicode space character
209  */

210 public static boolean isSpaceChar(char c) {
211     return Character.isSpaceChar(c);
212 }
213
214 /**
215  * Answers whether the character is a whitespace character.
216  *
217  * @param c the character to test
218  * @return true if the character is whitespace
219  */

220 public static boolean isWhitespace(char c) {
221     return Character.isWhitespace(c);
222 }
223
224 /**
225  * Execute a program in a separate platform process if the
226  * underlying platform support this.
227  * <p>
228  * The new process inherits the environment of the caller.
229  * </p>
230  *
231  * @param prog the name of the program to execute
232  *
233  * @exception IOException
234  * if the program cannot be executed
235  * @exception SecurityException
236  * if the current SecurityManager disallows program execution
237  */

238 public static void exec(String JavaDoc prog) throws java.io.IOException JavaDoc {
239     Runtime.getRuntime().exec(prog);
240 }
241
242 /**
243  * Execute progArray[0] in a separate platform process if the
244  * underlying platform support this.
245  * <p>
246  * The new process inherits the environment of the caller.
247  * <p>
248  *
249  * @param progArray array containing the program to execute and its arguments
250  *
251  * @exception IOException
252  * if the program cannot be executed
253  * @exception SecurityException
254  * if the current SecurityManager disallows program execution
255  */

256 public static void exec(String JavaDoc[] progArray) throws java.io.IOException JavaDoc{
257     Runtime.getRuntime().exec(progArray);
258 }
259
260 private static ResourceBundle JavaDoc msgs = null;
261
262 /**
263  * Returns the NLS'ed message for the given argument. This is only being
264  * called from SWT.
265  *
266  * @param key the key to look up
267  * @return the message for the given key
268  *
269  * @see SWT#getMessage(String)
270  */

271 public static String JavaDoc getMessage(String JavaDoc key) {
272     String JavaDoc answer = key;
273     
274     if (key == null) {
275         SWT.error (SWT.ERROR_NULL_ARGUMENT);
276     }
277     if (msgs == null) {
278         try {
279             msgs = ResourceBundle.getBundle("org.eclipse.swt.internal.SWTMessages"); //$NON-NLS-1$
280
} catch (MissingResourceException JavaDoc ex) {
281             answer = key + " (no resource bundle)"; //$NON-NLS-1$
282
}
283     }
284     if (msgs != null) {
285         try {
286             answer = msgs.getString(key);
287         } catch (MissingResourceException JavaDoc ex2) {}
288     }
289     return answer;
290 }
291
292 public static String JavaDoc getMessage(String JavaDoc key, Object JavaDoc[] args) {
293     String JavaDoc answer = key;
294     
295     if (key == null || args == null) {
296         SWT.error (SWT.ERROR_NULL_ARGUMENT);
297     }
298     if (msgs == null) {
299         try {
300             msgs = ResourceBundle.getBundle("org.eclipse.swt.internal.SWTMessages"); //$NON-NLS-1$
301
} catch (MissingResourceException JavaDoc ex) {
302             answer = key + " (no resource bundle)"; //$NON-NLS-1$
303
}
304     }
305     if (msgs != null) {
306         try {
307             MessageFormat JavaDoc formatter = new MessageFormat JavaDoc("");
308             formatter.applyPattern(msgs.getString(key));
309             answer = formatter.format(args);
310         } catch (MissingResourceException JavaDoc ex2) {}
311     }
312     return answer;
313 }
314
315 /**
316  * Interrupt the current thread.
317  * <p>
318  * Note that this is not available on CLDC.
319  * </p>
320  */

321 public static void interrupt() {
322     Thread.currentThread().interrupt();
323 }
324
325 /**
326  * Compares two instances of class String ignoring the case of the
327  * characters and answers if they are equal.
328  *
329  * @param s1 string
330  * @param s2 string
331  * @return true if the two instances of class String are equal
332  */

333 public static boolean equalsIgnoreCase(String JavaDoc s1, String JavaDoc s2) {
334     return s1.equalsIgnoreCase(s2);
335 }
336
337 }
338
Popular Tags