001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.swt.internal;
011:
012: import java.io.*;
013: import java.text.MessageFormat;
014: import java.util.MissingResourceException;
015: import java.util.ResourceBundle;
016: import java.util.zip.InflaterInputStream;
017: import java.util.zip.DeflaterOutputStream;
018:
019: import org.eclipse.swt.SWT;
020:
021: /**
022: * This class is a placeholder for utility methods commonly
023: * used on J2SE platforms but not supported on some J2ME
024: * profiles.
025: * <p>
026: * It is part of our effort to provide support for both J2SE
027: * and J2ME platforms.
028: * </p>
029: * <p>
030: * IMPORTANT: some of the methods have been modified from their
031: * J2SE parents. Refer to the description of each method for
032: * specific changes.
033: * </p>
034: * <ul>
035: * <li>Exceptions thrown may differ since J2ME's set of
036: * exceptions is a subset of J2SE's one.
037: * </li>
038: * <li>The range of the mathematic functions is subject to
039: * change.
040: * </li>
041: * </ul>
042: */
043: public final class Compatibility {
044:
045: /**
046: * Returns the PI constant as a double.
047: */
048: public static double PI = Math.PI;
049:
050: static double toRadians = PI / 180;
051:
052: /**
053: * Answers the length of the side adjacent to the given angle
054: * of a right triangle. In other words, it returns the integer
055: * conversion of length * cos (angle).
056: * <p>
057: * IMPORTANT: the j2me version has an additional restriction on
058: * the argument. length must be between -32767 and 32767 (inclusive).
059: * </p>
060: *
061: * @param angle the angle in degrees
062: * @param length the length of the triangle's hypotenuse
063: * @return the integer conversion of length * cos (angle)
064: */
065: public static int cos(int angle, int length) {
066: return (int) (Math.cos(angle * toRadians) * length);
067: }
068:
069: /**
070: * Answers the length of the side opposite to the given angle
071: * of a right triangle. In other words, it returns the integer
072: * conversion of length * sin (angle).
073: * <p>
074: * IMPORTANT: the j2me version has an additional restriction on
075: * the argument. length must be between -32767 and 32767 (inclusive).
076: * </p>
077: *
078: * @param angle the angle in degrees
079: * @param length the length of the triangle's hypotenuse
080: * @return the integer conversion of length * sin (angle)
081: */
082: public static int sin(int angle, int length) {
083: return (int) (Math.sin(angle * toRadians) * length);
084: }
085:
086: /**
087: * Answers the most negative (i.e. closest to negative infinity)
088: * integer value which is greater than the number obtained by dividing
089: * the first argument p by the second argument q.
090: *
091: * @param p numerator
092: * @param q denominator (must be different from zero)
093: * @return the ceiling of the rational number p / q.
094: */
095: public static int ceil(int p, int q) {
096: return (int) Math.ceil((float) p / q);
097: }
098:
099: /**
100: * Answers the most positive (i.e. closest to positive infinity)
101: * integer value which is less than the number obtained by dividing
102: * the first argument p by the second argument q.
103: *
104: * @param p numerator
105: * @param q denominator (must be different from zero)
106: * @return the floor of the rational number p / q.
107: */
108: public static int floor(int p, int q) {
109: return (int) Math.floor((double) p / q);
110: }
111:
112: /**
113: * Answers the result of rounding to the closest integer the number obtained
114: * by dividing the first argument p by the second argument q.
115: * <p>
116: * IMPORTANT: the j2me version has an additional restriction on
117: * the arguments. p must be within the range 0 - 32767 (inclusive).
118: * q must be within the range 1 - 32767 (inclusive).
119: * </p>
120: *
121: * @param p numerator
122: * @param q denominator (must be different from zero)
123: * @return the closest integer to the rational number p / q
124: */
125: public static int round(int p, int q) {
126: return Math.round((float) p / q);
127: }
128:
129: /**
130: * Returns 2 raised to the power of the argument.
131: *
132: * @param n an int value between 0 and 30 (inclusive)
133: * @return 2 raised to the power of the argument
134: *
135: * @exception IllegalArgumentException <ul>
136: * <li>ERROR_INVALID_RANGE - if the argument is not between 0 and 30 (inclusive)</li>
137: * </ul>
138: */
139: public static int pow2(int n) {
140: if (n >= 1 && n <= 30)
141: return 2 << (n - 1);
142: else if (n != 0) {
143: SWT.error(SWT.ERROR_INVALID_RANGE);
144: }
145: return 1;
146: }
147:
148: /**
149: * Create an DeflaterOutputStream if such things are supported.
150: *
151: * @param stream the output stream
152: * @return a deflater stream or <code>null</code>
153: * @exception IOException
154: *
155: * @since 3.4
156: */
157: public static OutputStream newDeflaterOutputStream(
158: OutputStream stream) throws IOException {
159: return new DeflaterOutputStream(stream);
160: }
161:
162: /**
163: * Open a file if such things are supported.
164: *
165: * @param filename the name of the file to open
166: * @return a stream on the file if it could be opened.
167: * @exception IOException
168: */
169: public static InputStream newFileInputStream(String filename)
170: throws IOException {
171: return new FileInputStream(filename);
172: }
173:
174: /**
175: * Open a file if such things are supported.
176: *
177: * @param filename the name of the file to open
178: * @return a stream on the file if it could be opened.
179: * @exception IOException
180: */
181: public static OutputStream newFileOutputStream(String filename)
182: throws IOException {
183: return new FileOutputStream(filename);
184: }
185:
186: /**
187: * Create an InflaterInputStream if such things are supported.
188: *
189: * @param stream the input stream
190: * @return a inflater stream or <code>null</code>
191: * @exception IOException
192: *
193: * @since 3.3
194: */
195: public static InputStream newInflaterInputStream(InputStream stream)
196: throws IOException {
197: return new InflaterInputStream(stream);
198: }
199:
200: /**
201: * Answers whether the character is a letter.
202: *
203: * @param c the character
204: * @return true when the character is a letter
205: */
206: public static boolean isLetter(char c) {
207: return Character.isLetter(c);
208: }
209:
210: /**
211: * Answers whether the character is a letter or a digit.
212: *
213: * @param c the character
214: * @return true when the character is a letter or a digit
215: */
216: public static boolean isLetterOrDigit(char c) {
217: return Character.isLetterOrDigit(c);
218: }
219:
220: /**
221: * Answers whether the character is a Unicode space character.
222: *
223: * @param c the character
224: * @return true when the character is a Unicode space character
225: */
226: public static boolean isSpaceChar(char c) {
227: return Character.isSpaceChar(c);
228: }
229:
230: /**
231: * Answers whether the character is a whitespace character.
232: *
233: * @param c the character to test
234: * @return true if the character is whitespace
235: */
236: public static boolean isWhitespace(char c) {
237: return Character.isWhitespace(c);
238: }
239:
240: /**
241: * Execute a program in a separate platform process if the
242: * underlying platform support this.
243: * <p>
244: * The new process inherits the environment of the caller.
245: * </p>
246: *
247: * @param prog the name of the program to execute
248: *
249: * @exception IOException
250: * if the program cannot be executed
251: * @exception SecurityException
252: * if the current SecurityManager disallows program execution
253: */
254: public static void exec(String prog) throws java.io.IOException {
255: Runtime.getRuntime().exec(prog);
256: }
257:
258: /**
259: * Execute progArray[0] in a separate platform process if the
260: * underlying platform support this.
261: * <p>
262: * The new process inherits the environment of the caller.
263: * <p>
264: *
265: * @param progArray array containing the program to execute and its arguments
266: *
267: * @exception IOException
268: * if the program cannot be executed
269: * @exception SecurityException
270: * if the current SecurityManager disallows program execution
271: */
272: public static void exec(String[] progArray)
273: throws java.io.IOException {
274: Runtime.getRuntime().exec(progArray);
275: }
276:
277: private static ResourceBundle msgs = null;
278:
279: /**
280: * Returns the NLS'ed message for the given argument. This is only being
281: * called from SWT.
282: *
283: * @param key the key to look up
284: * @return the message for the given key
285: *
286: * @see SWT#getMessage(String)
287: */
288: public static String getMessage(String key) {
289: String answer = key;
290:
291: if (key == null) {
292: SWT.error(SWT.ERROR_NULL_ARGUMENT);
293: }
294: if (msgs == null) {
295: try {
296: msgs = ResourceBundle
297: .getBundle("org.eclipse.swt.internal.SWTMessages"); //$NON-NLS-1$
298: } catch (MissingResourceException ex) {
299: answer = key + " (no resource bundle)"; //$NON-NLS-1$
300: }
301: }
302: if (msgs != null) {
303: try {
304: answer = msgs.getString(key);
305: } catch (MissingResourceException ex2) {
306: }
307: }
308: return answer;
309: }
310:
311: public static String getMessage(String key, Object[] args) {
312: String answer = key;
313:
314: if (key == null || args == null) {
315: SWT.error(SWT.ERROR_NULL_ARGUMENT);
316: }
317: if (msgs == null) {
318: try {
319: msgs = ResourceBundle
320: .getBundle("org.eclipse.swt.internal.SWTMessages"); //$NON-NLS-1$
321: } catch (MissingResourceException ex) {
322: answer = key + " (no resource bundle)"; //$NON-NLS-1$
323: }
324: }
325: if (msgs != null) {
326: try {
327: MessageFormat formatter = new MessageFormat("");
328: formatter.applyPattern(msgs.getString(key));
329: answer = formatter.format(args);
330: } catch (MissingResourceException ex2) {
331: }
332: }
333: return answer;
334: }
335:
336: /**
337: * Interrupt the current thread.
338: * <p>
339: * Note that this is not available on CLDC.
340: * </p>
341: */
342: public static void interrupt() {
343: Thread.currentThread().interrupt();
344: }
345:
346: /**
347: * Compares two instances of class String ignoring the case of the
348: * characters and answers if they are equal.
349: *
350: * @param s1 string
351: * @param s2 string
352: * @return true if the two instances of class String are equal
353: */
354: public static boolean equalsIgnoreCase(String s1, String s2) {
355: return s1.equalsIgnoreCase(s2);
356: }
357:
358: }
|