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 java.text.NumberFormat;
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import javax.swing.undo.UndoManager;
import java.awt.datatransfer.StringSelection;
import java.text.*;
import java.util.TimeZone;
class main {
static class A extends Concept { String a; }
public static void main(final String[] args) throws Exception { pExperiment();
ConceptsLoadedOnDemand cc = new ConceptsLoadedOnDemand();
Concepts c1 = cc.get("case 1");
assertSame(c1, cc.get("case 1"));
uniq(c1, A.class, "a" , "bla");
pnlStruct(list(c1, A.class));
Concepts c2 = cc.get("case 2");
assertSame(c2, cc.get("case 2"));
uniq(c2, A.class, "a" , "blubb");
pnlStruct(list(c2, A.class));
cleanUp(cc);
}
static void pExperiment() {
if (!isMain()) return;
//substance();
autoRestart(5);
experimentIcon();
centerHigherConsole();
tt();
}
static void assertSame(String msg, Object a, Object b) {
if (a != b)
throw fail(msg);
}
static void assertSame(Object a, Object b) {
if (a != b)
throw fail();
}
static A uniq(Class c, Object... params) {
return uniqueConcept(c, params);
}
static A uniq(Concepts cc, Class c, Object... params) {
return uniqueConcept(cc, c, params);
}
// currently collection and map only
static void pnlStruct(Object o) {
if (o instanceof Collection) pnlStruct(((Collection) o));
else if (o instanceof Map) pnlStruct(((Map) o));
else if (o != null) pnlStruct(ll(o));
}
static > B pnlStruct(B l) {
pnlStruct((Iterable) l);
return l;
}
static Iterable pnlStruct(Iterable l) {
return pnlStruct("", l);
}
static Iterable pnlStruct(String prefix, Iterable l) {
int i = 0;
if (l != null) for (A a : l) print(prefix + (++i) + ". " + struct_noStringSharing(a));
return l;
}
static A[] pnlStruct(A[] l) {
pnlStruct(asList(l));
return l;
}
static Map pnlStruct(Map map) {
pnl(map(map, new F2() { Object get(A a, B b) { try { return sfu(a) + " = " + sfu(b); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "sfu(a) + \" = \" + sfu(b)"; }}));
return map;
}
static List list(Class type) {
return db_mainConcepts().list(type);
}
static List list(Concepts concepts, Class type) {
return concepts.list(type);
}
static List list(String type) {
return db_mainConcepts().list(type);
}
static List list(Concepts concepts, String type) {
return concepts.list(type);
}
static boolean cleanUp_interruptThreads = false; // experimental
static void cleanUp(Object c) {
if (c == null) return;
if (c instanceof Collection) { cleanUp((Collection) c); return; }
if (c instanceof Map) {
for (Object o : keys((Map) c)) cleanUp(o);
for (Object o : values((Map) c)) cleanUp(o);
((Map) c).clear();
return;
}
//if (!(c instanceof Class)) ret;
try {
// revoke license
preCleanUp(c);
// unpause
setOpt(c, "ping_pauseAll", false);
// call custom cleanMeUp() and cleanMeUp_*() functions
innerCleanUp(c);
// Java spec says finalize should only be called by GC,
// but we care to differ.
// Edit: Not anymore (illegal access warnings)
/*if (isTrue(vmMap_get('callFinalize)))
pcallOpt(c, "finalize");*/
// remove all virtual bots (hope this works)
List androids = (List) getOpt(c, "record_list");
for (Object android : unnull(androids))
pcallOpt(android, "dispose"); // heck we'll dispose anything
// sub-cleanup
List classes = (List) (getOpt(c, "hotwire_classes"));
if (classes != null)
for (WeakReference cc : classes) { try {
cleanUp(cc.get());
} catch (Throwable __e) { _handleException(__e); }}
// interrupt all threads (experimental, they might be doing cleanup?)
if (cleanUp_interruptThreads) {
List threads = registeredThreads(c);
if (nempty(threads)) {
print("cleanUp: Interrupting " + n2(threads, "thread") + ": " + joinWithComma(allToString(threads)));
interruptThreads(threads);
}
}
} catch (Throwable __e) { _handleException(__e); }
setOpt(c, "cleaningUp_flag" , false);
if (c instanceof Class && ((Class) c).getName().equals("main"))
retireClassLoader(((Class) c).getClassLoader());
}
static void cleanUp(Collection l) {
if (l == null) return;
for (Object c : l)
cleanUp(c);
l.clear();
}
static boolean isMain() {
return isMainProgram();
}
static double autoRestart_interval = 10;
static boolean autoRestart_on, autoRestart_debug, autoRestart_simulate;
static java.util.Timer autoRestart_timer;
static void autoRestart(double interval) {
autoRestart_interval = interval;
autoRestart();
}
static void autoRestart() {
if (!isMain() || autoRestart_on) return;
autoRestart_on = true;
autoRestart_schedule();
preloadProgramTitle();
}
static void autoRestart_off() {
if (!autoRestart_on) return;
stopTimer(autoRestart_timer);
autoRestart_timer = null;
}
static void autoRestart_schedule() {
autoRestart_timer = doLater_daemon(toMS(autoRestart_interval), "autoRestart_check");
}
static void autoRestart_check() {
try {
String newMD5;
try {
newMD5 = serverAutoRestartMD5(programID());
} catch (Throwable e) {
return;
}
if (!isMD5(newMD5)) { if (autoRestart_debug) print("autoRestart: no server transpilation"); return; }
if (autoRestart_localMD5 == null)
autoRestart_localMD5 = md5(loadCachedTranspilation(programID()));
String localMD5 = autoRestart_localMD5();
if (neq(localMD5, newMD5)) {
if (autoRestart_simulate)
print("Would upgrade now. " + localMD5 + " -> " + newMD5);
else {
infoBox("Upgrading " + programTitle());
restartWithDelay(500);
sleep();
}
}
} finally {
if (autoRestart_debug) print("autoRestart: Done");
autoRestart_schedule();
}
}
static void experimentIcon() {
consoleAndDefaultFrameIcon("#1101173" /*#1101171*/);
}
static void centerHigherConsole() {
if (headless()) return;
setConsoleHeight(600);
centerConsole();
}
static void tt() {
typeWriterConsole();
}
static String tt(Object contents, Object... params) {
return tag("tt", contents, params);
}
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(String msg) { throw new RuntimeException(msg == null ? "" : msg); }
static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); }
static A uniqueConcept(Class c, Object... params) {
return uniqueConcept(db_mainConcepts(), c, params);
}
static A uniqueConcept(Concepts cc, Class c, Object... params) {
params = expandParams(c, params);
A x = findConceptWhere(cc, c, params);
if (x == null) {
x = unlisted(c);
csetAll(x, params);
cc.register(x);
}
return x;
}
static List ll(A... a) {
ArrayList l = new ArrayList(a.length);
if (a != null) for (A x : a) l.add(x);
return l;
}
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