]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/internal/Compatibility.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / internal / Compatibility.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.internal;
15
16
17 import java.io.*;
18 import java.text.*;
19 import java.util.*;
20
21 import org.eclipse.swt.*;
22
23 /**
24  * This class is a placeholder for utility methods commonly
25  * used on J2SE platforms but not supported on some J2ME
26  * profiles.
27  * <p>
28  * It is part of our effort to provide support for both J2SE
29  * and J2ME platforms.
30  * </p>
31  * <p>
32  * IMPORTANT: some of the methods have been modified from their
33  * J2SE parents. Refer to the description of each method for
34  * specific changes.
35  * </p>
36  * <ul>
37  * <li>Exceptions thrown may differ since J2ME's set of
38  * exceptions is a subset of J2SE's one.
39  * </li>
40  * <li>The range of the mathematic functions is subject to
41  * change.
42  * </li>
43  * </ul>
44  */
45 public final class Compatibility {
46
47 /**
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.
51  *
52  * @param p numerator
53  * @param q denominator (must be different from zero)
54  * @return the ceiling of the rational number p / q.
55  */
56 public static int ceil(int p, int q) {
57         return (int)Math.ceil((float)p / q);
58 }
59
60 /**
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.
63  * <p>
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).
67  * </p>
68  *
69  * @param p numerator
70  * @param q denominator (must be different from zero)
71  * @return the closest integer to the rational number p / q
72  */
73 public static int round(int p, int q) {
74         return Math.round((float)p / q);
75 }
76
77 /**
78  * Returns 2 raised to the power of the argument.
79  *
80  * @param n an int value between 0 and 30 (inclusive)
81  * @return 2 raised to the power of the argument
82  *
83  * @exception IllegalArgumentException <ul>
84  *    <li>ERROR_INVALID_RANGE - if the argument is not between 0 and 30 (inclusive)</li>
85  * </ul>
86  */
87 public static int pow2(int n) {
88         if (n >= 1 && n <= 30)
89                 return 2 << (n - 1);
90         else if (n != 0) {
91                 SWT.error(SWT.ERROR_INVALID_RANGE);
92         }
93         return 1;
94 }
95
96 /**
97  * Execute prog[0] in a separate platform process if the
98  * underlying platform supports this.
99  * <p>
100  * The new process inherits the environment of the caller.
101  * <p>
102  *
103  * @param prog array containing the program to execute and its arguments
104  * @param envp
105  *            array of strings, each element of which has environment
106  *            variable settings in the format name=value
107  * @param workingDir
108  *            the working directory of the new process, or null if the new
109  *            process should inherit the working directory of the caller
110  *
111  * @exception IOException
112  *  if the program cannot be executed
113  * @exception   SecurityException
114  *  if the current SecurityManager disallows program execution
115  *
116  * @since 3.6
117  */
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);
120 }
121
122 private static ResourceBundle msgs = null;
123
124 /**
125  * Returns the NLS'ed message for the given argument. This is only being
126  * called from SWT.
127  *
128  * @param key the key to look up
129  * @return the message for the given key
130  *
131  * @see SWT#getMessage(String)
132  */
133 public static String getMessage(String key) {
134         String answer = key;
135
136         if (key == null) {
137                 SWT.error (SWT.ERROR_NULL_ARGUMENT);
138         }
139         if (msgs == null) {
140                 try {
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$
144                 }
145         }
146         if (msgs != null) {
147                 try {
148                         answer = msgs.getString(key);
149                 } catch (MissingResourceException ex2) {}
150         }
151         return answer;
152 }
153
154 public static String getMessage(String key, Object[] args) {
155         String answer = key;
156
157         if (key == null || args == null) {
158                 SWT.error (SWT.ERROR_NULL_ARGUMENT);
159         }
160         if (msgs == null) {
161                 try {
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$
165                 }
166         }
167         if (msgs != null) {
168                 try {
169                         MessageFormat formatter = new MessageFormat("");
170                         formatter.applyPattern(msgs.getString(key));
171                         answer = formatter.format(args);
172                 } catch (MissingResourceException ex2) {}
173         }
174         return answer;
175 }
176
177 }