import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
public class main {
static boolean error;
static JFrame frame;
// returns false if done, otherwise calls whenDone later
static boolean action(String s, final Runnable whenDone) {
Matches m = new Matches();
if (match("Bot shows color *", s, m)) {
showColor(colorFromText(m.unq(0))); return false;
}
throw fail("unknown command: " + quote(s));
}
static void showColor(Color color) {
if (frame == null)
frame = showCenterFrame("Color Bot!", 200, 200);
setFrameContents(frame, singleColorPanel(color));
}
static RuntimeException fail() {
throw new RuntimeException("fail");
}
static RuntimeException fail(Object msg) {
throw new RuntimeException(String.valueOf(msg));
}
static RuntimeException fail(String msg) {
throw new RuntimeException(unnull(msg));
}
static RuntimeException fail(String msg, Object... args) {
throw new RuntimeException(format(msg, args));
}
static Color colorFromText(String s) {
return (Color) getOptIgnoreCase(Color.class, s);
}
static String quote(String s) {
if (s == null) return "null";
return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\r", "\\r").replace("\n", "\\n") + "\"";
}
static String quote(long l) {
return quote("" + l);
}
static String quote(char c) {
return quote("" + c);
}
static JComponent singleColorPanel(Color color) {
JPanel p = new JPanel();
p.setOpaque(true);
p.setBackground(color);
return p;
}
static void setFrameContents(JFrame frame, Component contents) {
frame.getContentPane().removeAll();
frame.getContentPane().add(contents);
revalidate(frame);
}
static JFrame showCenterFrame(String title, int w, int h) {
return showCenterFrame(title, w, h, null);
}
static JFrame showCenterFrame(String title, int w, int h, Component content) {
JFrame frame = makeFrame(title, content);
frame.setSize(w, h);
centerFrame(frame);
return frame;
}
static Object getOptIgnoreCase(Object o, String field) {
if (o instanceof String) o = getBot((String) o);
if (o == null) return null;
if (o instanceof Class) return getOptIgnoreCase((Class) o, field);
if (o.getClass().getName().equals("main$DynamicObject"))
fail("not implemented");
if (o instanceof Map) return lookupIgnoreCase((Map) o, field);
return getOptIgnoreCase_raw(o, field);
}
static Object getOptIgnoreCase_raw(Object o, String field) {
try {
Field f = getOptIgnoreCase_findField(o.getClass(), field);
if (f == null) return null;
f.setAccessible(true);
return f.get(o);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Object getOptIgnoreCase(Class c, String field) {
try {
Field f = getOptIgnoreCase_findStaticField(c, field);
if (f == null) return null;
f.setAccessible(true);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field getOptIgnoreCase_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (eqic(f.getName(), field) && (f.getModifiers() & Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static Field getOptIgnoreCase_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (eqic(f.getName(), field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static void centerFrame(JFrame frame) {
frame.setLocationRelativeTo(null); // magic trick
}
static void revalidate(JFrame frame) {
// magic combo to actually relayout and repaint
frame.revalidate();
frame.repaint();
}
static JFrame makeFrame() {
return makeFrame((Component) null);
}
static JFrame makeFrame(Object content) {
// TODO: get program ID
return makeFrame("A JavaX Frame", content);
}
static JFrame makeFrame(String title) {
return makeFrame(title, null);
}
static JFrame makeFrame(String title, Object content) {
JFrame frame = new JFrame(title);
if (content != null)
frame.getContentPane().add(wrap(content));
frame.setBounds(300, 100, 500, 400);
frame.setVisible(true);
//exitOnFrameClose(frame);
return frame;
}
static String unnull(String s) {
return s == null ? "" : s;
}
static List unnull(List l) {
return l == null ? emptyList() : l;
}
static String format(String pat, Object... args) {
return format3(pat, args);
}
static String format3(String pat, Object... args) {
if (args.length == 0) return pat;
List tok = javaTokPlusPeriod(pat);
int argidx = 0;
for (int i = 1; i < tok.size(); i += 2)
if (tok.get(i).equals("*"))
tok.set(i, format3_formatArg(argidx < args.length ? args[argidx++] : "null"));
return join(tok);
}
static String format3_formatArg(Object arg) {
if (arg == null) return "null";
if (arg instanceof String) {
String s = (String) arg;
return isIdentifier(s) || isNonNegativeInteger(s) ? s : quote(s);
}
if (arg instanceof Integer || arg instanceof Long) return String.valueOf(arg);
return quote(structure(arg));
}
static boolean eqic(String a, String b) {
if ((a == null) != (b == null)) return false;
if (a == null) return true;
return a.equalsIgnoreCase(b);
}
static List emptyList() {
return new ArrayList();
//ret Collections.emptyList();
}
static Object getBot(String botID) {
return callOpt(getMainBot(), "getBot", botID);
}
static A lookupIgnoreCase(Map map, String key) {
for (String s : map.keySet())
if (eqic(s, key))
return map.get(s); return null;
}
static List lookupIgnoreCase(MultiMap map, String key) {
for (String s : map.keySet())
if (eqic(s, key))
return map.get(s);
return litlist();
}
// c = Component or something implementing swing()
static Component wrap(Object swingable) {
Component c = (Component) ( swingable instanceof Component ? swingable : call(swingable, "swing"));
if (c instanceof JTable || c instanceof JList || c instanceof JTextArea)
return new JScrollPane(c);
return c;
}
static boolean isIdentifier(String s) {
return isJavaIdentifier(s);
}
static Object callOpt(Object o, String method, Object... args) {
try {
if (o == null) return null;
if (o instanceof Class) {
Method m = callOpt_findStaticMethod((Class) o, method, args, false);
if (m == null) return null;
m.setAccessible(true);
return m.invoke(null, args);
} else {
Method m = callOpt_findMethod(o, method, args, false);
if (m == null) return null;
m.setAccessible(true);
return m.invoke(o, args);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Method callOpt_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
Class _c = c;
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (!m.getName().equals(method)) {
if (debug) System.out.println("Method name mismatch: " + method);
continue;
}
if ((m.getModifiers() & Modifier.STATIC) == 0 || !callOpt_checkArgs(m, args, debug))
continue;
return m;
}
c = c.getSuperclass();
}
return null;
}
static Method callOpt_findMethod(Object o, String method, Object[] args, boolean debug) {
Class c = o.getClass();
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (m.getName().equals(method) && callOpt_checkArgs(m, args, debug))
return m;
}
c = c.getSuperclass();
}
return null;
}
private static boolean callOpt_checkArgs(Method m, Object[] args, boolean debug) {
Class>[] types = m.getParameterTypes();
if (types.length != args.length) {
if (debug)
System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
return false;
}
for (int i = 0; i < types.length; i++)
if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
if (debug)
System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
return false;
}
return true;
}
static Object mainBot;
static Object getMainBot() {
return mainBot;
}
static ArrayList litlist(A... a) {
return new ArrayList(Arrays.asList(a));
}
public static String join(String glue, Iterable strings) {
StringBuilder buf = new StringBuilder();
Iterator i = strings.iterator();
if (i.hasNext()) {
buf.append(i.next());
while (i.hasNext())
buf.append(glue).append(i.next());
}
return buf.toString();
}
public static String join(String glue, String[] strings) {
return join(glue, Arrays.asList(strings));
}
public static String join(Iterable strings) {
return join("", strings);
}
public static String join(String[] strings) {
return join("", strings);
}
static boolean isNonNegativeInteger(String s) {
return s != null && Pattern.matches("\\d+", s);
}
static String structure(Object o) {
HashSet refd = new HashSet();
return structure_2(structure_1(o, 0, new IdentityHashMap(), refd), refd);
}
// leave to false, unless unstructure() breaks
static boolean structure_allowShortening = false;
static String structure_1(Object o, int stringSizeLimit, IdentityHashMap