package st.ata.util; import java.io.PrintWriter; import java.io.StringWriter; import java.lang.reflect.UndeclaredThrowableException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.logging.Level; import java.util.logging.Logger; // Tested by TestX.java /** A collection of useful static methods. */ @SuppressWarnings("unchecked") public final class X { public static final int DEBUG = 2; /** Throws a runtime exception with message m. */ public static void fail(String m) { RuntimeException e = new RuntimeException(m); popTrace(e, 1); throw e; } /** * Throws a runtime exception with message systemProperty. * @param systemProperty a String value which specifies * a boolean system property, which if true will cause * an exception to be thrown. */ public static void testFailure(String systemProperty) { if (!Boolean.getBoolean(systemProperty)) { return; } RuntimeException e = new RuntimeException(systemProperty); popTrace(e, 1); throw e; } /** Throws a runtime exception if b is not true. */ public static void check(boolean b) { if (b) return; RuntimeException e = new RuntimeException("assertion failure"); popTrace(e, 1); throw e; } /** Throws a runtime exception if b is not true. */ public static void check(boolean b, String m) { if (b) return; RuntimeException e = new RuntimeException(m); popTrace(e, 1); throw e; } /** Throws an illegal argument exception if b is not true. */ public static void checkargs(boolean b) { if (b) return; RuntimeException e = new IllegalArgumentException(); popTrace(e, 1); throw e; } /** Throws an illegal state exception if b is not true. */ public static void checkstate(boolean b) { if (b) return; RuntimeException e = new IllegalStateException(); popTrace(e, 1); throw e; } /** * Returns an {@link UndeclaredThrowableException} * wrapping e. */ public static RuntimeException toRTE(Exception e) { RuntimeException rte = new UndeclaredThrowableException(e); popTrace(rte, 1); return rte; } /** Same as ut(b, ""). */ public static void ut(boolean b) { ut(b, ""); } /** Test condition during unit testing. If b is true, does nothing. If b is not true, prints (to System.out) "Unit test failure: " + m and a stack trace then returns. */ public static void ut(boolean b, String m) { if (! b) { try { if (b) return; RuntimeException e = new RuntimeException("Unit test failure: " + m); popTrace(e, 1); throw e; } catch (RuntimeException e) { System.out.println(""); e.printStackTrace(System.out); } } } /** * print out the programName and arguments used */ public static void printArgs(String programName, String[] args) { System.out.print(programName); for (int i=0; i System.out.print(" "); System.out.print(args[i]); } } public static void noimpl() { RuntimeException e = new RuntimeException("Not implemented yet."); popTrace(e, 1); throw e; } /** Returns a full description of a {@link Throwable}. This full * description includes a stack trace. This method will never * throw an error or exception: if something bad happens, it * simply returns null. */ public static String getFullDescription(Throwable t) { try { StringWriter sw = new StringWriter(); PrintWriter o = new PrintWriter(sw); t.printStackTrace(o); o.flush(); return sw.toString(); } catch (Throwable ignore) { return null; } } /** Removes the top n stack-trace elements from * t. This is useful inside methods like {@link * #fail} to help debuggers more quickly identify the location of * a failure. */ public static void popTrace(Throwable t, int n) { /*StackTraceElement[] ot = t.getStackTrace(); int len = ot.length - n; StackTraceElement[] nt = new StackTraceElement[len]; System.arraycopy(ot, n, nt, 0, len); t.setStackTrace(nt);*/ } public static int decodeInt(byte[] buf, int offset) { return ((buf[offset+3]&0xff) | (buf[offset+2]&0xff) | (buf[offset+1]&0xff) | (buf[offset]&0xff)); } public static int decodeShort(byte[] buf, int offset) { return ((buf[offset+1]&0xff) | (buf[offset]&0xff)); } public static void encodeShort(byte[] buf, int offset, int val) { X.check(val=Short.MIN_VALUE); buf[offset++] = (byte)val; val >>= 8; buf[offset] = (byte)val; } public static void encodeInt(byte[] buf, int offset, int val) { buf[offset++] = (byte)val; val >>= 8; buf[offset++] = (byte)val; val >>= 8; buf[offset++] = (byte)val; val >>= 8; buf[offset] = (byte)val; } public static long decodeLong(byte[] buf, int offset) { long lo = decodeInt(buf, offset) & 0xffffffffL; long hi = decodeInt(buf, offset+4); return (hi } public static void encodeLong(byte[] buf, int offset, long val) { buf[offset++] = (byte)val; val >>= 8; buf[offset++] = (byte)val; val >>= 8; buf[offset++] = (byte)val; val >>= 8; buf[offset++] = (byte)val; val >>= 8; buf[offset++] = (byte)val; val >>= 8; buf[offset++] = (byte)val; val >>= 8; buf[offset++] = (byte)val; val >>= 8; buf[offset] = (byte)val; } /** * returns the printable representation of data * after escaping non-printable characters in C style. */ public static String printable(byte[] data) { return printable(data, 0, data.length); } /** * returns the printable representation of data * from start (inclusive) to end (exclusive). * after escaping non-printable characters in C style. * data may not be null and * start must be smaller or equal to end * Both start and end are bounded by * 0 and data.length bot inclusive. */ public static String printable(byte[] data, int start, int end) { checkargs(data != null); checkargs(start checkargs(start >= 0); checkargs(end StringBuffer sb = new StringBuffer(); for (int i = start; i < end; i++) { final byte b = data[i]; if (b < 0x20 || b > 0x7e) { switch (b) { case '\r': sb.append("\\r"); break; case '\n': sb.append("\\n"); break; case '\t': sb.append("\\t"); break; case '\b': sb.append("\\b"); break; case '\f': sb.append("\\f"); break; case '\\': sb.append("\\\\"); break; default: sb.append('\\'); sb.append(b >>> 6); sb.append((b >>> 3) & 0x07); sb.append(b & 0x07); break; } } else { sb.append((char)b); } } return sb.toString(); } public static void log(String ctxt, Level level, String msg, Throwable t) { Logger.getLogger("st."+ctxt).log(level, ctxt+": "+msg, t); } public static void log(String ctxt, Level level, String msg) { Logger.getLogger("st."+ctxt).log(level, ctxt+": "+msg); } public static ArrayList dupElim(ArrayList al, Comparator cm) { if (al.size() return al; Collections.sort(al, cm); Object prev = al.get(0); ArrayList n = new ArrayList(); n.add(prev); for (int i = 1; i < al.size(); i++) { if (!prev.equals(al.get(i))) n.add(al.get(i)); prev = al.get(i); } return n; } }