import javax.imageio.*; import java.awt.image.*; import java.awt.event.*; import java.awt.*; import java.security.NoSuchAlgorithmException; import java.security.MessageDigest; import java.lang.reflect.*; import java.net.*; import java.io.*; import javax.swing.text.*; import javax.swing.event.*; import javax.swing.*; import java.util.concurrent.*; import java.util.regex.*; import java.util.List; import java.util.zip.*; import java.util.*; public class main { // variable assignments static class Ass { Map map = new TreeMap(); void assign(String var, Object value) { Object v = map.get(var); if (v == null) map.put(var, value); else if (!v.equals(value)) fail("mismatch"); } } static class Pred { } static class Assign extends Pred { String var; Object value; Assign(String var, Object value) { this.value = value; this.var = var;} } static class And extends Pred { List parts = new ArrayList(); And(Pred... parts) { this.parts = new ArrayList(Arrays.asList(parts)); } } static class Or extends Pred { List parts = new ArrayList(); Or(Pred... parts) { this.parts = new ArrayList(Arrays.asList(parts)); } } static void eval(Pred p, Ass ass) { if (p instanceof Assign) { Assign a = (Assign) ( p); ass.assign(a.var, a.value); } else if (p instanceof And) { And a = (And) ( p); for (Pred part : a.parts) eval(part, ass); } else fail(structure(p)); } static void shouldFail(Pred p) { try { eval(p, new Ass()); fail("did not fail"); } catch (RuntimeException e) { print("OK (expected fail): " + structure(p)); } } static List singleton(X x) { List list = new ArrayList(); list.add(x); return list; } static abstract class Iter { abstract Ass next(); } static class Singleton extends Iter { Ass value; Singleton(Ass value) { this.value = value;} Ass next() { if (value != null) { Ass v = value; value = null; return v; } return null; } } static class OrIterator extends Iter { Or o; int i; OrIterator(Or o) { this.o = o;} public Ass next() { if (i >= o.parts.size()) return null; Ass ass = new Ass(); eval(o.parts.get(i++), ass); return ass; } } static Iter multiEval(Pred p) { if (p instanceof Or) { Or o = (Or) ( p); return new OrIterator(o); } else { Ass ass = new Ass(); eval(p, ass); return new Singleton(ass); } } public static void main(String[] args) throws Exception { Ass ass = new Ass(); eval(new Assign("x", new Integer(1)), ass); print(structure(ass)); ass = new Ass(); eval(new And( new Assign("x", new Integer(1)), new Assign("y", new Integer(2))), ass); print(structure(ass)); shouldFail(new And( new Assign("x", new Integer(1)), new Assign("x", new Integer(2)))); // test multiEval List list = all(multiEval(new Assign("a", new Integer(5)))); assertEquals(1, list.size()); print(structure(list)); list = all(multiEval(new Or( new Assign("a", new Integer(5)), new Assign("a", new Integer(7))))); assertEquals(2, list.size()); print(structure(list)); } static List all(Iter it) { List l = new ArrayList(); while (true) { Ass ass = it.next(); if (ass != null) l.add(ass); else break; } return l; } static void assertEquals(Object x, Object y) { if (!(x == null ? y == null : x.equals(y))) fail(structure(x) + " != " + structure(y)); } static void print() { System.out.println(); } static void print(Object o) { System.out.println(o); } static void print(long i) { System.out.println(i); } static RuntimeException fail() { throw new RuntimeException("fail"); } static RuntimeException fail(String msg) { throw new RuntimeException(msg); } static String structure(Object o) { return structure(o, 0); } static String structure(Object o, int stringSizeLimit) { if (o == null) return "null"; String name = o.getClass().getName(); StringBuilder buf = new StringBuilder(); if (o instanceof Collection) { for (Object x : (Collection) o) { if (buf.length() != 0) buf.append(", "); buf.append(structure(x, stringSizeLimit)); } return "{" + buf + "}"; } if (o instanceof Map) { for (Object e : ((Map) o).entrySet()) { if (buf.length() != 0) buf.append(", "); buf.append(structure(((Map.Entry) e).getKey(), stringSizeLimit)); buf.append("="); buf.append(structure(((Map.Entry) e).getValue(), stringSizeLimit)); } return "{" + buf + "}"; } if (o.getClass().isArray()) { int n = Array.getLength(o); for (int i = 0; i < n; i++) { if (buf.length() != 0) buf.append(", "); buf.append(structure(Array.get(o, i), stringSizeLimit)); } return "{" + buf + "}"; } if (o instanceof String) return quote(stringSizeLimit != 0 ? shorten((String) o, stringSizeLimit) : (String) o); // Need more cases? This should cover all library classes... if (name.startsWith("java.") || name.startsWith("javax.")) return String.valueOf(o); String shortName = o.getClass().getName().replaceAll("^main\\$", ""); // TODO: go to superclasses too Field[] fields = o.getClass().getDeclaredFields(); int numFields = 0; String fieldName = ""; for (Field field : fields) { if ((field.getModifiers() & Modifier.STATIC) != 0) continue; Object value; try { value = field.get(o); } catch (Exception e) { value = "?"; } fieldName = field.getName(); // put special cases here... if (value != null) { if (buf.length() != 0) buf.append(", "); buf.append(fieldName + "=" + structure(value, stringSizeLimit)); } ++numFields; } String b = buf.toString(); if (numFields == 1) b = b.replaceAll("^" + fieldName + "=", ""); // drop field name if only one String s = shortName; if (buf.length() != 0) s += "(" + b + ")"; return s; } public static String quote(String s) { if (s == null) return "null"; return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\r", "\\r").replace("\n", "\\n") + "\""; } static String shorten(String s, int max) { return s.length() <= max ? s : s.substring(0, Math.min(s.length(), max)) + "..."; } }