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 java.time.Duration;
import java.text.*;
import java.util.TimeZone;
import java.awt.geom.*;
import static x30_pkg.x30_util.DynamicObject;
import java.text.NumberFormat;
class main {
static RunResultWithTimestamps runResultWithTimestamps_dontPrintStackTrace(IF0 f) {
return new RunResultWithTimestamps().run(f, false);
}
public static interface IF0 {
A get();
}
static class RunResultWithTimestamps extends OKOrError {
final public RunResultWithTimestamps setStarted(Timestamp started){ return started(started); }
public RunResultWithTimestamps started(Timestamp started) { this.started = started; return this; } final public Timestamp getStarted(){ return started(); }
public Timestamp started() { return started; }
Timestamp started;
final public RunResultWithTimestamps setEnded(Timestamp ended){ return ended(ended); }
public RunResultWithTimestamps ended(Timestamp ended) { this.ended = ended; return this; } final public Timestamp getEnded(){ return ended(); }
public Timestamp ended() { return ended; }
Timestamp ended;
final public RunResultWithTimestamps setPrintOutput(CharSequence printOutput){ return printOutput(printOutput); }
public RunResultWithTimestamps printOutput(CharSequence printOutput) { this.printOutput = printOutput; return this; } final public CharSequence getPrintOutput(){ return printOutput(); }
public CharSequence printOutput() { return printOutput; }
CharSequence printOutput;
RunResultWithTimestamps run(IF0 f) { return run(f, true); }
RunResultWithTimestamps run(IF0 f, boolean printStackTrace) {
return setPrintOutput(hijackPrint_tee(new Runnable() { public void run() { try {
started(tsNow());
copyFrom(okOrError(f, printStackTrace));
ended(tsNow());
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "started(tsNow());\r\n copyFrom(okOrError(f, printStackTrace));\r\n ende..."; }}));
}
boolean hasRun() { return ended != null; }
// legacy
OKOrError result() { return this; }
Duration duration() { return ended.minusAsDuration(started); }
String renderDuration() {
return formatElapsedTimeWithAppropriateUnit(nanosToSeconds(duration().toNanos()));
}
}
static class OKOrError implements IF0 {
A value;
final public Throwable getError(){ return error(); }
public Throwable error() { return error; }
Throwable error;
OKOrError() {}
OKOrError(A value) {
this.value = value;}
OKOrError(boolean dummy, Throwable error) {
this.error = error; assertNotNull(error); }
final boolean isOK(){ return ok(); }
boolean ok() { return error == null; }
boolean isError() { return error != null; }
public String toString() {
return ok()
? /*"OK: " +*/ str(value)
: "Error: " + str(error);
}
public A get() { return !ok() ? null : value; }
public A getMandatory() { if (!ok()) throw rethrow(error); return value; }
static OKOrError ok(A a) { return new OKOrError(a); }
static OKOrError error(Throwable error) { return new OKOrError(false, error); }
void setValue(A value) {
this.value = value;
error = null;
}
void setError(Throwable error) {
this.value = null;
this.error = error;
}
void copyFrom(OKOrError x) {
value = x.value;
error = x.error;
}
}
static class Timestamp implements Comparable , IFieldsToList{
long date;
Timestamp(long date) {
this.date = date;}
public boolean equals(Object o) {
if (!(o instanceof Timestamp)) return false;
Timestamp __1 = (Timestamp) o;
return date == __1.date;
}
public int hashCode() {
int h = 2059094262;
h = boostHashCombine(h, _hashCode(date));
return h;
}
public Object[] _fieldsToList() { return new Object[] {date}; }
Timestamp() { date = now(); }
long unixDate() { return date; }
public String toString() { return formatLocalDateWithSeconds(date); }
// Hmm. Should Timestamp(0) be equal to null? Question, questions...
public int compareTo(Timestamp t) {
return t == null ? 1 : cmp(date, t.date);
}
Timestamp plus(Seconds seconds) {
return plus(seconds == null ? null : seconds.getDouble());
}
final Timestamp plusSeconds(double seconds){ return plus(seconds); }
Timestamp plus(double seconds) {
return new Timestamp(date+toMS(seconds));
}
// returns milliseconds
long minus(Timestamp ts) {
return unixDate()-ts.unixDate();
}
long sysTime() {
return clockTimeToSystemTime(date);
}
Duration minusAsDuration(Timestamp ts) {
return Duration.ofMillis(minus(ts));
}
}
static interface IFieldsToList {
Object[] _fieldsToList();
}
static class Seconds implements Comparable , IFieldsToList{
double seconds;
Seconds() {}
Seconds(double seconds) {
this.seconds = seconds;}
public boolean equals(Object o) {
if (!(o instanceof Seconds)) return false;
Seconds __1 = (Seconds) o;
return seconds == __1.seconds;
}
public int hashCode() {
int h = -660217249;
h = boostHashCombine(h, _hashCode(seconds));
return h;
}
public Object[] _fieldsToList() { return new Object[] {seconds}; }
final double get(){ return seconds(); }
final double getDouble(){ return seconds(); }
double seconds() { return seconds; }
public String toString() { return formatDouble(seconds, 3) + " s"; }
public int compareTo(Seconds s) {
return cmp(seconds, s.seconds);
}
Seconds div(double x) { return new Seconds(get()/x); }
Seconds minus(Seconds x) { return new Seconds(get()-x.get()); }
}
static Class run(String progID, String... args) {
Class main = hotwire(progID);
callMain(main, args);
return main;
}
static String hijackPrint_tee(Runnable r) {
final StringBuilder buf = new StringBuilder();
Object old = interceptPrintInThisThread(new F1() {
Boolean get(String s) {
buf.append(s);
return true;
}
});
try {
callF(r);
return str(buf);
} finally {
interceptPrintInThisThread(old);
}
}
static Timestamp tsNow() {
return new Timestamp();
}
static OKOrError okOrError(IF0 f) { return okOrError(f, true); }
static OKOrError okOrError(IF0 f, boolean printStackTrace) {
try {
return OKOrError_ok(f.get());
} catch (Throwable e) {
if (printStackTrace) printStackTrace(e);
return OKOrError_error(e);
}
}
static OKOrError okOrError(Runnable r) {
return okOrError(runnableToIF0(r));
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static String formatElapsedTimeWithAppropriateUnit(double seconds) {
if (seconds >= 1) return formatDouble(seconds, 3) + " s";
double ms = seconds*1000;
if (ms >= 1) return formatDouble(ms, 3) + " ms";
double us = ms*1000;
if (us >= 1) return formatDouble(us, 3) + " µs";
double ns = us*1000;
return formatDouble(ns, 3) + " ns";
}
static double nanosToSeconds(double nanos) {
return nanoSecondsToSeconds(nanos);
}
static RuntimeException error() {
throw new RuntimeException("fail");
}
static RuntimeException error(String msg) {
throw new RuntimeException(msg);
}
static A assertNotNull(A a) {
assertTrue(a != null);
return a;
}
static A assertNotNull(String msg, A a) {
assertTrue(msg, a != null);
return a;
}
static String ok(Object o) {
return format("ok *", o);
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static String str(char[] c, int offset, int count) {
return new String(c, offset, count);
}
static int boostHashCombine(int a, int b) {
return a ^ (b + 0x9e3779b9 + (a << 6) + (a >>> 2));
// OLD (changed) 2022/3/10: ret a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2));
}
static int _hashCode(Object a) {
return a == null ? 0 : a.hashCode();
}
static long now_virtualTime;
static long now() {
return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis();
}
static String formatLocalDateWithSeconds(long time) {
return localDateWithSeconds(time);
}
static String formatLocalDateWithSeconds() {
return localDateWithSeconds();
}
static int cmp(Number a, Number b) {
return a == null ? b == null ? 0 : -1 : cmp(a.doubleValue(), b.doubleValue());
}
static int cmp(double a, double b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(int a, int b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(long a, long b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(Object a, Object b) {
if (a == null) return b == null ? 0 : -1;
if (b == null) return 1;
return ((Comparable) a).compareTo(b);
}
static BigInteger plus(BigInteger a, BigInteger b) {
return a.add(b);
}
static BigInteger plus(BigInteger a, long b) {
return a.add(bigint(b));
}
static long plus(long a, long b) { return a+b; }
static int plus(int a, int b) { return a+b; }
static float plus(float a, float b) { return a+b; }
static double plus(double a, double b) { return a+b; }
static long toMS(double seconds) {
return (long) (seconds*1000);
}
static long clockTimeToSystemTime(long now) {
return now == 0 ? 0 : now + clockToSysTimeDiff();
}
static List minus(Collection a, Object... b) {
Set set = asSet(b);
List l = new ArrayList();
for (Object s : unnull(a))
if (!set.contains(s))
l.add(s);
return l;
}
static BigInteger minus(BigInteger a, BigInteger b) {
return a.subtract(b);
}
static Complex minus(Complex c) {
return c == null ? null : complex(-c.re(), -c.im());
}
static int minus(int a, int b) {
return a-b;
}
static double minus(double a, double b) {
return a-b;
}
static int seconds() {
return seconds(java.util.Calendar.getInstance());
}
static int seconds(java.util.Calendar c) {
return c.get(java.util.Calendar.SECOND);
}
static String formatDouble(double d, int digits) {
String format = digits <= 0 ? "0" : "0." + rep(digits, '#');
return decimalFormatEnglish(format, d);
}
static String formatDouble(double d) {
return str(d);
}
// 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);
}
// custom mainClass only works with hotwire_here
static Class> hotwire(String src) { return hotwire(src, __1 -> mainClassNameForClassLoader(__1)); }
static Class> hotwire(String src, IF1 calculateMainClass) {
assertFalse(_inCore());
Class j = getJavaX();
if (isAndroid()) {
synchronized(j) { // hopefully this goes well...
List libraries = new ArrayList();
File srcDir = (File) call(j, "transpileMain", src, libraries);
if (srcDir == null)
throw fail("transpileMain returned null (src=" + quote(src) + ")");
Object androidContext = get(j, "androidContext");
return (Class) call(j, "loadx2android", srcDir, src);
}
} else {
Class c = (Class) (call(j, "hotwire", src));
hotwire_copyOver(c);
return c;
}
}
static A callMain(A c, String... args) {
callOpt(c, "main", new Object[] {args});
return c;
}
static void callMain() {
callMain(mc());
}
// f can return false to suppress regular printing
// call print_raw within f to actually print something
// f preferrably is F1
static Object interceptPrintInThisThread(Object f) {
Object old = print_byThread().get();
print_byThread().set(f);
return old;
}
static Map> callF_cache = newDangerousWeakHashMap();
static A callF(F0 f) {
return f == null ? null : f.get();
}
static B callF(F1 f, A a) {
return f == null ? null : f.get(a);
}
static A callF(IF0 f) {
return f == null ? null : f.get();
}
static B callF(IF1 f, A a) {
return f == null ? null : f.get(a);
}
static B callF(A a, IF1 f) {
return f == null ? null : f.get(a);
}
static C callF(IF2 f, A a, B b) {
return f == null ? null : f.get(a, b);
}
static void callF(VF1 f, A a) {
if (f != null) f.get(a);
}
static void callF(A a, IVF1 f) {
if (f != null) f.get(a);
}
static void callF(IVF1 f, A a) {
if (f != null) f.get(a);
}
static Object callF(Runnable r) { { if (r != null) r.run(); } return null; }
static Object callF(Object f, Object... args) {
return safeCallF(f, args);
}
static Object safeCallF(Object f, Object... args) {
if (f instanceof Runnable) {
((Runnable) f).run();
return null;
}
if (f == null) return null;
Class c = f.getClass();
ArrayList methods;
synchronized(callF_cache) {
methods = callF_cache.get(c);
if (methods == null)
methods = callF_makeCache(c);
}
int n = l(methods);
if (n == 0) {
if (f instanceof String)
throw fail("Legacy call: " + f);
throw fail("No get method in " + getClassName(c));
}
if (n == 1) return invokeMethod(methods.get(0), f, args);
for (int i = 0; i < n; i++) {
Method m = methods.get(i);
if (call_checkArgs(m, args, false))
return invokeMethod(m, f, args);
}
throw fail("No matching get method in " + getClassName(c));
}
// used internally
static ArrayList callF_makeCache(Class c) {
ArrayList l = new ArrayList();
Class _c = c;
do {
for (Method m : _c.getDeclaredMethods())
if (m.getName().equals("get")) {
makeAccessible(m);
l.add(m);
}
if (!l.isEmpty()) break;
_c = _c.getSuperclass();
} while (_c != null);
callF_cache.put(c, l);
return l;
}
static OKOrError OKOrError_ok(A a) {
return new OKOrError(a);
}
static A printStackTrace(A e) {
// we go to system.out now - system.err is nonsense
if (e != null) print(getStackTrace(e));
return e;
}
static void printStackTrace() {
printStackTrace(new Throwable());
}
static void printStackTrace(String msg) {
printStackTrace(new Throwable(msg));
}
static void printStackTrace(String msg, Throwable e) {
printStackTrace(new Throwable(msg, e));
}
static OKOrError OKOrError_error(Throwable e) {
return new OKOrError(false, e);
}
static IF0 runnableToIF0(Runnable r) {
return r == null ? null : () -> { r.run(); return null; };
}
static void _handleError(Error e) {
//call(javax(), '_handleError, e);
}
static double nanoSecondsToSeconds(double nanos) {
return nanos*1e-9;
}
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