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;
// returns last value
import java.text.SimpleDateFormat;
import java.text.*;
import java.util.TimeZone;
import static x30_pkg.x30_util.DynamicObject;
import java.text.NumberFormat;
import java.awt.geom.*;
class main {
static class PrintPolledValueUntilPredicateOrTimeout {
final public PrintPolledValueUntilPredicateOrTimeout setMakeValue(IF0 makeValue){ return makeValue(makeValue); }
public PrintPolledValueUntilPredicateOrTimeout makeValue(IF0 makeValue) { this.makeValue = makeValue; return this; } final public IF0 getMakeValue(){ return makeValue(); }
public IF0 makeValue() { return makeValue; }
IF0 makeValue;
final public PrintPolledValueUntilPredicateOrTimeout setPred(IF1 pred){ return pred(pred); }
public PrintPolledValueUntilPredicateOrTimeout pred(IF1 pred) { this.pred = pred; return this; } final public IF1 getPred(){ return pred(); }
public IF1 pred() { return pred; }
IF1 pred; // if true, loop ends
final public PrintPolledValueUntilPredicateOrTimeout setTimeout(double timeout){ return timeout(timeout); }
public PrintPolledValueUntilPredicateOrTimeout timeout(double timeout) { this.timeout = timeout; return this; } final public double getTimeout(){ return timeout(); }
public double timeout() { return timeout; }
double timeout = 60.0;
final public PrintPolledValueUntilPredicateOrTimeout setInterval(int interval){ return interval(interval); }
public PrintPolledValueUntilPredicateOrTimeout interval(int interval) { this.interval = interval; return this; } final public int getInterval(){ return interval(); }
public int interval() { return interval; }
int interval = 100;
final public PrintPolledValueUntilPredicateOrTimeout setPrintPrefix(String printPrefix){ return printPrefix(printPrefix); }
public PrintPolledValueUntilPredicateOrTimeout printPrefix(String printPrefix) { this.printPrefix = printPrefix; return this; } final public String getPrintPrefix(){ return printPrefix(); }
public String printPrefix() { return printPrefix; }
String printPrefix = "";
PrintPolledValueUntilPredicateOrTimeout(String printPrefix, IF0 makeValue, IF1 pred) {
this.pred = pred;
this.makeValue = makeValue;
this.printPrefix = printPrefix;}
void printValue(A a) {
printWithMS(printPrefix, a);
}
A get() {
Timeout timeOut = new Timeout(timeout);
A a = makeValue.get();
printValue(a);
if (pred != null && pred.get(a))
return a;
while (!timeOut.get()) {
A b = makeValue.get();
if (!eq(a, b))
printValue(a = b);
if (pred != null && pred.get(a))
break;
else
sleep(interval);
}
return a;
}
public void run() { try { get(); } catch (Exception __e) { throw rethrow(__e); } }
}
static void printWithMS(A a) { printWithMS("", a); }
static void printWithMS(String prefix, A a) {
printWithMSTime(prefix, a);
}
static boolean eq(Object a, Object b) {
return a == b || a != null && b != null && a.equals(b);
}
// a little kludge for stuff like eq(symbol, "$X")
static boolean eq(Symbol a, String b) {
return eq(str(a), b);
}
static volatile boolean sleep_noSleep = false;
static void sleep(long ms) {
ping();
if (ms < 0) return;
// allow spin locks
if (isAWTThread() && ms > 100) throw fail("Should not sleep on AWT thread");
try {
Thread.sleep(ms);
} catch (Exception e) { throw new RuntimeException(e); }
}
static void sleep() { try {
if (sleep_noSleep) throw fail("nosleep");
print("Sleeping.");
sleepQuietly();
} catch (Exception __e) { throw rethrow(__e); } }
// 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 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 void printWithMSTime(A a) { printWithMSTime("", a); }
static void printWithMSTime(String prefix, A a) {
print(hmsWithColonsAndMS() + ": " + combinePrintParameters(prefix, a));
}
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);
}
// legacy mode
//sbool ping_actions_shareable = true;
static volatile boolean ping_pauseAll = false;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile boolean ping_anyActions = false;
static Map ping_actions = newWeakHashMap();
static ThreadLocal ping_isCleanUpThread = new ThreadLocal();
// ignore pingSource if not PingV3
static boolean ping(PingSource pingSource) { return ping(); }
// always returns true
static boolean ping() {
//ifdef useNewPing
newPing();
//endifdef
if (ping_pauseAll || ping_anyActions) ping_impl(true /* XXX */);
//ifndef LeanMode ping_impl(); endifndef
return true;
}
// returns true when it slept
static boolean ping_impl(boolean okInCleanUp) { try {
if (ping_pauseAll && !isAWTThread()) {
do
Thread.sleep(ping_sleep);
while (ping_pauseAll);
return true;
}
if (ping_anyActions) { // don't allow sharing ping_actions
if (!okInCleanUp && !isTrue(ping_isCleanUpThread.get()))
failIfUnlicensed();
Object action = null;
synchronized(ping_actions) {
if (!ping_actions.isEmpty()) {
action = ping_actions.get(currentThread());
if (action instanceof Runnable)
ping_actions.remove(currentThread());
if (ping_actions.isEmpty()) ping_anyActions = false;
}
}
if (action instanceof Runnable)
((Runnable) action).run();
else if (eq(action, "cancelled"))
throw fail("Thread cancelled.");
}
return false;
} catch (Exception __e) { throw rethrow(__e); } }
// TODO: test if android complains about this
static boolean isAWTThread() {
if (isAndroid()) return false;
if (isHeadless()) return false;
return isAWTThread_awt();
}
static boolean isAWTThread_awt() {
return SwingUtilities.isEventDispatchThread();
}
static RuntimeException fail() { throw new RuntimeException("fail"); }
static RuntimeException fail(Throwable e) { throw asRuntimeException(e); }
static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); }
static RuntimeException fail(Object... objects) { throw new Fail(objects); }
static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); }
static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); }
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