comparison graal/com.oracle.max.base/src/com/sun/max/Utils.java @ 3733:e233f5660da4

Added Java files from Maxine project.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 17 Dec 2011 19:59:18 +0100
parents
children bc8527f3071c
comparison
equal deleted inserted replaced
3732:3e2e8b8abdaf 3733:e233f5660da4
1 /*
2 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.sun.max;
24
25 import java.io.*;
26 import java.util.*;
27
28 import com.sun.max.program.*;
29
30 /**
31 * Miscellaneous utility methods.
32 */
33 public final class Utils {
34
35 private Utils() {
36 }
37
38 /**
39 * Gets the first element in a list.
40 */
41 public static <T> T first(List<T> list) {
42 if (list instanceof Queue) {
43 Queue queue = (Queue) list;
44 Class<T> type = null;
45 T t = cast(type, queue.peek());
46 return t;
47 }
48 return list.get(0);
49 }
50
51 /**
52 * Gets the last element in a list.
53 */
54 public static <T> T last(List<T> list) {
55 if (list instanceof Deque) {
56 Deque deque = (Deque) list;
57 Class<T> type = null;
58 T t = cast(type, deque.getLast());
59 return t;
60 }
61 return list.get(list.size() - 1);
62 }
63
64 /**
65 * Creates an array of a generic type.
66 *
67 * @param <T> the type of the array elements
68 * @param length the length of the array to create
69 * @return a generic array of type {@code <T>} with a length of {@code length}
70 */
71 public static <T> T[] newArray(int length) {
72 return cast(new Object[length]);
73 }
74
75 /**
76 * Creates an array of a generic type.
77 *
78 * @param <T> the type of the array elements
79 * @param type explicit class of {@code <T[]>} needed due to javac bug (see {@link #cast(Class, Object)})
80 * @param length the length of the array to create
81 * @return a generic array of type {@code <T>} with a length of {@code length}
82 */
83 public static <T> T[] newArray(Class<T[]> type, int length) {
84 return cast(type, new Object[length]);
85 }
86
87 /**
88 * Adds a set of key-value pairs to a given map.
89 *
90 * @param <K>
91 * @param <V>
92 * @param map
93 * @param keyValuePairs
94 * @return
95 */
96 public static <K, V> Map<K, V> addEntries(Map<K, V> map, Object... keyValuePairs) {
97 assert keyValuePairs.length % 2 == 0;
98 for (int i = 0; i < keyValuePairs.length; i += 2) {
99 Object key = keyValuePairs[i];
100 Object value = keyValuePairs[i + 1];
101 Class<K> keyType = null;
102 Class<V> valueType = null;
103 map.put(cast(keyType, key), cast(valueType, value));
104 }
105 return map;
106 }
107
108 /**
109 * Creates an array of a generic type and {@linkplain Arrays#asList(Object...) wraps} it with
110 * a fixed-size list.
111 *
112 * @param <T> the type of the array elements
113 * @param length the length of the array to create
114 * @return a fixed-size list backed by a new generic array of type {@code <T>} with a length of {@code length}
115 */
116 public static <T> List<T> newArrayAsList(int length) {
117 Class<T[]> type = null;
118 T[] array = newArray(type, length);
119 return Arrays.asList(array);
120 }
121
122 /**
123 * Returns the index in {@code list} of the first occurrence identical to {@code value}, or -1 if
124 * {@code list} does not contain {@code value}. More formally, returns the lowest index
125 * {@code i} such that {@code (list.get(i) == value)}, or -1 if there is no such index.
126 */
127 public static int indexOfIdentical(List list, Object value) {
128 int i = 0;
129 for (Object element : list) {
130 if (element == value) {
131 return i;
132 }
133 ++i;
134 }
135 return -1;
136 }
137
138 /**
139 * Returns the index in {@code array} of the first occurrence identical to {@code value}, or -1 if
140 * {@code array} does not contain {@code value}. More formally, returns the lowest index
141 * {@code i} such that {@code (array[i] == value)}, or -1 if there is no such index.
142 */
143 public static int indexOfIdentical(Object[] array, Object value) {
144 for (int i = 0; i < array.length; i++) {
145 if (array[i] == value) {
146 return i;
147 }
148 }
149 return -1;
150 }
151
152 /**
153 * Statically cast an object to an arbitrary type. The cast is still dynamically checked.
154 *
155 * This alternate version of {@link #cast(Object)} is required due to bug
156 * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6302954">6302954</a> in javac.
157 *
158 * The pattern for calling this method is:
159 * <pre>
160 * Foo foo = ...;
161 * Class<Foo<T>> type = null;
162 * Foo<T> genericFoo = uncheckedCast(type, foo);
163 * </pre>
164 *
165 */
166 @SuppressWarnings("unchecked")
167 public static <T> T cast(Class<T> type, Object object) {
168 return (T) object;
169 }
170
171 /**
172 * Statically cast an object to an arbitrary type. The cast is still dynamically checked.
173 */
174 @SuppressWarnings("unchecked")
175 public static <T> T cast(Object object) {
176 return (T) object;
177 }
178
179 /**
180 * Concatenates two arrays.
181 *
182 * @param <T> the type of elements in the source arrays
183 * @param head the prefix of the result array
184 * @param tail the elements to be concatenated to {@code head}
185 * @return the result of concatenating {@code tail} to the end of {@code head}
186 */
187 public static <T> T[] concat(T[] head, T... tail) {
188 T[] result = Arrays.copyOf(head, head.length + tail.length);
189 System.arraycopy(tail, 0, result, head.length, tail.length);
190 return result;
191 }
192
193 /**
194 * Concatenates two arrays.
195 *
196 * @param <T> the type of elements in the source arrays
197 * @param tail the elements to be concatenated to the end of {@code head}
198 * @param head the prefix of the result array
199 * @return the result of concatenating {@code tail} to the end of {@code head}
200 */
201 public static <T> T[] prepend(T[] tail, T... head) {
202 return concat(head, tail);
203 }
204
205 /**
206 * Returns a string representation of the contents of the specified array.
207 * If the array contains other arrays as elements, they are converted to
208 * strings by the {@link Object#toString} method inherited from
209 * <tt>Object</tt>, which describes their <i>identities</i> rather than
210 * their contents.
211 *
212 * @param array the array whose string representation to return
213 * @param separator the separator to use
214 * @return a string representation of <tt>array</tt>
215 * @throws NullPointerException if {@code array} or {@code separator} is null
216 */
217 public static <T> String toString(T[] array, String separator) {
218 if (array == null || separator == null) {
219 throw new NullPointerException();
220 }
221 if (array.length == 0) {
222 return "";
223 }
224
225 final StringBuilder builder = new StringBuilder();
226 builder.append(array[0]);
227
228 for (int i = 1; i < array.length; i++) {
229 builder.append(separator);
230 builder.append(array[i]);
231 }
232
233 return builder.toString();
234 }
235
236 /**
237 * Tests a given exception to see if it is an instance of a given exception type, casting and throwing it if so.
238 * Otherwise if the exception is an unchecked exception (i.e. an instance of a {@link RuntimeException} or
239 * {@link Error}) then it is cast to the appropriate unchecked exception type and thrown. Otherwise, it is wrapped
240 * in a {@link ProgramError} and thrown.
241 *
242 * This method declares a return type simply so that a call to this method can be the expression to a throw
243 * instruction.
244 */
245 public static <T extends Throwable> T cast(Class<T> exceptionType, Throwable exception) throws T {
246 if (exceptionType.isInstance(exception)) {
247 throw exceptionType.cast(exception);
248 }
249 if (exception instanceof Error) {
250 throw (Error) exception;
251 }
252 if (exception instanceof RuntimeException) {
253 throw (RuntimeException) exception;
254 }
255 throw ProgramError.unexpected(exception);
256 }
257
258 /**
259 * Gets the stack trace for a given exception as a string.
260 */
261 public static String stackTraceAsString(Throwable throwable) {
262 final StringWriter stringWriter = new StringWriter();
263 throwable.printStackTrace(new PrintWriter(stringWriter));
264 return stringWriter.getBuffer().toString();
265 }
266 }