KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > st > ata > util > X


1
2 package st.ata.util JavaDoc;
3
4 import java.io.PrintWriter JavaDoc;
5 import java.io.StringWriter JavaDoc;
6 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.util.Comparator JavaDoc;
10 import java.util.logging.Level JavaDoc;
11 import java.util.logging.Logger JavaDoc;
12
13 // Tested by TestX.java
14

15 /** A collection of useful static methods. */
16 @SuppressWarnings JavaDoc("unchecked")
17 public final class X {
18     public static final int DEBUG = 2;
19
20     /** Throws a runtime exception with message <code>m</code>. */
21     public static void fail(String JavaDoc m) {
22         RuntimeException JavaDoc e = new RuntimeException JavaDoc(m);
23         popTrace(e, 1);
24         throw e;
25     }
26
27     /**
28      * Throws a runtime exception with message <code>systemProperty</code>.
29      * @param systemProperty a <code>String</code> value which specifies
30      * a boolean system property, which if true will cause
31      * an exception to be thrown.
32      */

33     public static void testFailure(String JavaDoc systemProperty) {
34         if (!Boolean.getBoolean(systemProperty)) {
35             return;
36         }
37
38         RuntimeException JavaDoc e = new RuntimeException JavaDoc(systemProperty);
39         popTrace(e, 1);
40         throw e;
41     }
42
43     /** Throws a runtime exception if <code>b</code> is not true. */
44     public static void check(boolean b) {
45         if (b) return;
46         RuntimeException JavaDoc e = new RuntimeException JavaDoc("assertion failure");
47         popTrace(e, 1);
48          throw e;
49     }
50
51     /** Throws a runtime exception if <code>b</code> is not true. */
52     public static void check(boolean b, String JavaDoc m) {
53         if (b) return;
54         RuntimeException JavaDoc e = new RuntimeException JavaDoc(m);
55         popTrace(e, 1);
56         throw e;
57     }
58
59     /** Throws an illegal argument exception if <code>b</code> is not true. */
60     public static void checkargs(boolean b) {
61         if (b) return;
62         RuntimeException JavaDoc e = new IllegalArgumentException JavaDoc();
63         popTrace(e, 1);
64         throw e;
65     }
66
67     /** Throws an illegal state exception if <code>b</code> is not true. */
68     public static void checkstate(boolean b) {
69         if (b) return;
70         RuntimeException JavaDoc e = new IllegalStateException JavaDoc();
71         popTrace(e, 1);
72         throw e;
73     }
74
75     /**
76      * Returns an {@link UndeclaredThrowableException}
77      * wrapping <code>e</code>.
78      */

79
80     public static RuntimeException JavaDoc toRTE(Exception JavaDoc e) {
81         RuntimeException JavaDoc rte = new UndeclaredThrowableException JavaDoc(e);
82         popTrace(rte, 1);
83         return rte;
84     }
85
86     /** Same as <c>ut(b,&nbsp;"")</c>. */
87     public static void ut(boolean b) { ut(b, ""); }
88
89     /** Test condition during unit testing. If <c>b</c> is true, does
90     nothing. If <c>b</c> is not true, prints (to
91     <c>System.out</c>)
92     <c>"Unit&nbsp;test&nbsp;failure:&nbsp;"&nbsp;+&nbsp;m</c> and
93     a stack trace then returns. */

94     public static void ut(boolean b, String JavaDoc m) {
95     if (! b) {
96         try {
97                 if (b) return;
98                 RuntimeException JavaDoc e
99                     = new RuntimeException JavaDoc("Unit test failure: " + m);
100                 popTrace(e, 1);
101                 throw e;
102         } catch (RuntimeException JavaDoc e) {
103         System.out.println("");
104         e.printStackTrace(System.out);
105         }
106     }
107     }
108     /**
109      * print out the programName and arguments used
110      */

111     public static void printArgs(String JavaDoc programName, String JavaDoc[] args) {
112         System.out.print(programName);
113         for (int i=0; i<args.length; i++){
114             System.out.print(" ");
115             System.out.print(args[i]);
116         }
117     }
118     public static void noimpl() {
119         RuntimeException JavaDoc e = new RuntimeException JavaDoc("Not implemented yet.");
120         popTrace(e, 1);
121         throw e;
122     }
123
124     /** Returns a full description of a {@link Throwable}. This full
125      * description includes a stack trace. This method will never
126      * throw an error or exception: if something bad happens, it
127      * simply returns null. */

128     public static String JavaDoc getFullDescription(Throwable JavaDoc t) {
129         try {
130             StringWriter JavaDoc sw = new StringWriter JavaDoc();
131             PrintWriter JavaDoc o = new PrintWriter JavaDoc(sw);
132             t.printStackTrace(o);
133             o.flush();
134             return sw.toString();
135         } catch (Throwable JavaDoc ignore) { return null; }
136     }
137
138     /** Removes the top <code>n</code> stack-trace elements from
139      * <code>t</code>. This is useful inside methods like {@link
140      * #fail} to help debuggers more quickly identify the location of
141      * a failure. */

142     public static void popTrace(Throwable JavaDoc t, int n) {
143         /*StackTraceElement[] ot = t.getStackTrace();
144         int len = ot.length - n;
145         StackTraceElement[] nt = new StackTraceElement[len];
146         System.arraycopy(ot, n, nt, 0, len);
147         t.setStackTrace(nt);*/

148     }
149
150     public static int decodeInt(byte[] buf, int offset) {
151         return ((buf[offset+3]&0xff)<<24
152                 | (buf[offset+2]&0xff)<<16
153                 | (buf[offset+1]&0xff)<<8
154                 | (buf[offset]&0xff));
155     }
156
157     public static int decodeShort(byte[] buf, int offset) {
158         return ((buf[offset+1]&0xff)<<8
159                 | (buf[offset]&0xff));
160     }
161
162     public static void encodeShort(byte[] buf, int offset, int val) {
163         X.check(val<=Short.MAX_VALUE && val>=Short.MIN_VALUE);
164         buf[offset++] = (byte)val; val >>= 8;
165         buf[offset] = (byte)val;
166     }
167
168     public static void encodeInt(byte[] buf, int offset, int val) {
169         buf[offset++] = (byte)val; val >>= 8;
170         buf[offset++] = (byte)val; val >>= 8;
171         buf[offset++] = (byte)val; val >>= 8;
172         buf[offset] = (byte)val;
173     }
174
175     public static long decodeLong(byte[] buf, int offset) {
176         long lo = decodeInt(buf, offset) & 0xffffffffL;
177         long hi = decodeInt(buf, offset+4);
178         return (hi<<32) | lo;
179     }
180
181     public static void encodeLong(byte[] buf, int offset, long val) {
182         buf[offset++] = (byte)val; val >>= 8;
183         buf[offset++] = (byte)val; val >>= 8;
184         buf[offset++] = (byte)val; val >>= 8;
185         buf[offset++] = (byte)val; val >>= 8;
186         buf[offset++] = (byte)val; val >>= 8;
187         buf[offset++] = (byte)val; val >>= 8;
188         buf[offset++] = (byte)val; val >>= 8;
189         buf[offset] = (byte)val;
190     }
191
192
193     /**
194      * returns the printable representation of <code>data</code>
195      * after escaping non-printable characters in C style.
196      */

197     public static String JavaDoc printable(byte[] data) {
198         return printable(data, 0, data.length);
199     }
200
201     /**
202      * returns the printable representation of <code>data</code>
203      * from <code>start</code> (inclusive) to <code>end</code> (exclusive).
204      * after escaping non-printable characters in C style.
205      * <code>data</code> may not be <code>null</code> and
206      * <code>start</code> must be smaller or equal to <code>end</code>
207      * Both <code>start</code> and <code>end</code> are bounded by
208      * <code>0</code> and <code>data.length</code> bot inclusive.
209      */

210
211     public static String JavaDoc printable(byte[] data, int start, int end) {
212         checkargs(data != null);
213         checkargs(start <= end);
214         checkargs(start >= 0);
215         checkargs(end <= data.length);
216         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
217
218         for (int i = start; i < end; i++) {
219             final byte b = data[i];
220             if (b < 0x20 || b > 0x7e) {
221                 switch (b) {
222                 case '\r':
223                     sb.append("\\r");
224                     break;
225                 case '\n':
226                     sb.append("\\n");
227                     break;
228                 case '\t':
229                     sb.append("\\t");
230                     break;
231                 case '\b':
232                     sb.append("\\b");
233                     break;
234                 case '\f':
235                     sb.append("\\f");
236                     break;
237                 case '\\':
238                     sb.append("\\\\");
239                     break;
240                 default:
241                     sb.append('\\');
242                     sb.append(b >>> 6);
243                     sb.append((b >>> 3) & 0x07);
244                     sb.append(b & 0x07);
245                     break;
246                 }
247             } else {
248                 sb.append((char)b);
249             }
250         }
251         return sb.toString();
252     }
253
254     public static void log(String JavaDoc ctxt, Level JavaDoc level, String JavaDoc msg, Throwable JavaDoc t) {
255         Logger.getLogger("st."+ctxt).log(level, ctxt+": "+msg, t);
256     }
257     public static void log(String JavaDoc ctxt, Level JavaDoc level, String JavaDoc msg) {
258         Logger.getLogger("st."+ctxt).log(level, ctxt+": "+msg);
259     }
260     public static ArrayList JavaDoc dupElim(ArrayList JavaDoc al, Comparator JavaDoc cm) {
261         if (al.size()<2)
262             return al;
263         Collections.sort(al, cm);
264         Object JavaDoc prev = al.get(0);
265         ArrayList JavaDoc n = new ArrayList JavaDoc();
266         n.add(prev);
267         for (int i = 1; i < al.size(); i++) {
268             if (!prev.equals(al.get(i)))
269                 n.add(al.get(i));
270             prev = al.get(i);
271         }
272         return n;
273     }
274
275 }
276
Popular Tags