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 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.*;
import static x30_pkg.x30_util.DynamicObject;
class main {
static class WeakCompactLinkedHashSet extends AbstractSet {
CompactLinkedHashSet> set = new CompactLinkedHashSet();
ReferenceQueue queue = new ReferenceQueue();
static class MyWeakReference extends WeakReference {
int hash;
MyWeakReference(A o) {
super(o);
hash = o.hashCode();
}
MyWeakReference(A o, ReferenceQueue queue) {
super(o, queue);
hash = o.hashCode();
}
public int hashCode() { return hash; }
public boolean equals(Object o) {
return o instanceof MyWeakReference
&& eq(get(), ((MyWeakReference) o).get());
}
}
// Make sure we get reaped once a minute (or whatever the policy is)
WeakCompactLinkedHashSet() {
_registerWeakCollection(this);
}
void reap() {
MyWeakReference ref;
while ((ref = (MyWeakReference) queue.poll()) != null)
set.removeIfSame(ref);
}
public int size() { reap(); return set.size(); }
public Iterator iterator() {
final Iterator> it = set.iterator();
return iteratorFromFunction(() -> {
while (it.hasNext()) {
MyWeakReference ref = it.next();
A a = ref.get();
if (a != null) return a;
// todo: clean up entry
}
return null;
});
}
public boolean contains(Object o) {
reap();
return set.contains(new MyWeakReference(o));
}
public boolean add(A a) {
reap();
return set.add(new MyWeakReference(a, queue));
}
public boolean remove(Object o) {
reap();
return set.remove(new MyWeakReference(o));
}
A find(Object o) {
reap();
MyWeakReference ref = set.find(new MyWeakReference(o));
return ref == null ? null : ref.get();
}
}
static boolean eq(Object a, Object b) {
return a == b || a != null && b != null && a.equals(b);
}
// 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 List _registerWeakCollection_preList;
static A _registerWeakCollection(A cl) {
if (javax() == null) {
// We're in class init
if (_registerWeakCollection_preList == null) _registerWeakCollection_preList = synchroList();
_registerWeakCollection_preList.add(cl);
return cl;
}
try {
call(javax(), "_registerWeakCollection", cl);
} catch (Throwable e) {
printException(e);
print("Upgrade JavaX!!");
}
return cl;
}
static void _onLoad_registerWeakCollection() {
assertNotNull(javax());
if (_registerWeakCollection_preList == null) return;
for (Object o : _registerWeakCollection_preList)
_registerWeakCollection(o);
_registerWeakCollection_preList = null;
}
static Iterator iterator(Iterable c) {
return c == null ? emptyIterator() : c.iterator();
}
// f: func -> A (stream ends when f returns null)
static IterableIterator iteratorFromFunction(final Object f) {
class IFF extends IterableIterator {
A a;
boolean done = false;
public boolean hasNext() {
getNext();
return !done;
}
public A next() {
getNext();
if (done) throw fail();
A _a = a;
a = null;
return _a;
}
void getNext() {
if (done || a != null) return;
a = (A) callF(f);
done = a == null;
}
};
return new IFF();
}
// optimized version for F0 argument
static IterableIterator iteratorFromFunction(F0 f) {
return iteratorFromFunction_f0(f);
}
static IterableIterator iteratorFromFunction(IF0 f) {
return iteratorFromFunction_if0(f);
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Iterator i) { return iteratorCount_int_close(i); } // consumes the iterator && closes it if possible
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); }
static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o == null ? 0
: o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: o instanceof Collection ? l((Collection) o)
: o instanceof Object[] ? l((Object[]) o)
: o instanceof boolean[] ? l((boolean[]) o)
: o instanceof byte[] ? l((byte[]) o)
: o instanceof char[] ? l((char[]) o)
: o instanceof short[] ? l((short[]) o)
: o instanceof int[] ? l((int[]) o)
: o instanceof float[] ? l((float[]) o)
: o instanceof double[] ? l((double[]) o)
: o instanceof long[] ? l((long[]) o)
: (Integer) call(o, "size");
}
static Field getOpt_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);
return null;
}
static Field makeAccessible(Field f) {
try {
f.setAccessible(true);
} catch (Throwable e) {
// Note: The error reporting only works with Java VM option --illegal-access=deny
vmBus_send("makeAccessible_error", e, f);
}
return f;
}
static Method makeAccessible(Method m) {
try {
m.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, m);
}
return m;
}
static Constructor makeAccessible(Constructor c) {
try {
c.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, c);
}
return c;
}
static Object getOptDynOnly(DynamicObject o, String field) {
if (o == null || o.fieldValues == null) return null;
return o.fieldValues.get(field);
}
static RuntimeException asRuntimeException(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
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 Class javax() {
return getJavaX();
}
static List synchroList() {
return synchroList(new ArrayList());
}
static List synchroList(List l) {
return Collections.synchronizedList(l);
}
static Object call(Object o) {
return callF(o);
}
// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] {arg});
}
static Object call(Object o, String method, Object... args) {
//ret call_cached(o, method, args);
return call_withVarargs(o, method, args);
}
static A printException(A e) {
printStackTrace(e);
return e;
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
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