1 2 package st.ata.util ; 3 4 import java.io.PrintWriter ; 5 import java.io.StringWriter ; 6 import java.lang.reflect.UndeclaredThrowableException ; 7 import java.util.ArrayList ; 8 import java.util.Collections ; 9 import java.util.Comparator ; 10 import java.util.logging.Level ; 11 import java.util.logging.Logger ; 12 13 15 16 @SuppressWarnings ("unchecked") 17 public final class X { 18 public static final int DEBUG = 2; 19 20 21 public static void fail(String m) { 22 RuntimeException e = new RuntimeException (m); 23 popTrace(e, 1); 24 throw e; 25 } 26 27 33 public static void testFailure(String systemProperty) { 34 if (!Boolean.getBoolean(systemProperty)) { 35 return; 36 } 37 38 RuntimeException e = new RuntimeException (systemProperty); 39 popTrace(e, 1); 40 throw e; 41 } 42 43 44 public static void check(boolean b) { 45 if (b) return; 46 RuntimeException e = new RuntimeException ("assertion failure"); 47 popTrace(e, 1); 48 throw e; 49 } 50 51 52 public static void check(boolean b, String m) { 53 if (b) return; 54 RuntimeException e = new RuntimeException (m); 55 popTrace(e, 1); 56 throw e; 57 } 58 59 60 public static void checkargs(boolean b) { 61 if (b) return; 62 RuntimeException e = new IllegalArgumentException (); 63 popTrace(e, 1); 64 throw e; 65 } 66 67 68 public static void checkstate(boolean b) { 69 if (b) return; 70 RuntimeException e = new IllegalStateException (); 71 popTrace(e, 1); 72 throw e; 73 } 74 75 79 80 public static RuntimeException toRTE(Exception e) { 81 RuntimeException rte = new UndeclaredThrowableException (e); 82 popTrace(rte, 1); 83 return rte; 84 } 85 86 87 public static void ut(boolean b) { ut(b, ""); } 88 89 94 public static void ut(boolean b, String m) { 95 if (! b) { 96 try { 97 if (b) return; 98 RuntimeException e 99 = new RuntimeException ("Unit test failure: " + m); 100 popTrace(e, 1); 101 throw e; 102 } catch (RuntimeException e) { 103 System.out.println(""); 104 e.printStackTrace(System.out); 105 } 106 } 107 } 108 111 public static void printArgs(String programName, String [] 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 e = new RuntimeException ("Not implemented yet."); 120 popTrace(e, 1); 121 throw e; 122 } 123 124 128 public static String getFullDescription(Throwable t) { 129 try { 130 StringWriter sw = new StringWriter (); 131 PrintWriter o = new PrintWriter (sw); 132 t.printStackTrace(o); 133 o.flush(); 134 return sw.toString(); 135 } catch (Throwable ignore) { return null; } 136 } 137 138 142 public static void popTrace(Throwable t, int n) { 143 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 197 public static String printable(byte[] data) { 198 return printable(data, 0, data.length); 199 } 200 201 210 211 public static String 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 sb = new StringBuffer (); 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 ctxt, Level level, String msg, Throwable t) { 255 Logger.getLogger("st."+ctxt).log(level, ctxt+": "+msg, t); 256 } 257 public static void log(String ctxt, Level level, String msg) { 258 Logger.getLogger("st."+ctxt).log(level, ctxt+": "+msg); 259 } 260 public static ArrayList dupElim(ArrayList al, Comparator cm) { 261 if (al.size()<2) 262 return al; 263 Collections.sort(al, cm); 264 Object prev = al.get(0); 265 ArrayList n = new ArrayList (); 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 |