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 java.util.concurrent.locks.*;
import java.util.function.*;
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 java.awt.geom.*;
import javax.imageio.*;
import java.math.*;
import static x30_pkg.x30_util.DynamicObject;
import java.awt.geom.*;
import java.awt.datatransfer.StringSelection;
import java.text.NumberFormat;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
class main {
static class HashedBWImage implements IBWImage, MakesBWImage {
BWImage image; // don't change this afterwards...
long hash;
boolean hashComputed = false;
HashedBWImage(BufferedImage image) { this(bwImage(image)); }
HashedBWImage(BWImage image) {
this.image = image; assertNotNull(image); }
long hash() {
while (!hashComputed) {
synchronized(this) {
if (!hashComputed) {
hash = hashBWImage(image);
hashComputed = true;
}
}
}
return hash;
}
public boolean equals(Object o) { if (o instanceof HashedBWImage) {
if (hash() != ((HashedBWImage) o).hash()) return false;
if (!bwImagesIdentical(image, ((HashedBWImage) o).image)) {
reportHashCollision(this, ((HashedBWImage) o));
return false;
}
return true;
} return false; }
public int hashCode() { return (int) hash(); }
final public BWImage getBWImage(){ return get(); }
public BWImage get() { return image; }
public int getWidth() { return image.getWidth(); }
public int getHeight() { return image.getHeight(); }
public BufferedImage getBufferedImage() { return image.getBufferedImage(); }
// check that BWImage wasn't modified
void verify() {
if (hash != 0)
assertEquals(hash, hashBWImage(image));
}
String classNameForPrinting() { return cnfp(this, HashedBWImage.class, "HashedBWImage"); }
public String toString() { return formatFunctionCall(classNameForPrinting(),
w() + "*" + h() + "px", "hash " + longToHex(hash())); }
// implement IBWImage
public float getFloatPixel(int x, int y) { return image.getFloatPixel(x, y); }
public int getInt(int x, int y) { return image.getInt(x, y); }
}
static BWImage bwImage(RGBImage img) {
return bwImage(img);
}
static BWImage bwImage(BufferedImage img) {
return toBWImage(img);
}
static BWImage bwImage(MakesBufferedImage img) {
return toBWImage(img);
}
static A assertNotNull(A a) {
assertTrue(a != null);
return a;
}
static A assertNotNull(String msg, A a) {
assertTrue(msg, a != null);
return a;
}
static long hashBWImage(BWImage img) {
return new HashBWImage(img).get();
}
static int hash(Object... l) {
return hashAboutObjects(l);
}
static boolean bwImagesIdentical(BWImage img1, BWImage img2) {
if (img1 == img2) return true;
if (img1 == null) return img2 == null;
if (img2 == null) return false;
int w = img1.getWidth(), h = img1.getHeight();
if (w != img2.getWidth() || h != img2.getHeight()) return false;
byte[] pixels1 = img1.pixels, pixels2 = img2.pixels;
return Arrays.equals(pixels1, pixels2);
}
static long hashCollisions;
static void reportHashCollision(Object... data) {
printVars("Hash collision! " + shorten(joinWithComma(data)));
++hashCollisions;
}
// get purpose 1: access a list/array/map (safer version of x.get(y))
static A get(List l, int idx) {
return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}
// seems to conflict with other signatures
/*static B get(Map map, A key) {
ret map != null ? map.get(key) : null;
}*/
static A get(A[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : null;
}
// default to false
static boolean get(boolean[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : false;
}
// get purpose 2: access a field by reflection or a map
static Object get(Object o, String field) {
try {
if (o == null) return null;
if (o instanceof Class) return get((Class) o, field);
if (o instanceof Map)
return ((Map) o).get(field);
Field f = getOpt_findField(o.getClass(), field);
if (f != null) {
makeAccessible(f);
return f.get(o);
}
if (o instanceof DynamicObject)
return getOptDynOnly(((DynamicObject) o), field);
} catch (Exception e) {
throw asRuntimeException(e);
}
throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}
static Object get_raw(String field, Object o) {
return get_raw(o, field);
}
static Object get_raw(Object o, String field) { try {
if (o == null) return null;
Field f = get_findField(o.getClass(), field);
makeAccessible(f);
return f.get(o);
} catch (Exception __e) { throw rethrow(__e); } }
static Object get(Class c, String field) {
try {
Field f = get_findStaticField(c, field);
makeAccessible(f);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field get_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
static Field get_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static Object get(String field, Object o) {
return get(o, field);
}
static boolean get(BitSet bs, int idx) {
return bs != null && bs.get(idx);
}
static A assertEquals(Object x, A y) {
return assertEquals("", x, y);
}
static A assertEquals(String msg, Object x, A y) {
if (assertVerbose()) return assertEqualsVerbose(msg, x, y);
if (!(x == null ? y == null : x.equals(y)))
throw fail((msg != null ? msg + ": " : "") + y + " != " + x);
return y;
}
static String cnfp(Object o, Class c, String className) {
return classNameForPrinting(o, c, className);
}
// binary legacy syntax
static String formatFunctionCall(String fname, Object... args) {
return formatFunctionCall((Object) fname, args);
}
static String formatFunctionCall(Object fname, Object... args) {
return fname + "(" + joinWithComma(allToString(args)) + ")";
}
static String formatFunctionCall(String fname, Iterable args) {
return formatFunctionCall((Object) fname, args);
}
static String formatFunctionCall(Object fname, Iterable args) {
return formatFunctionCall(fname, toObjectArray(args));
}
static String classNameForPrinting(Object o, Class c, String className) {
return (o.getClass() != c ? "subclass of " : "") + className;
}
static String longToHex(long l) {
return bytesToHex(longToBytes(l));
}
static BWImage toBWImage(RGBImage img) {
return toBW(img);
}
static BWImage toBWImage(BufferedImage img) {
return img == null ? null : new BWImage(img);
}
static BWImage toBWImage(MakesBufferedImage img) {
if (img == null) return null;
if (img instanceof BWImage) return ((BWImage) img);
return new BWImage(toBufferedImage(img));
}
static void assertTrue(Object o) {
if (!(eq(o, true) /*|| isTrue(pcallF(o))*/))
throw fail(str(o));
}
static boolean assertTrue(String msg, boolean b) {
if (!b)
throw fail(msg);
return b;
}
static boolean assertTrue(boolean b) {
if (!b)
throw fail("oops");
return b;
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static boolean printAlsoToSystemOut = true;
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal