1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.internal;
21 import org.eclipse.swt.*;
24 * This class is a placeholder for utility methods commonly
25 * used on J2SE platforms but not supported on some J2ME
28 * It is part of our effort to provide support for both J2SE
32 * IMPORTANT: some of the methods have been modified from their
33 * J2SE parents. Refer to the description of each method for
37 * <li>Exceptions thrown may differ since J2ME's set of
38 * exceptions is a subset of J2SE's one.
40 * <li>The range of the mathematic functions is subject to
45 public final class Compatibility {
48 * Answers the most negative (i.e. closest to negative infinity)
49 * integer value which is greater than or equal to the number obtained by dividing
50 * the first argument p by the second argument q.
53 * @param q denominator (must be different from zero)
54 * @return the ceiling of the rational number p / q.
56 public static int ceil(int p, int q) {
57 return (int)Math.ceil((float)p / q);
61 * Answers the result of rounding to the closest integer the number obtained
62 * by dividing the first argument p by the second argument q.
64 * IMPORTANT: the j2me version has an additional restriction on
65 * the arguments. p must be within the range 0 - 32767 (inclusive).
66 * q must be within the range 1 - 32767 (inclusive).
70 * @param q denominator (must be different from zero)
71 * @return the closest integer to the rational number p / q
73 public static int round(int p, int q) {
74 return Math.round((float)p / q);
78 * Returns 2 raised to the power of the argument.
80 * @param n an int value between 0 and 30 (inclusive)
81 * @return 2 raised to the power of the argument
83 * @exception IllegalArgumentException <ul>
84 * <li>ERROR_INVALID_RANGE - if the argument is not between 0 and 30 (inclusive)</li>
87 public static int pow2(int n) {
88 if (n >= 1 && n <= 30)
91 SWT.error(SWT.ERROR_INVALID_RANGE);
97 * Execute prog[0] in a separate platform process if the
98 * underlying platform supports this.
100 * The new process inherits the environment of the caller.
103 * @param prog array containing the program to execute and its arguments
105 * array of strings, each element of which has environment
106 * variable settings in the format name=value
108 * the working directory of the new process, or null if the new
109 * process should inherit the working directory of the caller
111 * @exception IOException
112 * if the program cannot be executed
113 * @exception SecurityException
114 * if the current SecurityManager disallows program execution
118 public static void exec(String[] prog, String[] envp, String workingDir) throws java.io.IOException{
119 Runtime.getRuntime().exec(prog, null, workingDir != null ? new File(workingDir) : null);
122 private static ResourceBundle msgs = null;
125 * Returns the NLS'ed message for the given argument. This is only being
128 * @param key the key to look up
129 * @return the message for the given key
131 * @see SWT#getMessage(String)
133 public static String getMessage(String key) {
137 SWT.error (SWT.ERROR_NULL_ARGUMENT);
141 msgs = ResourceBundle.getBundle("org.eclipse.swt.internal.SWTMessages"); //$NON-NLS-1$
142 } catch (MissingResourceException ex) {
143 answer = key + " (no resource bundle)"; //$NON-NLS-1$
148 answer = msgs.getString(key);
149 } catch (MissingResourceException ex2) {}
154 public static String getMessage(String key, Object[] args) {
157 if (key == null || args == null) {
158 SWT.error (SWT.ERROR_NULL_ARGUMENT);
162 msgs = ResourceBundle.getBundle("org.eclipse.swt.internal.SWTMessages"); //$NON-NLS-1$
163 } catch (MissingResourceException ex) {
164 answer = key + " (no resource bundle)"; //$NON-NLS-1$
169 MessageFormat formatter = new MessageFormat("");
170 formatter.applyPattern(msgs.getString(key));
171 answer = formatter.format(args);
172 } catch (MissingResourceException ex2) {}