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 x30_pkg.x30_util; import static x30_pkg.x30_util.VF1; import static x30_pkg.x30_util.l; import static x30_pkg.x30_util.fail; import static x30_pkg.x30_util.indexOf; import static x30_pkg.x30_util.getOpt; import static x30_pkg.x30_util.setOpt; import static x30_pkg.x30_util.callOpt; import static x30_pkg.x30_util.newWeakHashMap; import static x30_pkg.x30_util.newDangerousWeakHashMap; import static x30_pkg.x30_util.get; import static x30_pkg.x30_util.get_raw; import static x30_pkg.x30_util.assertTrue; import static x30_pkg.x30_util.isHeadless; import static x30_pkg.x30_util.isAndroid; import static x30_pkg.x30_util.isTrue; import static x30_pkg.x30_util.asList; import x30_pkg.x30_util.DynamicObject; import loadableUtils.utils; import static loadableUtils.utils._threadInfo; import static loadableUtils.utils._threadInheritInfo; import static loadableUtils.utils._threadInfo_addMakerAndRetriever; import static loadableUtils.utils.dm_currentModule; import static loadableUtils.utils.dm_current_mandatory; import static loadableUtils.utils.match; import static loadableUtils.utils.getOpt_raw; import static loadableUtils.utils.setOpt_raw; import static loadableUtils.utils.getField; import static loadableUtils.utils.fieldType; import static loadableUtils.utils.format3; import static loadableUtils.utils.vm_generalIdentityHashSet; import static loadableUtils.utils.vm_generalHashMap; import static loadableUtils.utils.vm_generalWeakSubMap; import static loadableUtils.utils.bindToComponent; import static loadableUtils.utils.struct; import static loadableUtils.utils.structure; import static loadableUtils.utils.loadPage; import static loadableUtils.utils.loadPage_utf8; import static loadableUtils.utils.loadPageSilentlyWithTimeout; import static loadableUtils.utils.loadPageSilently; import static loadableUtils.utils.loadSnippet; import static loadableUtils.utils.loadSnippetQuietly; import static loadableUtils.utils.sendToLocalBot; import static loadableUtils.utils.componentPopupMenu; import static loadableUtils.utils.componentPopupMenu_top; import static loadableUtils.utils.componentPopupMenu_initForComponent; import static loadableUtils.utils.componentPopupMenu_getEvent; import static loadableUtils.utils.listPopupMenu; import static loadableUtils.utils.tablePopupMenu; import static loadableUtils.utils.rowSorter_setComparators; import static loadableUtils.utils.sexyTableWithoutDrag; import static loadableUtils.utils.dm_current_generic; import static loadableUtils.utils.dm_current_mandatory_generic; import static loadableUtils.utils.cset; import static loadableUtils.utils.DynamicObject_loading; import static loadableUtils.utils.concepts_unlisted; import static loadableUtils.utils.makePopupMenuConditional; import static loadableUtils.utils.makeConceptsTable_idWidth; import static loadableUtils.utils.showConceptsTable_afterUpdate; import static loadableUtils.utils.dynamicObjectIsLoading; import loadableUtils.utils.F0; import loadableUtils.utils.F1; import loadableUtils.utils.IF1; import loadableUtils.utils.Matches; import loadableUtils.utils.BetterLabel; import loadableUtils.utils.SingleComponentPanel; import loadableUtils.utils.Snippet; import loadableUtils.utils.Q; import loadableUtils.utils.ImageSurface; import loadableUtils.utils.structure_Data; import loadableUtils.utils.RGBImage; import loadableUtils.utils.RGB; import loadableUtils.utils.BWImage; import loadableUtils.utils.MakesBufferedImage; import loadableUtils.utils.MultiSet; import loadableUtils.utils.Concept; import loadableUtils.utils.Concepts; import loadableUtils.utils.IConceptIndex; import loadableUtils.utils.IFieldIndex; import loadableUtils.utils.Derefable; import loadableUtils.utils.ImageSurfaceSelector; import loadableUtils.utils.SimpleCRUD; import loadableUtils.utils.PersistableThrowable; import loadableUtils.utils.DynModule; import loadableUtils.utils.DynPrintLog; import loadableUtils.utils.DynObjectTable; import loadableUtils.utils.DynImageSurface; import loadableUtils.utils.DynCalculatedList; import loadableUtils.utils.Rect; import loadableUtils.utils.Pt; 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 java.nio.file.Files; import java.nio.file.Path; class main { public static class Timeline extends DynPrintLog { public transient String input = "\r\n --- Rules\r\n\r\n Event 1 happens before event 2 :=\r\n vars {event 1, event 2, X, Y}\r\n Event 1 happens at day X.\r\n Event 2 happens at day Y.\r\n X < Y.\r\n \r\n --- Facts\r\n\r\n Enrolling happens before graduation.\r\n Enrolling happens at day 200.\r\n Graduation happens at day 100. // Contradiction!\r\n "; public transient List rules = new ArrayList(); public void start() { super.start(); startThread("Start", new Runnable() { public void run() { try { AutoCloseable __7 = enter(); try { Map sections = minusSignSectionsCI(input); for (String s : splitAtEmptyLines(sections.get("rules"))) { List lines = tlft(s); String head = dropSuffix_trim(":=", popFirst(lines)); Rule_StringToStrings rule = new Rule_StringToStrings(head); Matches m = new Matches(); if (match3_plusBrackets("vars * ", first(lines), m)) { rule.vars = asCISet(tok_splitAtComma(uncurly(m.get(0)))); popFirst(lines); } rule.out = lines; print("Got rule: " + rule); printStruct(rule); rules.add(rule); } for (String fact : tlft(dropJavaComments(sections.get("facts")))) { print("Got fact: " + fact); } } finally { _close(__7); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "AutoCloseable __7 = enter(); try { \r\n Map sections = minus..."; } }); } } public static boolean _moduleClass_Timeline = true; public static class Gazelle_MiniEngine2 { public int minScore = 50; public boolean printMappings, printGroupedData, printRejectedVars; public F1 formatForUser = new F1() { public String get(String s) { try { return ai_superSimpleVerbCorrector(tok_dropCurlyBrackets(s)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_superSimpleVerbCorrector(tok_dropCurlyBrackets(s))"; } }; public F1> parse = new F1>() { public List get(String s) { try { return codeTokens_lazy_uncurly(javaTokWithBrackets_cached(s)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "codeTokens_lazy_uncurly(javaTokWithBrackets_cached(s))"; } }; public String formatForUser(String s) { return callF(formatForUser, s); } public List parse(String s) { return callF(parse, s); } public List> rules; public Map, Set> ruleVariables = new HashMap(); public MultiMap run(String _data) { String data = mapEachLine("ai_groupSimplestNounPhrases", _data); if (printGroupedData) print(data); Map sections = asCIMap(minusSignsSections(data)); rules = ai_findDoubleArrowRulesAsPairs(sections.get("Rules")); for (Pair rule : rules) ruleVariables.put(rule, ai_wordsInBothSidesOfPair_uncurly(rule)); printAsciiHeading("REASONING"); MultiMap solutions = new MultiMap(); for (String input : tlft(sections.get("Input"))) { print("\nInput: " + tok_dropCurlyBrackets(input)); AutoCloseable __8 = tempIndent(); try { for (Pair in : interpretations(input)) { print("\nInterpretation: " + formatForUser(in.a)); Set out = litciset(); interpretRecursively(in.a, 5, out); if (!matchAny("fail", out)) solutions.put(input, in.a); } } finally { _close(__8); } } printAsciiHeading("RESULTS"); print(formatDoubleArrowPairs_horizontallyAligned(mapPairBoth(formatForUser, multiMapToPairs(solutions)))); return solutions; } public void interpretRecursively(String input, int level, Set out) { if (level <= 0) return; List interpretations = pairsA(interpretations(ai_groupSimplestNounPhrases(input))); AutoCloseable __9 = tempIndent(); try { for (String in : addAllAndReturnNew(out, interpretations)) { print(" => " + formatForUser(in)); for (String s : lithashset(in, ai_superSimpleVerbCorrector(in))) interpretRecursively(s, level - 1, out); } } finally { _close(__9); } } public List> interpretations(String input) { List tokI = parse(input); List> interpretations = new ArrayList(); for (Pair rule : rules) { List tokR = parse(rule.a); final Map map = ciMapWithoutKeysEqualToValues(zipTwoListsToCIMap_strict(tokR, tokI)); if (map == null) continue; int score = l(tokR) - l(map); List nonVars = withoutDollarVars(listMinusSet(keys(map), ruleVariables.get(rule))); if (printRejectedVars && nempty(nonVars)) print("Rejected vars: " + nonVars); if (nempty(nonVars)) score = 0; if (printMappings) print(" " + reverseMapCI_joinDuplicatesWithPlus(map) + " | " + rule.a); String c = rule.b; c = join(mapCodeTokens(javaTok(c), new F1() { public String get(String s) { try { return getOrKeep_tryUncurlied(map, s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "getOrKeep_tryUncurlied(map, s)"; } })); c = join(mapCodeTokens(javaTokWithBrackets(c), new F1() { public String get(String s) { try { return getOrKeep_tryUncurlied(map, s); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "getOrKeep_tryUncurlied(map, s)"; } })); addPairIfBAtLeast(interpretations, c, ratioToIntPercent(score, l(tokR)), minScore); } return interpretations; } } public static Object callF(Object f, Object... args) { try { if (f instanceof String) return callMC((String) f, args); return x30_util.callF(f, args); } catch (Exception __e) { throw rethrow(__e); } } public static A callF(F0 f) { return f == null ? null : f.get(); } public static A callF(IF0 f) { return f == null ? null : f.get(); } public static B callF(F1 f, A a) { return f == null ? null : f.get(a); } public static B callF(IF1 f, A a) { return f == null ? null : f.get(a); } public static void callF(VF1 f, A a) { if (f != null) f.get(a); } public static Object callMC(String method, Object... args) { return call(mc(), method, args); } public static Object call(Object o) { return callF(o); } public static Object call(Object o, String method, Object... args) { return call_withVarargs(o, method, args); } public static void _onLoad_initUtils() { utils.__javax = javax(); } public static void _onLoad_defaultClassFinder() { setDefaultClassFinder(new F1() { public Class get(String name) { try { Class c = findClass_fullName(name); if (c != null) return c; if (startsWith(name, "main$")) return loadableUtils.utils.findClass_fullName("loadableUtils.utils" + substring(name, 4)); return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Class c = findClass_fullName(name);\r\n if (c != null) ret c;\r\n if (start..."; } }); } public static String programID() { return getProgramID(); } public static String programID(Object o) { return getProgramID(o); } public static volatile StringBuffer local_log = new StringBuffer(); public static volatile Appendable print_log = local_log; public static volatile int print_log_max = 1024 * 1024; public static volatile int local_log_max = 100 * 1024; public static boolean print_silent = false; public static Object print_byThread_lock = new Object(); public static volatile ThreadLocal print_byThread; public static volatile Object print_allThreads; public static volatile Object print_preprocess; public static void print() { print(""); } public static A print(String s, A o) { print((endsWithLetterOrDigit(s) ? s + ": " : s) + o); return o; } public static A print(A o) { ping_okInCleanUp(); if (print_silent) return o; String s = String.valueOf(o) + "\n"; print_noNewLine(s); return o; } public static void print_noNewLine(String s) { Object f = getThreadLocal(print_byThread_dontCreate()); if (f == null) f = print_allThreads; if (f != null) if (isFalse(f instanceof F1 ? ((F1) f).get(s) : callF(f, s))) return; print_raw(s); } public static void print_raw(String s) { if (print_preprocess != null) s = (String) callF(print_preprocess, s); s = fixNewLines(s); Appendable loc = local_log; Appendable buf = print_log; int loc_max = print_log_max; if (buf != loc && buf != null) { print_append(buf, s, print_log_max); loc_max = local_log_max; } if (loc != null) print_append(loc, s, loc_max); System.out.print(s); } public static void print_autoRotate() { } public static AutoCloseable tempInterceptPrintIfNotIntercepted(F1 f) { return print_byThread().get() == null ? tempInterceptPrint(f) : null; } public static Thread startThread(Object runnable) { return startThread(defaultThreadName(), runnable); } public static Thread startThread(String name, Object runnable) { runnable = wrapAsActivity(runnable); return startThread(newThread(toRunnable(runnable), name)); } public static Thread startThread(Thread t) { _registerThread(t); t.start(); return t; } public static TreeMap minusSignSectionsCI(String text) { return asCIMap(minusSignsSections(text)); } public static List splitAtEmptyLines(String text) { List lines = toLines(text); int n = l(lines), i = 0; List parts = new ArrayList(); while (true) { while (i < n && emptyAfterTrim(lines.get(i))) ++i; if (i >= n) break; int j = i; while (j < n && !emptyAfterTrim(lines.get(j))) ++j; parts.add(fromLines(subList(lines, i, j))); i = j; } return parts; } public static List tlft(String s) { return toLinesFullTrim(s); } public static List tlft(File f) { return toLinesFullTrim(f); } public static String dropSuffix_trim(String suffix, String s) { return dropSuffixTrim(suffix, s); } public static boolean dropSuffix_trim(String suffix, String s, Matches m) { return dropSuffixTrim(suffix, s, m); } public static A popFirst(List l) { if (empty(l)) return null; A a = first(l); l.remove(0); return a; } public static A popFirst(Collection l) { if (empty(l)) return null; A a = first(l); l.remove(a); return a; } public static List popFirst(int n, List l) { List part = cloneSubList(l, 0, n); removeSubList(l, 0, n); return part; } public static boolean match3_plusBrackets(String pat, String s) { return match3_plusBrackets(pat, s, null); } public static boolean match3_plusBrackets(String pat, String s, Matches matches) { if (pat == null || s == null) return false; return match3_plusBrackets(pat, parse3_plusBrackets(s), matches); } public static boolean match3_plusBrackets(String pat, List toks, Matches matches) { List tokpat = parse3_cachedPattern(pat); return match3(tokpat, toks, matches); } public static Object first(Object list) { return first((Iterable) list); } public static A first(List list) { return empty(list) ? null : list.get(0); } public static A first(A[] bla) { return bla == null || bla.length == 0 ? null : bla[0]; } public static A first(IterableIterator i) { return first((Iterator) i); } public static A first(Iterator i) { return i == null || !i.hasNext() ? null : i.next(); } public static A first(Iterable i) { if (i == null) return null; Iterator it = i.iterator(); return it.hasNext() ? it.next() : null; } public static Character first(String s) { return empty(s) ? null : s.charAt(0); } public static A first(Pair p) { return p == null ? null : p.a; } public static TreeSet asCISet(Iterable c) { return toCaseInsensitiveSet(c); } public static TreeSet asCISet(String... x) { return toCaseInsensitiveSet(x); } public static List tok_splitAtComma(String s) { return tok_splitAtComma(javaTok(s)); } public static List tok_splitAtComma(List tok) { List out = new ArrayList(); for (int i = 0; i < l(tok); i++) { int j = smartIndexOf(tok, ",", i); out.add(joinSubList(tok, i + 1, j - 1)); i = j; } return out; } public static String uncurly(String s) { return tok_unCurlyBracket(s); } public static A printStruct(String prefix, A a) { printStructure(prefix, a); return a; } public static A printStruct(A a) { printStructure(a); return a; } public static String dropJavaComments(String s) { return javaDropAllComments(s); } public static List dropJavaComments(List tok) { return javaDropAllComments(tok); } public static void _close(AutoCloseable c) { if (c != null) try { c.close(); } catch (Throwable e) { if (c instanceof javax.imageio.stream.ImageOutputStream) return; else throw rethrow(e); } } public static RuntimeException rethrow(Throwable t) { if (t instanceof Error) _handleError((Error) t); throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); } public static RuntimeException rethrow(String msg, Throwable t) { throw new RuntimeException(msg, t); } public static String ai_superSimpleVerbCorrector(String s) { List tok = javaTokNPunctuation(s); for (int i = 1; i < l(tok) - 2; i += 2) if (mechCISetContains("Plural nouns", tok.get(i))) tokReplaceIfNotNull(tok, i + 2, mechCIMapGet("Singular verb => Plural", tok.get(i + 2))); return join(tok); } public static String tok_dropCurlyBrackets(String s) { return join(tok_dropCurlyBrackets(javaTok(s))); } public static List tok_dropCurlyBrackets(List tok) { for (int i = 1; i < l(tok); i += 2) { while (eqGetOneOf(tok, i, "{", "}")) { tokAppend(tok, i - 1, tok.get(i + 1)); removeSubList(tok, i, i + 2); } } return tok; } public static List codeTokens_lazy_uncurly(List tok) { return lazilyMap("uncurly", codeTokens_lazy(tok)); } public static Map> javaTokWithBrackets_cached_cache = synchronizedMRUCache(100); public static List javaTokWithBrackets_cached(String s) { List tok = javaTokWithBrackets_cached_cache.get(s); if (tok == null) javaTokWithBrackets_cached_cache.put(s, tok = javaTokWithBrackets(s)); return tok; } public static Class run(String progID, String... args) { Class main = hotwire(progID); callMain(main, args); return main; } public static String mapEachLine(Object f, String text) { return lines_possiblyRTrim(text, mapLines(f, text)); } public static String mapEachLine(String f, String text) { return mapEachLine((Object) f, text); } public static String mapEachLine(String text, Object f) { return mapEachLine(f, text); } public static String mapEachLine(String text, IF1 f) { return mapEachLine(f, text); } public static String ai_groupSimplestNounPhrases(String s) { return ai_groupUsingProductions(s, ai_standardBUParserWithProductions(" + = ")); } public static TreeMap asCIMap(Map map) { return asCaseInsensitiveMap(map); } public static Map minusSignsSections(String text) { List lines = lines(text); List l = indicesOfStartingWithTrim(lines, "--"); LinkedHashMap out = new LinkedHashMap(); if (nempty(l)) mapPutIfNemptyValue(out, "", rtrim(lines(subList(lines, 0, first(l))))); for (int i = 0; i < l(l); i++) { int i1 = l.get(i); int i2 = or(get(l, i + 1), l(lines)); String heading = trim(lines.get(i1)); int iHeading = 0; while (iHeading < l(heading) && heading.charAt(iHeading) == '-') iHeading++; heading = trim(substring(heading, iHeading)); out.put(heading, lines(subList(lines, i1 + 1, i2))); } return out; } public static List> ai_findDoubleArrowRulesAsPairs(String text) { List rulesRaw = ai_splitEntriesWithMultipleDoubleArrows(splitAtEmptyLines(text)); return mapNonNulls("splitAtDoubleArrow_pair", rulesRaw); } public static Set ai_wordsInBothSidesOfPair_uncurly(Pair p) { return mapCISet("uncurly", ai_wordsInBothSidesOfPair(p)); } public static void printAsciiHeading(String text) { print(asciiHeading2(text)); } public static AutoCloseable tempIndent() { threadIndent(); return new AutoCloseable() { public void close() { threadUnindent(); } }; } public static TreeSet litciset(String... items) { TreeSet set = caseInsensitiveSet(); for (String a : items) set.add(a); return set; } public static boolean matchAny(Collection patterns, String s, Matches m) { for (String pattern : patterns) if (match(pattern, s, m)) return true; return false; } public static boolean matchAny(Collection patterns, String s) { return matchAny(patterns, s, null); } public static boolean matchAny(String pat, Iterable l) { return matchAny(pat, l, null); } public static boolean matchAny(String pat, Iterable l, Matches m) { if (l != null) for (String s : l) if (match(pat, s, m)) return true; return false; } public static String formatDoubleArrowPairs_horizontallyAligned(List> l) { return asciiSideBySideWithSeparator(" => ", ll(lines(pairsA(l)), lines(pairsB(l)))); } public static List> mapPairBoth(final Object f, Iterable> l) { return map(l, new F1, Pair>() { public Pair get(Pair p) { try { return p == null ? null : pair((C) callF(f, p.a), (C) callF(f, p.b)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "p == null ? null : pair((C) callF(f, p.a), (C) callF(f, p.b))"; } }); } public static List> mapPairB(final F1 f, Iterable> l) { return mapPairBoth((Object) f, l); } public static List> multiMapToPairs(MultiMap mm) { List> out = new ArrayList(); for (A key : keys(mm)) for (B value : mm.get(key)) out.add(pair(key, value)); return out; } public static List pairsA(Collection> l) { return firstOfPairs(l); } public static List addAllAndReturnNew(Collection c, Collection b) { if (c == null || b == null) return emptyList(); List newEntries = new ArrayList(); for (A a : b) if (c.add(a)) newEntries.add(a); return newEntries; } public static HashSet lithashset(A... items) { HashSet set = new HashSet(); for (A a : items) set.add(a); return set; } public static Map ciMapWithoutKeysEqualToValues(Map map) { if (map == null) return null; Map map2 = ciMap(); for (String key : keys(map)) { String value = map.get(key); if (!eqic(key, value)) map2.put(key, value); } return map2; } public static Map zipTwoListsToCIMap_strict(List l1, List l2) { int n = l(l1); if (n != l(l2)) return null; Map map = ciMap(); for (int i = 0; i < n; i++) if (!strictPutIC(map, l1.get(i), l2.get(i))) return null; return map; } public static List withoutDollarVars(List l) { return antiFilter("isDollarVar", l); } public static List listMinusSet(Iterable l, Collection stuff) { if (l == null) return null; if (empty(stuff)) return asList(l); Set set = asSet(stuff); List l2 = new ArrayList(); for (A a : l) if (!set.contains(a)) l2.add(a); return l2; } public static List listMinusSet(Iterable l, Collection stuff, Collection stuff2) { return listMinusSet(listMinusSet(l, stuff), stuff2); } public static Set keys(Map map) { return map == null ? new HashSet() : map.keySet(); } public static Set keys(Object map) { return keys((Map) map); } public static Set keys(MultiSet ms) { return ms.keySet(); } public static Set keys(MultiMap mm) { return mm.keySet(); } public static boolean nempty(Collection c) { return !empty(c); } public static boolean nempty(CharSequence s) { return !empty(s); } public static boolean nempty(Object[] o) { return !empty(o); } public static boolean nempty(byte[] o) { return !empty(o); } public static boolean nempty(int[] o) { return !empty(o); } public static boolean nempty(Map m) { return !empty(m); } public static boolean nempty(Iterator i) { return i != null && i.hasNext(); } public static boolean nempty(Object o) { return !empty(o); } public static Map reverseMapCI_joinDuplicatesWithPlus(Map map) { MultiMap mm = reverseMapAsMultiMapCI(map); Map out = ciMap(); for (String key : keys(mm)) out.put(key, join("+", mm.get(key))); return out; } public static String join(String glue, Iterable strings) { if (strings == null) return ""; if (strings instanceof Collection) { if (((Collection) strings).size() == 1) return str(first(((Collection) strings))); } StringBuilder buf = new StringBuilder(); Iterator i = strings.iterator(); if (i.hasNext()) { buf.append(i.next()); while (i.hasNext()) buf.append(glue).append(i.next()); } return buf.toString(); } public static String join(String glue, String... strings) { return join(glue, Arrays.asList(strings)); } public static String join(Iterable strings) { return join("", strings); } public static String join(Iterable strings, String glue) { return join(glue, strings); } public static String join(String[] strings) { return join("", strings); } public static String join(String glue, Pair p) { return p == null ? "" : str(p.a) + glue + str(p.b); } public static List mapCodeTokens(Object f, List l) { List out = new ArrayList(); for (int i = 0; i < l(l); i++) { Object o = l.get(i); out.add(odd(i) ? callF(f, o) : o); } return out; } public static List mapCodeTokens(List l, Object f) { return mapCodeTokens(f, l); } public static int javaTok_n, javaTok_elements; public static boolean javaTok_opt = false; public static List javaTok(String s) { ++javaTok_n; ArrayList tok = new ArrayList(); int l = s == null ? 0 : s.length(); int i = 0, n = 0; while (i < l) { int j = i; char c, d; while (j < l) { c = s.charAt(j); d = j + 1 >= l ? '\0' : s.charAt(j + 1); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (c == '/' && d == '*') { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (c == '/' && d == '/') { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } tok.add(javaTok_substringN(s, i, j)); ++n; i = j; if (i >= l) break; c = s.charAt(i); d = i + 1 >= l ? '\0' : s.charAt(i + 1); if (c == '\'' && Character.isJavaIdentifierStart(d) && i + 2 < l && "'\\".indexOf(s.charAt(i + 2)) < 0) { j += 2; while (j < l && Character.isJavaIdentifierPart(s.charAt(j))) ++j; } else if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { int c2 = s.charAt(j); if (c2 == opener || c2 == '\n' && opener == '\'') { ++j; break; } else if (c2 == '\\' && j + 1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } else if (c == '[' && d == '[') { do ++j; while (j + 1 < l && !s.substring(j, j + 2).equals("]]")); j = Math.min(j + 2, l); } else if (c == '[' && d == '=' && i + 2 < l && s.charAt(i + 2) == '[') { do ++j; while (j + 2 < l && !s.substring(j, j + 3).equals("]=]")); j = Math.min(j + 3, l); } else ++j; tok.add(javaTok_substringC(s, i, j)); ++n; i = j; } if ((tok.size() % 2) == 0) tok.add(""); javaTok_elements += tok.size(); return tok; } public static List javaTok(List tok) { return javaTokWithExisting(join(tok), tok); } public static String getOrKeep_tryUncurlied(Map map, String a) { String v = map.get(a); if (v != null) return v; String b = uncurly(a); if (a != b && (v = map.get(b)) != null) return v; return a; } public static List javaTokWithBrackets(String s) { return javaTokPlusBrackets(s); } public static void addPairIfBAtLeast(Collection> c, A a, B b, Number threshold) { if (c != null && cmp(b, threshold) >= 0) c.add(pair(a, b)); } public static int ratioToIntPercent(double x, double y) { return roundToInt(x * 100 / y); } public static Class mc() { return main.class; } public static Object call_withVarargs(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Class c = (Class) o; _MethodCache cache = callOpt_getCache(c); Method me = cache.findStaticMethod(method, args); if (me != null) return invokeMethod(me, null, args); List methods = cache.cache.get(method); if (methods != null) methodSearch: for (Method m : methods) { { if (!(m.isVarArgs())) continue; } { if (!(isStaticMethod(m))) continue; } Object[] newArgs = massageArgsForVarArgsCall(m, args); if (newArgs != null) return invokeMethod(m, null, newArgs); } throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found"); } else { Class c = o.getClass(); _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(method, args); if (me != null) return invokeMethod(me, o, args); List methods = cache.cache.get(method); if (methods != null) methodSearch: for (Method m : methods) { { if (!(m.isVarArgs())) continue; } Object[] newArgs = massageArgsForVarArgsCall(m, args); if (newArgs != null) return invokeMethod(m, o, newArgs); } throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found in " + c); } } catch (Exception __e) { throw rethrow(__e); } } public static Class javax() { return getJavaX(); } public static void setDefaultClassFinder(Object cf) { _defaultClassFinder_value = cf; } public static HashMap findClass_fullName_cache = new HashMap(); public static Class findClass_fullName(String name) { synchronized (findClass_fullName_cache) { if (findClass_fullName_cache.containsKey(name)) return findClass_fullName_cache.get(name); Class c; try { c = Class.forName(name); } catch (ClassNotFoundException e) { c = null; } findClass_fullName_cache.put(name, c); return c; } } public static boolean startsWith(String a, String b) { return a != null && a.startsWith(b); } public static boolean startsWith(String a, char c) { return nemptyString(a) && a.charAt(0) == c; } public static boolean startsWith(String a, String b, Matches m) { if (!startsWith(a, b)) return false; m.m = new String[] { substring(a, strL(b)) }; return true; } public static boolean startsWith(List a, List b) { if (a == null || listL(b) > listL(a)) return false; for (int i = 0; i < listL(b); i++) if (neq(a.get(i), b.get(i))) return false; return true; } public static String substring(String s, int x) { return substring(s, x, strL(s)); } public static String substring(String s, int x, int y) { if (s == null) return null; if (x < 0) x = 0; if (x >= s.length()) return ""; if (y < x) y = x; if (y > s.length()) y = s.length(); return s.substring(x, y); } public static String substring(String s, IntRange r) { return r == null ? null : substring(s, r.start, r.end); } public static Object _defaultClassFinder_value = defaultDefaultClassFinder(); public static Object _defaultClassFinder() { return _defaultClassFinder_value; } public static String programID; public static String getProgramID() { return nempty(programID) ? formatSnippetIDOpt(programID) : "?"; } public static String getProgramID(Class c) { String id = (String) getOpt(c, "programID"); if (nempty(id)) return formatSnippetID(id); return "?"; } public static String getProgramID(Object o) { return getProgramID(getMainClass(o)); } public static boolean endsWithLetterOrDigit(String s) { return s != null && s.length() > 0 && Character.isLetterOrDigit(s.charAt(s.length() - 1)); } public static void ping_okInCleanUp() { if (ping_pauseAll || ping_anyActions) ping_impl(true); } public static Object getThreadLocal(Object o, String name) { ThreadLocal t = (ThreadLocal) (getOpt(o, name)); return t != null ? t.get() : null; } public static A getThreadLocal(ThreadLocal tl) { return tl == null ? null : tl.get(); } public static A getThreadLocal(ThreadLocal tl, A defaultValue) { return or(getThreadLocal(tl), defaultValue); } public static ThreadLocal print_byThread_dontCreate() { return print_byThread; } public static boolean isFalse(Object o) { return eq(false, o); } public static String fixNewLines(String s) { int i = indexOf(s, '\r'); if (i < 0) return s; int l = s.length(); StringBuilder out = new StringBuilder(l); out.append(s, 0, i); for (; i < l; i++) { char c = s.charAt(i); if (c != '\r') out.append(c); else { out.append('\n'); if (i + 1 < l && s.charAt(i + 1) == '\n') ++i; } } return out.toString(); } public static void print_append(Appendable buf, String s, int max) { try { synchronized (buf) { buf.append(s); if (buf instanceof StringBuffer) rotateStringBuffer(((StringBuffer) buf), max); else if (buf instanceof StringBuilder) rotateStringBuilder(((StringBuilder) buf), max); } } catch (Exception __e) { throw rethrow(__e); } } public static ThreadLocal print_byThread() { synchronized (print_byThread_lock) { if (print_byThread == null) print_byThread = new ThreadLocal(); } return print_byThread; } public static AutoCloseable tempInterceptPrint(F1 f) { return tempSetThreadLocal(print_byThread(), f); } public static String defaultThreadName_name; public static String defaultThreadName() { if (defaultThreadName_name == null) defaultThreadName_name = "A thread by " + programID(); return defaultThreadName_name; } public static Runnable wrapAsActivity(Object r) { return toRunnable(r); } public static Thread newThread(Object runnable) { return new Thread(_topLevelErrorHandling(toRunnable(runnable))); } public static Thread newThread(Object runnable, String name) { if (name == null) name = defaultThreadName(); return new Thread(_topLevelErrorHandling(toRunnable(runnable)), name); } public static Thread newThread(String name, Object runnable) { return newThread(runnable, name); } public static Runnable toRunnable(final Object o) { if (o instanceof Runnable) return (Runnable) o; return new Runnable() { public void run() { try { callF(o); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(o)"; } }; } public static Map _registerThread_threads; public static Object _onRegisterThread; public static Thread _registerThread(Thread t) { if (_registerThread_threads == null) _registerThread_threads = newWeakHashMap(); _registerThread_threads.put(t, true); vm_generalWeakSubMap("thread2mc").put(t, weakRef(mc())); callF(_onRegisterThread, t); return t; } public static void _registerThread() { _registerThread(Thread.currentThread()); } public static IterableIterator toLines(File f) { return linesFromFile(f); } public static List toLines(String s) { List lines = new ArrayList(); if (s == null) return lines; int start = 0; while (true) { int i = toLines_nextLineBreak(s, start); if (i < 0) { if (s.length() > start) lines.add(s.substring(start)); break; } lines.add(s.substring(start, i)); if (s.charAt(i) == '\r' && i + 1 < s.length() && s.charAt(i + 1) == '\n') i += 2; else ++i; start = i; } return lines; } public static int toLines_nextLineBreak(String s, int start) { for (int i = start; i < s.length(); i++) { char c = s.charAt(i); if (c == '\r' || c == '\n') return i; } return -1; } public static boolean emptyAfterTrim(String s) { return empty(trim(s)); } public static String fromLines(Iterable lines) { StringBuilder buf = new StringBuilder(); if (lines != null) for (Object line : lines) buf.append(str(line)).append('\n'); return buf.toString(); } public static String fromLines(String... lines) { return fromLines(asList(lines)); } public static List subList(List l, int startIndex) { return subList(l, startIndex, l(l)); } public static List subList(int startIndex, int endIndex, List l) { return subList(l, startIndex, endIndex); } public static List subList(List l, int startIndex, int endIndex) { if (l == null) return null; int n = l(l); startIndex = Math.max(0, startIndex); endIndex = Math.min(n, endIndex); if (startIndex >= endIndex) return ll(); if (startIndex == 0 && endIndex == n) return l; return l.subList(startIndex, endIndex); } public static List subList(List l, IntRange r) { return subList(l, r.start, r.end); } public static List toLinesFullTrim(String s) { List l = new ArrayList(); for (String line : toLines(s)) if (nempty(line = trim(line))) l.add(line); return l; } public static List toLinesFullTrim(File f) { List l = new ArrayList(); for (String line : linesFromFile(f)) if (nempty(line = trim(line))) l.add(line); return l; } public static String dropSuffixTrim(String suffix, String s) { return dropSuffixAndTrim(suffix, s); } public static boolean dropSuffixTrim(String suffix, String s, Matches m) { if (!endsWith(s, suffix)) return false; m.m = new String[] { dropLast(l(suffix), s) }; return true; } public static boolean empty(Collection c) { return c == null || c.isEmpty(); } public static boolean empty(CharSequence s) { return s == null || s.length() == 0; } public static boolean empty(Map map) { return map == null || map.isEmpty(); } public static boolean empty(Object[] o) { return o == null || o.length == 0; } public static boolean empty(Object o) { if (o instanceof Collection) return empty((Collection) o); if (o instanceof String) return empty((String) o); if (o instanceof Map) return empty((Map) o); if (o instanceof Object[]) return empty((Object[]) o); if (o instanceof byte[]) return empty((byte[]) o); if (o == null) return true; throw fail("unknown type for 'empty': " + getType(o)); } public static boolean empty(Iterator i) { return i == null || !i.hasNext(); } public static boolean empty(float[] a) { return a == null || a.length == 0; } public static boolean empty(int[] a) { return a == null || a.length == 0; } public static boolean empty(long[] a) { return a == null || a.length == 0; } public static boolean empty(byte[] a) { return a == null || a.length == 0; } public static boolean empty(short[] a) { return a == null || a.length == 0; } public static boolean empty(MultiSet ms) { return ms == null || ms.isEmpty(); } public static boolean empty(File f) { return getFileSize(f) == 0; } public static List cloneSubList(List l, int startIndex, int endIndex) { return newSubList(l, startIndex, endIndex); } public static List cloneSubList(List l, int startIndex) { return newSubList(l, startIndex); } public static void removeSubList(List l, int from, int to) { if (l != null) subList(l, from, to).clear(); } public static void removeSubList(List l, int from) { if (l != null) subList(l, from).clear(); } public static List parse3_plusBrackets(String s) { return dropPunctuation(javaTokPlusBrackets2(s)); } public static Map> parse3_cachedPattern_cache = synchronizedMRUCache(1000); public static synchronized List parse3_cachedPattern(String s) { List tok = parse3_cachedPattern_cache.get(s); if (tok == null) parse3_cachedPattern_cache.put(s, tok = parse3(s)); return tok; } public static boolean match3(String pat, String s) { return match3(pat, s, null); } public static boolean match3(String pat, String s, Matches matches) { if (pat == null || s == null) return false; return match3(pat, parse3_cachedInput(s), matches); } public static boolean match3(String pat, List toks, Matches matches) { List tokpat = parse3_cachedPattern(pat); return match3(tokpat, toks, matches); } public static boolean match3(List tokpat, List toks, Matches matches) { String[] m = match2(tokpat, toks); if (m == null) return false; if (matches != null) matches.m = m; return true; } public static TreeSet toCaseInsensitiveSet(Iterable c) { if (isCISet(c)) return (TreeSet) c; TreeSet set = caseInsensitiveSet(); addAll(set, c); return set; } public static TreeSet toCaseInsensitiveSet(String... x) { TreeSet set = caseInsensitiveSet(); addAll(set, x); return set; } public static int smartIndexOf(String s, String sub, int i) { if (s == null) return 0; i = s.indexOf(sub, min(i, l(s))); return i >= 0 ? i : l(s); } public static int smartIndexOf(String s, int i, char c) { return smartIndexOf(s, c, i); } public static int smartIndexOf(String s, char c, int i) { if (s == null) return 0; i = s.indexOf(c, min(i, l(s))); return i >= 0 ? i : l(s); } public static int smartIndexOf(String s, String sub) { return smartIndexOf(s, sub, 0); } public static int smartIndexOf(String s, char c) { return smartIndexOf(s, c, 0); } public static int smartIndexOf(List l, A sub) { return smartIndexOf(l, sub, 0); } public static int smartIndexOf(List l, int start, A sub) { return smartIndexOf(l, sub, start); } public static int smartIndexOf(List l, A sub, int start) { int i = indexOf(l, sub, start); return i < 0 ? l(l) : i; } public static String joinSubList(List l, int i, int j) { return join(subList(l, i, j)); } public static String joinSubList(List l, int i) { return join(subList(l, i)); } public static String joinSubList(List l, IntRange r) { return r == null ? null : joinSubList(l, r.start, r.end); } public static String tok_unCurlyBracket(String s) { return isCurlyBraced(s) ? join(dropFirstThreeAndLastThree(javaTok(s))) : s; } public static void printStructure(String prefix, Object o) { if (endsWithLetter(prefix)) prefix += ": "; print(prefix + structureForUser(o)); } public static void printStructure(Object o) { print(structureForUser(o)); } public static String javaDropAllComments(String s) { return join(javaDropAllComments(javaTok(s))); } public static List javaDropAllComments(List tok) { for (int i = 0; i < l(tok); i += 2) tok.set(i, tok_javaDropCommentsFromWhitespace(tok.get(i))); return tok; } public static void _handleError(Error e) { call(javax(), "_handleError", e); } public static List javaTokNPunctuation(String s) { List tok = javaTok(s); for (int i = 1; i < l(tok); i += 2) { int j = i; String t; while (j < l(tok) && l(t = tok.get(j)) == 1 && !Character.isLetterOrDigit(t.charAt(0))) j += 2; if (j > i) replaceSubList(tok, i - 1, j, ll(joinSubList(tok, i - 1, j))); } return tok; } public static boolean mechCISetContains(String listName, String a) { return mechCISet(listName).contains(a); } public static void tokReplaceIfNotNull(List tok, int i, String t) { if (t != null) tok.set(i, t); } public static String mechCIMapGet(String listName, String a) { return mechCIMap(listName).get(a); } public static boolean eqGetOneOf(List l, int i, A... options) { return eqOneOf(get(l, i), options); } public static void tokAppend(List tok, int i, String s) { tok.set(i, tok.get(i) + s); } public static List lazilyMap(final Object f, final List l) { return new RandomAccessAbstractList() { public final int size = l(l); public HashMap data = new HashMap(); public int size() { return size; } public Object get(int i) { if (data.containsKey(i)) return data.get(i); Object o = callF(f, l.get(i)); data.put(i, o); return o; } }; } public static List codeTokens_lazy(final List tok) { return new RandomAccessAbstractList() { public final int size = l(tok) / 2; public int size() { return size; } public String get(int i) { return tok.get(i * 2 + 1); } }; } public static Map synchronizedMRUCache(int maxSize) { return synchroMap(new MRUCache(maxSize)); } public static Class hotwire(String src) { assertFalse(_inCore()); Class j = getJavaX(); if (isAndroid()) { synchronized (j) { 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; } } public static A callMain(A c, String... args) { callOpt(c, "main", new Object[] { args }); return c; } public static void callMain() { callMain(mc()); } public static String lines_possiblyRTrim(String originalText, List lines) { return endsWithNewLine(originalText) ? lines(lines) : lines_rtrim(lines); } public static List mapLines(Object f, String text) { return map(f, lines(text)); } public static List mapLines(String text, F1 f) { return mapLines(f, text); } public static List mapLines(F1 f, String text) { return map(f, lines(text)); } public static String ai_groupUsingProductions(String s, AI_BottomUpParser1 p) { p.splitSplittables = false; p.parse(javaTokNPunctuation(s)); for (AI_BottomUpParser1.Word g : p.groups()) { int i = g.wordFrom * 2 + 1, j = g.wordTo * 2 - 1; String pre = get(p.cnc, i - 1), post = get(p.cnc, j + 1); if (endsWith(pre, "{") && startsWith(post, "}")) continue; tokPrepend(p.cnc, i, "{"); tokAppend(p.cnc, j, "}"); } return join(p.cnc); } public static AI_BottomUpParser1 ai_standardBUParserWithProductions(String productionsText) { AI_BottomUpParser1 p = new AI_BottomUpParser1(); p.productionsText = productionsText; return p; } public static TreeMap asCaseInsensitiveMap(Map map) { if (isCIMap(map)) return (TreeMap) map; TreeMap m = ciMap(); m.putAll(map); return m; } public static String lines(Iterable lines) { return fromLines(lines); } public static String lines(Object[] lines) { return fromLines(asList(lines)); } public static List lines(String s) { return toLines(s); } public static List indicesOfStartingWithTrim(List l, String prefix) { List x = new ArrayList(); for (int i = 0; i < l(l); i++) if (startsWith(trim(l.get(i)), prefix)) x.add(i); return x; } public static void mapPutIfNemptyValue(Map map, A key, String value) { if (map != null && key != null && nempty(value)) map.put(key, value); } public static void mapPutIfNemptyValue(Map map, A key, B value) { if (map != null && key != null && nempty(value)) map.put(key, value); } public static String rtrim(String s) { if (s == null) return null; int i = s.length(); while (i > 0 && " \t\r\n".indexOf(s.charAt(i - 1)) >= 0) --i; return i < s.length() ? s.substring(0, i) : s; } public static A or(A a, A b) { return a != null ? a : b; } public static String trim(String s) { return s == null ? null : s.trim(); } public static String trim(StringBuilder buf) { return buf.toString().trim(); } public static String trim(StringBuffer buf) { return buf.toString().trim(); } public static List ai_splitEntriesWithMultipleDoubleArrows(List l) { List out = new ArrayList(); for (String s : unnull(l)) if (l(splitAtDoubleArrow(javaTokWithBrackets(s))) > 2) addAll(out, tlft(s)); else out.add(s); return out; } public static List mapNonNulls(Iterable l, Object f) { return mapNonNulls(f, l); } public static List mapNonNulls(Object f, Iterable l) { List x = new ArrayList(); if (l != null) for (Object o : l) addIfNotNull(x, callF(f, o)); return x; } public static List mapNonNulls(Object f, Object[] l) { List x = new ArrayList(); if (l != null) for (Object o : l) addIfNotNull(x, callF(f, o)); return x; } public static List mapNonNulls(Iterable l, F1 f) { return mapNonNulls(f, l); } public static List mapNonNulls(F1 f, Iterable l) { List x = new ArrayList(); if (l != null) for (Object o : l) addIfNotNull(x, callF(f, o)); return x; } public static List mapNonNulls(Iterable l, IF1 f) { List x = emptyList(l); if (l != null) for (A o : l) addIfNotNull(x, f.get(o)); return x; } public static Pair splitAtDoubleArrow_pair(String s) { return splitAtDoubleArrow_pair(javaTokWithBrackets(s)); } public static Pair splitAtDoubleArrow_pair(List tok) { return listToPair(splitAtDoubleArrow(tok)); } public static TreeSet mapCISet(Object f, Iterable l) { return mapToCISet(f, l); } public static Set ai_wordsInBothSidesOfPair(Pair p) { return filterCISet("hasLetterOrDigit", ciSetIntersection(javaTokWithBracketsPlusJavaTokC(p.a), javaTokWithBracketsPlusJavaTokC(p.b))); } public static String asciiHeading2(String title) { return asciiHeading(title) + "\n"; } public static void threadIndent() { print_threadIndentPlus(); } public static void threadUnindent() { print_threadIndentMinus(); } public static TreeSet caseInsensitiveSet() { return caseInsensitiveSet_treeSet(); } public static TreeSet caseInsensitiveSet(Collection c) { return caseInsensitiveSet_treeSet(c); } public static TreeSet treeSet() { return new TreeSet(); } public static String asciiSideBySideWithSeparator(String sep, List texts) { if (empty(texts)) return asciiSideBySide(texts); int rows = maxLineCount(texts); List out = new ArrayList(); for (int i = 0; i < l(texts); i++) { if (i != 0) out.add(repLine(rows, sep)); out.add(texts.get(i)); } return asciiSideBySide(out); } public static List ll(A... a) { ArrayList l = new ArrayList(a.length); if (a != null) for (A x : a) l.add(x); return l; } public static List pairsB(Collection> l) { return secondOfPairs(l); } public static List map(Iterable l, Object f) { return map(f, l); } public static List map(Object f, Iterable l) { List x = emptyList(l); if (l != null) for (Object o : l) x.add(callF(f, o)); return x; } public static List map(Iterable l, F1 f) { return map(f, l); } public static List map(F1 f, Iterable l) { List x = emptyList(l); if (l != null) for (A o : l) x.add(callF(f, o)); return x; } public static List map(IF1 f, Iterable l) { return map(l, f); } public static List map(Iterable l, IF1 f) { List x = emptyList(l); if (l != null) for (A o : l) x.add(f.get(o)); return x; } public static List map(IF1 f, A[] l) { return map(l, f); } public static List map(A[] l, IF1 f) { List x = emptyList(l); if (l != null) for (A o : l) x.add(f.get(o)); return x; } public static List map(Object f, Object[] l) { return map(f, asList(l)); } public static List map(Object[] l, Object f) { return map(f, l); } public static List map(Object f, Map map) { return map(map, f); } public static List map(Map map, Object f) { List x = new ArrayList(); if (map != null) for (Object _e : map.entrySet()) { Map.Entry e = (Map.Entry) _e; x.add(callF(f, e.getKey(), e.getValue())); } return x; } public static List map(Map map, IF2 f) { return map(map, (Object) f); } public static Pair pair(A a, B b) { return new Pair(a, b); } public static Pair pair(A a) { return new Pair(a, a); } public static List firstOfPairs(Collection> l) { List out = new ArrayList(); for (Pair p : unnull(l)) out.add(firstOfPair(p)); return out; } public static ArrayList emptyList() { return new ArrayList(); } public static ArrayList emptyList(int capacity) { return new ArrayList(max(0, capacity)); } public static ArrayList emptyList(Iterable l) { return l instanceof Collection ? emptyList(((Collection) l).size()) : emptyList(); } public static ArrayList emptyList(Object[] l) { return emptyList(l(l)); } public static ArrayList emptyList(Class c) { return new ArrayList(); } public static TreeMap ciMap() { return caseInsensitiveMap(); } public static boolean eqic(String a, String b) { if ((a == null) != (b == null)) return false; if (a == null) return true; return a.equalsIgnoreCase(b); } public static boolean eqic(char a, char b) { if (a == b) return true; char u1 = Character.toUpperCase(a); char u2 = Character.toUpperCase(b); if (u1 == u2) return true; return Character.toLowerCase(u1) == Character.toLowerCase(u2); } public static boolean strictPutIC(Map map, A key, String value) { if (!(map != null && key != null && value != null)) return true; String old = map.get(key); if (old == null) { map.put(key, value); return true; } return eqic(old, value); } public static List antiFilter(Iterable c, Object pred) { if (pred instanceof F1) return antiFilter(c, (F1) pred); List x = new ArrayList(); if (c != null) for (Object o : c) if (!isTrue(callF(pred, o))) x.add(o); return x; } public static List antiFilter(Object pred, Iterable c) { return antiFilter(c, pred); } public static List antiFilter(Object pred, Object[] c) { return antiFilter(pred, wrapArrayAsList(c)); } public static List antiFilter(Iterable c, F1 pred) { List x = new ArrayList(); if (c != null) for (B o : c) if (!pred.get(o).booleanValue()) x.add(o); return x; } public static List antiFilter(F1 pred, Iterable c) { return antiFilter(c, pred); } public static List antiFilter(Iterable c, IF1 pred) { List x = new ArrayList(); if (c != null) for (B o : c) if (!pred.get(o).booleanValue()) x.add(o); return x; } public static List antiFilter(IF1 pred, Iterable c) { return antiFilter(c, pred); } public static boolean isDollarVar(String s) { return startsWith(s, '$') && isJavaIdentifierAfter(s, 1); } public static Set asSet(Object[] array) { HashSet set = new HashSet(); for (Object o : array) if (o != null) set.add(o); return set; } public static Set asSet(String[] array) { TreeSet set = new TreeSet(); for (String o : array) if (o != null) set.add(o); return set; } public static Set asSet(Iterable l) { if (l instanceof Set) return (Set) l; HashSet set = new HashSet(); for (A o : unnull(l)) if (o != null) set.add(o); return set; } public static MultiMap reverseMapAsMultiMapCI(Map map) { return reverseMapAsMultiMap(map, (MultiMap) ciMultiMap()); } public static String str(Object o) { return o == null ? "null" : o.toString(); } public static String str(char[] c) { return new String(c); } public static boolean odd(int i) { return (i & 1) != 0; } public static boolean odd(long i) { return (i & 1) != 0; } public static boolean odd(BigInteger i) { return odd(toInt(i)); } public static String javaTok_substringN(String s, int i, int j) { if (i == j) return ""; if (j == i + 1 && s.charAt(i) == ' ') return " "; return s.substring(i, j); } public static String javaTok_substringC(String s, int i, int j) { return s.substring(i, j); } public static List javaTokWithExisting(String s, List existing) { ++javaTok_n; int nExisting = javaTok_opt && existing != null ? existing.size() : 0; ArrayList tok = existing != null ? new ArrayList(nExisting) : new ArrayList(); int l = s.length(); int i = 0, n = 0; while (i < l) { int j = i; char c, d; while (j < l) { c = s.charAt(j); d = j + 1 >= l ? '\0' : s.charAt(j + 1); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (c == '/' && d == '*') { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (c == '/' && d == '/') { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } if (n < nExisting && javaTokWithExisting_isCopyable(existing.get(n), s, i, j)) tok.add(existing.get(n)); else tok.add(javaTok_substringN(s, i, j)); ++n; i = j; if (i >= l) break; c = s.charAt(i); d = i + 1 >= l ? '\0' : s.charAt(i + 1); if (c == '\'' && Character.isJavaIdentifierStart(d) && i + 2 < l && "'\\".indexOf(s.charAt(i + 2)) < 0) { j += 2; while (j < l && Character.isJavaIdentifierPart(s.charAt(j))) ++j; } else if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } else if (c == '[' && d == '[') { do ++j; while (j + 1 < l && !s.substring(j, j + 2).equals("]]")); j = Math.min(j + 2, l); } else if (c == '[' && d == '=' && i + 2 < l && s.charAt(i + 2) == '[') { do ++j; while (j + 2 < l && !s.substring(j, j + 3).equals("]=]")); j = Math.min(j + 3, l); } else ++j; if (n < nExisting && javaTokWithExisting_isCopyable(existing.get(n), s, i, j)) tok.add(existing.get(n)); else tok.add(javaTok_substringC(s, i, j)); ++n; i = j; } if ((tok.size() % 2) == 0) tok.add(""); javaTok_elements += tok.size(); return tok; } public static boolean javaTokWithExisting_isCopyable(String t, String s, int i, int j) { return t.length() == j - i && s.regionMatches(i, t, 0, j - i); } public static List javaTokPlusBrackets(String s) { return tok_combineRoundOrCurlyBrackets_keep(javaTok(s)); } public static int cmp(Number a, Number b) { return a == null ? b == null ? 0 : -1 : cmp(a.doubleValue(), b.doubleValue()); } public static int cmp(double a, double b) { return a < b ? -1 : a == b ? 0 : 1; } public 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); } public static int roundToInt(double d) { return (int) Math.round(d); } public static final Map callOpt_cache = newDangerousWeakHashMap(); public static Object callOpt_cached(Object o, String methodName, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Class c = (Class) o; _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(methodName, args); if (me == null || (me.getModifiers() & Modifier.STATIC) == 0) return null; return invokeMethod(me, null, args); } else { Class c = o.getClass(); _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(methodName, args); if (me == null) return null; return invokeMethod(me, o, args); } } catch (Exception __e) { throw rethrow(__e); } } public static _MethodCache callOpt_getCache(Class c) { synchronized (callOpt_cache) { _MethodCache cache = callOpt_cache.get(c); if (cache == null) callOpt_cache.put(c, cache = new _MethodCache(c)); return cache; } } public static Object invokeMethod(Method m, Object o, Object... args) { try { try { return m.invoke(o, args); } catch (InvocationTargetException e) { throw rethrow(getExceptionCause(e)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(e.getMessage() + " - was calling: " + m + ", args: " + joinWithSpace(classNames(args))); } } catch (Exception __e) { throw rethrow(__e); } } public static boolean isStaticMethod(Method m) { return methodIsStatic(m); } public static Object[] massageArgsForVarArgsCall(Method m, Object[] args) { Class[] types = m.getParameterTypes(); int n = types.length - 1, nArgs = args.length; if (nArgs < n) return null; for (int i = 0; i < n; i++) if (!argumentCompatibleWithType(args[i], types[i])) return null; Class varArgType = types[n].getComponentType(); for (int i = n; i < nArgs; i++) if (!argumentCompatibleWithType(args[i], varArgType)) return null; Object[] newArgs = new Object[n + 1]; arraycopy(args, 0, newArgs, 0, n); Object[] varArgs = arrayOfType(varArgType, nArgs - n); arraycopy(args, n, varArgs, 0, nArgs - n); newArgs[n] = varArgs; return newArgs; } public static String joinWithComma(Collection c) { return join(", ", c); } public static String joinWithComma(String... c) { return join(", ", c); } public static String joinWithComma(Pair p) { return p == null ? "" : joinWithComma(str(p.a), str(p.b)); } public static List classNames(Collection l) { return getClassNames(l); } public static List classNames(Object[] l) { return getClassNames(Arrays.asList(l)); } public static Class __javax; public static Class getJavaX() { try { return __javax; } catch (Exception __e) { throw rethrow(__e); } } public static boolean nemptyString(String s) { return s != null && s.length() > 0; } public static int strL(String s) { return s == null ? 0 : s.length(); } public static int listL(Collection l) { return l == null ? 0 : l.size(); } public static boolean neq(Object a, Object b) { return !eq(a, b); } public static Object defaultDefaultClassFinder() { return new F1() { public Class get(String name) { try { Class c = findClass_fullName(name); if (c != null) return c; if (startsWith(name, "loadableUtils.utils$")) return findClass_fullName("main" + substring(name, 19)); return null; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Class c = findClass_fullName(name);\r\n if (c != null) ret c;\r\n if (start..."; } }; } public static String formatSnippetIDOpt(String s) { return isSnippetID(s) ? formatSnippetID(s) : s; } public static String formatSnippetID(String id) { return "#" + parseSnippetID(id); } public static String formatSnippetID(long id) { return "#" + id; } public static Class getMainClass() { return mc(); } public static Class getMainClass(Object o) { try { if (o == null) return null; if (o instanceof Class && eq(((Class) o).getName(), "x30")) return (Class) o; return (o instanceof Class ? (Class) o : o.getClass()).getClassLoader().loadClass("main"); } catch (Exception __e) { throw rethrow(__e); } } public static volatile boolean ping_pauseAll = false; public static int ping_sleep = 100; public static volatile boolean ping_anyActions = false; public static Map ping_actions = newWeakHashMap(); public static ThreadLocal ping_isCleanUpThread = new ThreadLocal(); public static boolean ping() { if (ping_pauseAll || ping_anyActions) ping_impl(true); return true; } public static boolean ping_impl(boolean okInCleanUp) { try { if (ping_pauseAll && !isAWTThread()) { do Thread.sleep(ping_sleep); while (ping_pauseAll); return true; } if (ping_anyActions) { 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); } } public static boolean eq(Object a, Object b) { return a == b || (a == null ? b == null : b != null && a.equals(b)); } public static void rotateStringBuffer(StringBuffer buf, int max) { try { if (buf == null) return; synchronized (buf) { if (buf.length() <= max) return; try { int newLength = max / 2; int ofs = buf.length() - newLength; String newString = buf.substring(ofs); buf.setLength(0); buf.append("[...] ").append(newString); } catch (Exception e) { buf.setLength(0); } buf.trimToSize(); } } catch (Exception __e) { throw rethrow(__e); } } public static void rotateStringBuilder(StringBuilder buf, int max) { try { if (buf == null) return; synchronized (buf) { if (buf.length() <= max) return; try { int newLength = max / 2; int ofs = buf.length() - newLength; String newString = buf.substring(ofs); buf.setLength(0); buf.append("[...] ").append(newString); } catch (Exception e) { buf.setLength(0); } buf.trimToSize(); } } catch (Exception __e) { throw rethrow(__e); } } public static AutoCloseable tempSetThreadLocal(final ThreadLocal tl, A a) { if (tl == null) return null; final A prev = setThreadLocal(tl, a); return new AutoCloseable() { public String toString() { return "tl.set(prev);"; } public void close() throws Exception { tl.set(prev); } }; } public static Runnable _topLevelErrorHandling(final Runnable runnable) { final Object info = _threadInfo(); return new Runnable() { public void run() { try { try { _threadInheritInfo(info); runnable.run(); } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall {\r\n _threadInheritInfo(info);\r\n runnable.run();\r\n }"; } }; } public static WeakReference weakRef(A a) { return newWeakReference(a); } public static CloseableIterableIterator linesFromFile(File f) { try { if (!f.exists()) return emptyCloseableIterableIterator(); if (ewic(f.getName(), ".gz")) return linesFromReader(utf8bufferedReader(newGZIPInputStream(f))); return linesFromReader(utf8bufferedReader(f)); } catch (Exception __e) { throw rethrow(__e); } } public static CloseableIterableIterator linesFromFile(String path) { return linesFromFile(newFile(path)); } public static String dropSuffixAndTrim(String suffix, String s) { return trim(dropSuffix(suffix, s)); } public static boolean endsWith(String a, String b) { return a != null && a.endsWith(b); } public static boolean endsWith(String a, char c) { return nempty(a) && lastChar(a) == c; } public static boolean endsWith(String a, String b, Matches m) { if (!endsWith(a, b)) return false; m.m = new String[] { dropLast(l(b), a) }; return true; } public static String[] dropLast(String[] a, int n) { n = Math.min(n, a.length); String[] b = new String[a.length - n]; System.arraycopy(a, 0, b, 0, b.length); return b; } public static List dropLast(List l) { return subList(l, 0, l(l) - 1); } public static List dropLast(int n, List l) { return subList(l, 0, l(l) - n); } public static List dropLast(Iterable l) { return dropLast(asList(l)); } public static String dropLast(String s) { return substring(s, 0, l(s) - 1); } public static String dropLast(String s, int n) { return substring(s, 0, l(s) - n); } public static String dropLast(int n, String s) { return dropLast(s, n); } public static String getType(Object o) { return getClassName(o); } public static long getFileSize(String path) { return path == null ? 0 : new File(path).length(); } public static long getFileSize(File f) { return f == null ? 0 : f.length(); } public static List newSubList(List l, int startIndex, int endIndex) { return cloneList(subList(l, startIndex, endIndex)); } public static List newSubList(List l, int startIndex) { return cloneList(subList(l, startIndex)); } public static List dropPunctuation_keep = ll("*", "<", ">"); public static List dropPunctuation(List tok) { tok = new ArrayList(tok); for (int i = 1; i < tok.size(); i += 2) { String t = tok.get(i); if (t.length() == 1 && !Character.isLetter(t.charAt(0)) && !Character.isDigit(t.charAt(0)) && !dropPunctuation_keep.contains(t)) { tok.set(i - 1, tok.get(i - 1) + tok.get(i + 1)); tok.remove(i); tok.remove(i); i -= 2; } } return tok; } public static String dropPunctuation(String s) { return join(dropPunctuation(nlTok(s))); } public static List javaTokPlusBrackets2(String s) { return tok_combineRoundCurlySquareBrackets_keep(javaTok(s)); } public static List parse3(String s) { return dropPunctuation(javaTokPlusPeriod(s)); } public static Map> parse3_cachedInput_cache = synchronizedMRUCache(1000); public static List parse3_cachedInput(String s) { List tok = parse3_cachedInput_cache.get(s); if (tok == null) parse3_cachedInput_cache.put(s, tok = parse3(s)); return tok; } public static String[] match2(List pat, List tok) { int i = pat.indexOf("..."); if (i < 0) return match2_match(pat, tok); pat = new ArrayList(pat); pat.set(i, "*"); while (pat.size() < tok.size()) { pat.add(i, "*"); pat.add(i + 1, ""); } return match2_match(pat, tok); } public static String[] match2_match(List pat, List tok) { List result = new ArrayList(); if (pat.size() != tok.size()) { return null; } for (int i = 1; i < pat.size(); i += 2) { String p = pat.get(i), t = tok.get(i); if (eq(p, "*")) result.add(t); else if (!equalsIgnoreCase(unquote(p), unquote(t))) return null; } return result.toArray(new String[result.size()]); } public static boolean isCISet(Iterable l) { return l instanceof TreeSet && ((TreeSet) l).comparator() == caseInsensitiveComparator(); } public static void addAll(Collection c, Iterable b) { if (c != null && b != null) for (A a : b) c.add(a); } public static boolean addAll(Collection c, Collection b) { return c != null && b != null && c.addAll(b); } public static boolean addAll(Collection c, B... b) { return c != null && c.addAll(Arrays.asList(b)); } public static Map addAll(Map a, Map b) { if (a != null) a.putAll(b); return a; } public static int min(int a, int b) { return Math.min(a, b); } public static long min(long a, long b) { return Math.min(a, b); } public static float min(float a, float b) { return Math.min(a, b); } public static float min(float a, float b, float c) { return min(min(a, b), c); } public static double min(double a, double b) { return Math.min(a, b); } public static double min(double[] c) { double x = Double.MAX_VALUE; for (double d : c) x = Math.min(x, d); return x; } public static float min(float[] c) { float x = Float.MAX_VALUE; for (float d : c) x = Math.min(x, d); return x; } public static byte min(byte[] c) { byte x = 127; for (byte d : c) if (d < x) x = d; return x; } public static short min(short[] c) { short x = 0x7FFF; for (short d : c) if (d < x) x = d; return x; } public static int min(int[] c) { int x = Integer.MAX_VALUE; for (int d : c) if (d < x) x = d; return x; } public static boolean isCurlyBraced(String s) { List tok = tok_combineCurlyBrackets_keep(javaTok(s)); return l(tok) == 3 && startsWithAndEndsWith(tok.get(1), "{", "}"); } public static List dropFirstThreeAndLastThree(List l) { return dropFirstAndLast(3, l); } public static boolean endsWithLetter(String s) { return nempty(s) && isLetter(last(s)); } public static String structureForUser(Object o) { return beautifyStructure(struct_noStringSharing(o)); } public static String tok_javaDropCommentsFromWhitespace(String s) { int l = l(s), j = 0; StringBuilder buf = new StringBuilder(); while (j < l) { char c = s.charAt(j); char d = j + 1 >= l ? '\0' : s.charAt(j + 1); if (c == '/' && d == '*') { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (c == '/' && d == '/') { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else { buf.append(c); ++j; } } return str(buf); } public static List replaceSubList(List l, List x, List y) { return replaceSublist(l, x, y); } public static List replaceSubList(List l, int fromIndex, int toIndex, List y) { return replaceSublist(l, fromIndex, toIndex, y); } public static MechListDependentCaches> mechCISet_cache = new MechListDependentCaches(new F1>() { public TreeSet get(String text) { try { return asCISet(tlft(text)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "asCISet(tlft(text))"; } }); public static TreeSet mechCISet(String name) { return mechCISet_cache.get(name); } public static Map mechCIMap(String listName) { return mechMapCI(listName); } public static boolean eqOneOf(Object o, Object... l) { for (Object x : l) if (eq(o, x)) return true; return false; } public static Map synchroMap() { return synchroHashMap(); } public static Map synchroMap(Map map) { return Collections.synchronizedMap(map); } public static void assertFalse(Object o) { if (!(eq(o, false))) throw fail(str(o)); } public static boolean assertFalse(boolean b) { if (b) throw fail("oops"); return b; } public static boolean assertFalse(String msg, boolean b) { if (b) throw fail(msg); return b; } public static boolean _inCore() { return false; } public static String quote(Object o) { if (o == null) return "null"; return quote(str(o)); } public static String quote(String s) { if (s == null) return "null"; StringBuilder out = new StringBuilder((int) (l(s) * 1.5 + 2)); quote_impl(s, out); return out.toString(); } public static void quote_impl(String s, StringBuilder out) { out.append('"'); int l = s.length(); for (int i = 0; i < l; i++) { char c = s.charAt(i); if (c == '\\' || c == '"') out.append('\\').append(c); else if (c == '\r') out.append("\\r"); else if (c == '\n') out.append("\\n"); else if (c == '\0') out.append("\\0"); else out.append(c); } out.append('"'); } public static List hotwire_copyOver_after = synchroList(); public static void hotwire_copyOver(Class c) { for (String field : ll("print_log", "print_silent", "androidContext", "_userHome")) setOptIfNotNull(c, field, getOpt(mc(), field)); setOptIfNotNull(c, "mainBot", getMainBot()); setOpt(c, "creator_class", new WeakReference(mc())); pcallFAll(hotwire_copyOver_after, c); } public static boolean endsWithNewLine(String s) { return endsWith(s, "\n"); } public static String lines_rtrim(Collection lines) { return rtrim_fromLines(lines); } public static void tokPrepend(List tok, int i, String s) { tok.set(i, s + tok.get(i)); } public static boolean isCIMap(Map m) { return m instanceof TreeMap && ((TreeMap) m).comparator() == caseInsensitiveComparator(); } public static String unnull(String s) { return s == null ? "" : s; } public static Collection unnull(Collection l) { return l == null ? emptyList() : l; } public static List unnull(List l) { return l == null ? emptyList() : l; } public static Map unnull(Map l) { return l == null ? emptyMap() : l; } public static Iterable unnull(Iterable i) { return i == null ? emptyList() : i; } public static A[] unnull(A[] a) { return a == null ? (A[]) new Object[0] : a; } public static BitSet unnull(BitSet b) { return b == null ? new BitSet() : b; } public static Pt unnull(Pt p) { return p == null ? new Pt() : p; } public static Pair unnull(Pair p) { return p != null ? p : new Pair(null, null); } public static List splitAtDoubleArrow(String s) { return splitAtDoubleArrow(javaTok(s)); } public static List splitAtDoubleArrow(List tok) { List l = new ArrayList(); int i = 0; while (i < l(tok)) { int j = indexOfSubList(tok, splitAtDoubleArrow_tok(), i); if (i >= l(tok)) break; if (j < 0) j = l(tok); l.add(trimJoin(tok.subList(i, j))); i = j + 3; } return l; } public static void addIfNotNull(Collection l, A a) { if (a != null && l != null) l.add(a); } public static void addIfNotNull(MultiSet ms, A a) { if (a != null && ms != null) ms.add(a); } public static Pair listToPair(List l) { return l(l) != 2 ? null : pair(first(l), second(l)); } public static TreeSet mapToCISet(Object f, Iterable l) { TreeSet x = ciSet(); if (l != null) for (Object o : l) addIfNotNull(x, (String) callF(f, o)); return x; } public static Set filterCISet(Iterable c, Object pred) { Set x = ciSet(); if (c != null) for (String o : c) if (isTrue(callF(pred, o))) x.add(o); return x; } public static Set filterCISet(Object pred, Iterable c) { return filterCISet(c, pred); } public static boolean hasLetterOrDigit(String s) { return containsLetterOrDigit(s); } public static Set ciSetIntersection(Collection a, Collection b) { Set set = ciSet(); Set bSet = asCISet(b); for (String x : a) if (bSet.contains(x)) set.add(x); return set; } public static List javaTokWithBracketsPlusJavaTokC(String s) { return concatLists(javaTokPlusBracketsC(s), javaTokC(s)); } public static String asciiHeading(String title) { return "\n" + title + "\n" + rep(l(title), '-'); } public static void print_threadIndentPlus() { print_setPrefixForThread(print_getPrefixForThread() + " "); } public static void print_threadIndentMinus() { print_setPrefixForThread(dropSuffix(" ", print_getPrefixForThread())); } public static TreeSet caseInsensitiveSet_treeSet() { return new TreeSet(caseInsensitiveComparator()); } public static TreeSet caseInsensitiveSet_treeSet(Collection c) { return toCaseInsensitiveSet_treeSet(c); } public static String asciiSideBySide(List texts) { if (l(texts) < 2) return unnull(first(texts)); if (l(texts) > 2) return asciiSideBySide(ll(asciiSideBySide(dropLast(texts)), last(texts))); List l1 = lines(first(texts)), l2 = lines(second(texts)); int col1 = maxStringLength(l1), n = max(l(l1), l(l2)); StringBuilder buf = new StringBuilder(); for (int i = 0; i < n; i++) buf.append(rpad(unnull(get(l1, i)), col1)).append(unnull(get(l2, i))).append("\n"); return str(buf); } public static int maxLineCount(Collection l) { int n = 0; if (l != null) for (String s : l) n = max(n, countLines(s)); return n; } public static String repLine(int n, String s) { return lines(rep(n, s)); } public static List secondOfPairs(Collection> l) { return map("secondOfPair", l); } public static A firstOfPair(Pair p) { return p == null ? null : p.a; } public static int max(int a, int b) { return Math.max(a, b); } public static int max(int a, int b, int c) { return max(max(a, b), c); } public static long max(int a, long b) { return Math.max((long) a, b); } public static long max(long a, long b) { return Math.max(a, b); } public static double max(int a, double b) { return Math.max((double) a, b); } public static float max(float a, float b) { return Math.max(a, b); } public static double max(double a, double b) { return Math.max(a, b); } public static int max(Collection c) { int x = Integer.MIN_VALUE; for (int i : c) x = max(x, i); return x; } public static double max(double[] c) { if (c.length == 0) return Double.MIN_VALUE; double x = c[0]; for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]); return x; } public static float max(float[] c) { if (c.length == 0) return Float.MAX_VALUE; float x = c[0]; for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]); return x; } public static byte max(byte[] c) { byte x = -128; for (byte d : c) if (d > x) x = d; return x; } public static short max(short[] c) { short x = -0x8000; for (short d : c) if (d > x) x = d; return x; } public static int max(int[] c) { int x = Integer.MIN_VALUE; for (int d : c) if (d > x) x = d; return x; } public static TreeMap caseInsensitiveMap() { return new TreeMap(caseInsensitiveComparator()); } public static String asString(Object o) { return o == null ? null : o.toString(); } public static List wrapArrayAsList(A[] a) { return a == null ? null : Arrays.asList(a); } public static boolean isJavaIdentifierAfter(String s, int i) { int n = l(s); if (i >= n || !Character.isJavaIdentifierStart(s.charAt(i))) return false; for (i++; i < n; i++) if (!Character.isJavaIdentifierPart(s.charAt(i))) return false; return true; } public static MultiMap reverseMapAsMultiMap(Map map) { return reverseMapAsMultiMap(map, new MultiMap()); } public static MultiMap reverseMapAsMultiMap(Map map, MultiMap mm) { for (A key : keys(map)) mm.put(map.get(key), key); return mm; } public static MultiMap ciMultiMap() { return caseInsensitiveMultiMap(); } public static int toInt(Object o) { if (o == null) return 0; if (o instanceof Number) return ((Number) o).intValue(); if (o instanceof String) return parseInt((String) o); throw fail("woot not int: " + getClassName(o)); } public static int toInt(long l) { if (l != (int) l) throw fail("Too large for int: " + l); return (int) l; } public static List tok_combineRoundOrCurlyBrackets_keep(List tok) { List l = new ArrayList(); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && eqOneOf(t, "{", "(")) { int j = findEndOfBracketPart(tok, i); l.add(joinSubList(tok, i, j)); i = j - 1; } else l.add(t); } return l; } public static Throwable getExceptionCause(Throwable e) { Throwable c = e.getCause(); return c != null ? c : e; } public static String joinWithSpace(Collection c) { return join(" ", c); } public static String joinWithSpace(String... c) { return join(" ", c); } public static boolean methodIsStatic(Method m) { return (m.getModifiers() & Modifier.STATIC) != 0; } public static boolean argumentCompatibleWithType(Object arg, Class type) { return arg == null ? !type.isPrimitive() : isInstanceX(type, arg); } public static void arraycopy(Object[] a, Object[] b) { if (a != null && b != null) arraycopy(a, 0, b, 0, min(a.length, b.length)); } public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int n) { if (n != 0) System.arraycopy(src, srcPos, dest, destPos, n); } public static A[] arrayOfType(Class type, int n) { return makeArray(type, n); } public static A[] arrayOfType(int n, Class type) { return arrayOfType(type, n); } public static List getClassNames(Collection l) { List out = new ArrayList(); if (l != null) for (Object o : l) out.add(o == null ? null : getClassName(o)); return out; } public static B secondOfPair(Pair p) { return p == null ? null : p.b; } public static boolean isSnippetID(String s) { try { parseSnippetID(s); return true; } catch (RuntimeException e) { return false; } } public static long parseSnippetID(String snippetID) { long id = Long.parseLong(shortenSnippetID(snippetID)); if (id == 0) throw fail("0 is not a snippet ID"); return id; } public static boolean isAWTThread() { if (isAndroid()) return false; if (isHeadless()) return false; return isAWTThread_awt(); } public static boolean isAWTThread_awt() { return SwingUtilities.isEventDispatchThread(); } public static void failIfUnlicensed() { assertTrue("license off", licensed()); } public static Thread currentThread() { return Thread.currentThread(); } public static A setThreadLocal(ThreadLocal tl, A value) { if (tl == null) return null; A old = tl.get(); tl.set(value); return old; } public static volatile PersistableThrowable _handleException_lastException; public static List _handleException_onException = synchroList(ll("printStackTrace2")); public static void _handleException(Throwable e) { _handleException_lastException = persistableThrowable(e); Throwable e2 = innerException(e); if (e2.getClass() == RuntimeException.class && eq(e2.getMessage(), "Thread cancelled.") || e2 instanceof InterruptedException) return; for (Object f : cloneList(_handleException_onException)) try { callF(f, e); } catch (Throwable e3) { printStackTrace2(e3); } } public static WeakReference newWeakReference(A a) { return a == null ? null : new WeakReference(a); } public static CloseableIterableIterator emptyCloseableIterableIterator_instance = new CloseableIterableIterator() { public Object next() { throw fail(); } public boolean hasNext() { return false; } }; public static CloseableIterableIterator emptyCloseableIterableIterator() { return emptyCloseableIterableIterator_instance; } public static boolean ewic(String a, String b) { return endsWithIgnoreCase(a, b); } public static boolean ewic(String a, String b, Matches m) { return endsWithIgnoreCase(a, b, m); } public static CloseableIterableIterator linesFromReader(Reader r) { final BufferedReader br = bufferedReader(r); return iteratorFromFunction_f0_autoCloseable(new F0() { public String get() { try { return readLineFromReaderWithClose(br); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret readLineFromReaderWithClose(br);"; } }, _wrapIOCloseable(r)); } public static BufferedReader utf8bufferedReader(InputStream in) { try { return bufferedReader(_registerIOWrap(new InputStreamReader(in, "UTF-8"), in)); } catch (Exception __e) { throw rethrow(__e); } } public static BufferedReader utf8bufferedReader(File f) { try { return utf8bufferedReader(newFileInputStream(f)); } catch (Exception __e) { throw rethrow(__e); } } public static GZIPInputStream newGZIPInputStream(File f) { return gzInputStream(f); } public static GZIPInputStream newGZIPInputStream(InputStream in) { return gzInputStream(in); } public static File newFile(File base, String... names) { for (String name : names) base = new File(base, name); return base; } public static File newFile(String name) { return name == null ? null : new File(name); } public static File newFile(String base, String... names) { return newFile(newFile(base), names); } public static String dropSuffix(String suffix, String s) { return s.endsWith(suffix) ? s.substring(0, l(s) - l(suffix)) : s; } public static char lastChar(String s) { return empty(s) ? '\0' : s.charAt(l(s) - 1); } public static String getClassName(Object o) { return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName(); } public static ArrayList cloneList(Iterable l) { return l instanceof Collection ? cloneList((Collection) l) : asList(l); } public static ArrayList cloneList(Collection l) { if (l == null) return new ArrayList(); synchronized (collectionMutex(l)) { return new ArrayList(l); } } public static List nlTok(String s) { return javaTokPlusPeriod(s); } public static List tok_combineRoundCurlySquareBrackets_keep(List tok) { List l = new ArrayList(); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && eqOneOf(t, "{", "(", "[")) { int j = findEndOfBracketPart2(tok, i); l.add(joinSubList(tok, i, j)); i = j - 1; } else l.add(t); } return l; } public static List javaTokPlusPeriod(String s) { List tok = new ArrayList(); if (s == null) return tok; int l = s.length(); int i = 0; while (i < l) { int j = i; char c; String cc; while (j < l) { c = s.charAt(j); cc = s.substring(j, Math.min(j + 2, l)); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (cc.equals("/*")) { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (cc.equals("//")) { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } tok.add(s.substring(i, j)); i = j; if (i >= l) break; c = s.charAt(i); cc = s.substring(i, Math.min(i + 2, l)); if (c == (char) 0x201C || c == (char) 0x201D) c = '"'; if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { char _c = s.charAt(j); if (_c == (char) 0x201C || _c == (char) 0x201D) _c = '"'; if (_c == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } if (j - 1 >= i + 1) { tok.add(opener + s.substring(i + 1, j - 1) + opener); i = j; continue; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); else if (Character.isDigit(c)) do ++j; while (j < l && Character.isDigit(s.charAt(j))); else if (cc.equals("[[")) { do ++j; while (j + 1 < l && !s.substring(j, j + 2).equals("]]")); j = Math.min(j + 2, l); } else if (cc.equals("[=") && i + 2 < l && s.charAt(i + 2) == '[') { do ++j; while (j + 2 < l && !s.substring(j, j + 3).equals("]=]")); j = Math.min(j + 3, l); } else if (s.substring(j, Math.min(j + 3, l)).equals("...")) j += 3; else if (c == '$' || c == '#') do ++j; while (j < l && Character.isDigit(s.charAt(j))); else ++j; tok.add(s.substring(i, j)); i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } public static boolean equalsIgnoreCase(String a, String b) { return eqic(a, b); } public static boolean equalsIgnoreCase(char a, char b) { return eqic(a, b); } public static String unquote(String s) { if (s == null) return null; if (startsWith(s, '[')) { int i = 1; while (i < s.length() && s.charAt(i) == '=') ++i; if (i < s.length() && s.charAt(i) == '[') { String m = s.substring(1, i); if (s.endsWith("]" + m + "]")) return s.substring(i + 1, s.length() - i - 1); } } if (s.length() > 1) { char c = s.charAt(0); if (c == '\"' || c == '\'') { int l = endsWith(s, c) ? s.length() - 1 : s.length(); StringBuilder sb = new StringBuilder(l - 1); for (int i = 1; i < l; i++) { char ch = s.charAt(i); if (ch == '\\') { char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1); if (nextChar >= '0' && nextChar <= '7') { String code = "" + nextChar; i++; if ((i < l - 1) && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '7') { code += s.charAt(i + 1); i++; if ((i < l - 1) && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '7') { code += s.charAt(i + 1); i++; } } sb.append((char) Integer.parseInt(code, 8)); continue; } switch(nextChar) { case '\"': ch = '\"'; break; case '\\': ch = '\\'; break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case '\'': ch = '\''; break; case 'u': if (i >= l - 5) { ch = 'u'; break; } int code = Integer.parseInt("" + s.charAt(i + 2) + s.charAt(i + 3) + s.charAt(i + 4) + s.charAt(i + 5), 16); sb.append(Character.toChars(code)); i += 5; continue; default: ch = nextChar; } i++; } sb.append(ch); } return sb.toString(); } } return s; } public static Comparator caseInsensitiveComparator() { return betterCIComparator(); } public static List tok_combineCurlyBrackets_keep(List tok) { List l = new ArrayList(); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && eq(t, "{")) { int j = findEndOfCurlyBracketPart(tok, i); l.add(joinSubList(tok, i, j)); i = j - 1; } else l.add(t); } return l; } public static boolean startsWithAndEndsWith(String s, String prefix, String suffix) { return startsWith(s, prefix) && endsWith(s, suffix); } public static List dropFirstAndLast(int n, List l) { return new ArrayList(subList(l, n, l(l) - n)); } public static List dropFirstAndLast(List l) { return dropFirstAndLast(1, l); } public static String dropFirstAndLast(String s) { return substring(s, 1, l(s) - 1); } public static boolean isLetter(char c) { return Character.isLetter(c); } public static A last(List l) { return empty(l) ? null : l.get(l.size() - 1); } public static char last(String s) { return empty(s) ? '#' : s.charAt(l(s) - 1); } public static int last(int[] a) { return l(a) != 0 ? a[l(a) - 1] : 0; } public static A last(A[] a) { return l(a) != 0 ? a[l(a) - 1] : null; } public static A last(Iterator it) { A a = null; while (it.hasNext()) { ping(); a = it.next(); } return a; } public static String beautifyStructure(String s) { List tok = javaTok(s); structure_addTokenMarkers(tok); jreplace(tok, "lhm", ""); return join(tok); } public static String struct_noStringSharing(Object o) { structure_Data d = new structure_Data(); d.noStringSharing = true; return structure(o, d); } public static List replaceSublist(List l, List x, List y) { if (x == null) return l; int i = 0; while (true) { i = indexOfSubList(l, x, i); if (i < 0) break; removeSubList(l, i, i + l(x)); l.addAll(i, y); i += l(y); } return l; } public static List replaceSublist(List l, int fromIndex, int toIndex, List y) { removeSubList(l, fromIndex, toIndex); l.addAll(fromIndex, y); return l; } public static List replaceSublist(List l, IntRange r, List y) { return replaceSublist(l, r.start, r.end, y); } public static Map>> mechMapCI_cache = synchroMap(); public static Map mechMapCI(String listName) { Pair> p = mechMapCI_cache.get(listName); String text = mechList_raw(listName); if (p != null && eq(p.a, text)) return p.b; String parsing = first(mechList_opt(listName + " | Parsing")); Map map; if (parsing == null) map = parseDoubleArrowMapCI(tlft(text)); else map = asCIMap((Map) callAndMake(parsing, text)); p = pair(text, map); mechMapCI_cache.put(listName, p); return p.b; } public static Map synchroHashMap() { return Collections.synchronizedMap(new HashMap()); } public static List synchroList() { return Collections.synchronizedList(new ArrayList()); } public static List synchroList(List l) { return Collections.synchronizedList(l); } public static void setOptIfNotNull(Object o, String field, Object value) { if (value != null) setOpt(o, field, value); } public static Object mainBot; public static Object getMainBot() { return mainBot; } public static void pcallFAll(Collection l, Object... args) { if (l != null) for (Object f : cloneList(l)) pcallF(f, args); } public static void pcallFAll(Iterator it, Object... args) { while (it.hasNext()) pcallF(it.next(), args); } public static String rtrim_fromLines(Collection lines) { StringBuilder buf = new StringBuilder(); if (lines != null) { boolean first = true; for (Object line : lines) { if (first) first = false; else buf.append('\n'); buf.append(str(line)); } } return buf.toString(); } public static Map emptyMap() { return new HashMap(); } public static int indexOfSubList(List x, List y) { return indexOfSubList(x, y, 0); } public static int indexOfSubList(List x, List y, int i) { outer: for (; i + l(y) <= l(x); i++) { for (int j = 0; j < l(y); j++) if (neq(x.get(i + j), y.get(j))) continue outer; return i; } return -1; } public static int indexOfSubList(List x, A[] y, int i) { outer: for (; i + l(y) <= l(x); i++) { for (int j = 0; j < l(y); j++) if (neq(x.get(i + j), y[j])) continue outer; return i; } return -1; } public static List splitAtDoubleArrow_tok; public static List splitAtDoubleArrow_tok() { if (splitAtDoubleArrow_tok == null) splitAtDoubleArrow_tok = ll("=", "", ">"); return splitAtDoubleArrow_tok; } public static String trimJoin(List s) { return trim(join(s)); } public static A second(List l) { return get(l, 1); } public static A second(Iterable l) { if (l == null) return null; Iterator it = iterator(l); if (!it.hasNext()) return null; it.next(); return it.hasNext() ? it.next() : null; } public static A second(A[] bla) { return bla == null || bla.length <= 1 ? null : bla[1]; } public static B second(Pair p) { return p == null ? null : p.b; } public static char second(String s) { return charAt(s, 1); } public static TreeSet ciSet() { return caseInsensitiveSet(); } public static boolean containsLetterOrDigit(String s) { for (int i = 0; i < l(s); i++) if (isLetterOrDigit(s.charAt(i))) return true; return false; } public static List concatLists(Collection... lists) { List l = new ArrayList(); if (lists != null) for (Collection list : lists) if (list != null) l.addAll(list); return l; } public static List concatLists(Collection> lists) { List l = new ArrayList(); if (lists != null) for (Collection list : lists) if (list != null) l.addAll(list); return l; } public static List javaTokPlusBracketsC(String s) { return codeTokens(javaTokPlusBrackets(s)); } public static List javaTokC(String s) { if (s == null) return null; int l = s.length(); ArrayList tok = new ArrayList(); int i = 0; while (i < l) { int j = i; char c, d; while (j < l) { c = s.charAt(j); d = j + 1 >= l ? '\0' : s.charAt(j + 1); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (c == '/' && d == '*') { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (c == '/' && d == '/') { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } i = j; if (i >= l) break; c = s.charAt(i); d = i + 1 >= l ? '\0' : s.charAt(i + 1); if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener || s.charAt(j) == '\n') { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; } else if (c == '[' && d == '[') { do ++j; while (j + 1 < l && !s.substring(j, j + 2).equals("]]")); j = Math.min(j + 2, l); } else if (c == '[' && d == '=' && i + 2 < l && s.charAt(i + 2) == '[') { do ++j; while (j + 2 < l && !s.substring(j, j + 3).equals("]=]")); j = Math.min(j + 3, l); } else ++j; tok.add(javaTok_substringC(s, i, j)); i = j; } return tok; } public static String rep(int n, char c) { return repeat(c, n); } public static String rep(char c, int n) { return repeat(c, n); } public static List rep(A a, int n) { return repeat(a, n); } public static List rep(int n, A a) { return repeat(n, a); } public static void print_setPrefixForThread(final String prefix) { interceptPrintInThisThread(empty(prefix) ? null : new _PrintIndent(prefix)); } public static String print_getPrefixForThread() { Object o = print_byThread().get(); return o instanceof _PrintIndent ? ((_PrintIndent) o).prefix : ""; } public static TreeSet toCaseInsensitiveSet_treeSet(Iterable c) { if (isCISet(c)) return (TreeSet) c; TreeSet set = caseInsensitiveSet_treeSet(); addAll(set, c); return set; } public static TreeSet toCaseInsensitiveSet_treeSet(String... x) { TreeSet set = caseInsensitiveSet_treeSet(); addAll(set, x); return set; } public static int maxStringLength(Collection l) { int n = 0; if (l != null) for (String s : l) n = max(n, l(s)); return n; } public static String rpad(String s, int l) { return lengthOfString(s) >= l ? s : s + rep(' ', l - lengthOfString(s)); } public static String rpad(int l, String s) { return rpad(s, l); } public static int countLines(String s) { return l(toLines(s)); } public static MultiMap caseInsensitiveMultiMap() { MultiMap mm = new MultiMap(); mm.data = caseInsensitiveMap(); return mm; } public static int parseInt(String s) { return empty(s) ? 0 : Integer.parseInt(s); } public static int parseInt(char c) { return Integer.parseInt(str(c)); } public static int findEndOfBracketPart(List cnc, int i) { int j = i + 2, level = 1; while (j < cnc.size()) { if (eqOneOf(cnc.get(j), "{", "(")) ++level; else if (eqOneOf(cnc.get(j), "}", ")")) --level; if (level == 0) return j + 1; ++j; } return cnc.size(); } public static boolean isInstanceX(Class type, Object arg) { if (type == boolean.class) return arg instanceof Boolean; if (type == int.class) return arg instanceof Integer; if (type == long.class) return arg instanceof Long; if (type == float.class) return arg instanceof Float; if (type == short.class) return arg instanceof Short; if (type == char.class) return arg instanceof Character; if (type == byte.class) return arg instanceof Byte; if (type == double.class) return arg instanceof Double; return type.isInstance(arg); } public static A[] makeArray(Class type, int n) { return (A[]) Array.newInstance(type, n); } public static Throwable printStackTrace2(Throwable e) { print(getStackTrace2(e)); return e; } public static void printStackTrace2() { printStackTrace2(new Throwable()); } public static void printStackTrace2(String msg) { printStackTrace2(new Throwable(msg)); } public static Map parseDoubleLeftArrowMapCI_tlft(String s) { return parseDoubleLeftArrowMapCI(tlft(s)); } public static String shortenSnippetID(String snippetID) { if (snippetID.startsWith("#")) snippetID = snippetID.substring(1); String httpBlaBla = "http://tinybrain.de/"; if (snippetID.startsWith(httpBlaBla)) snippetID = snippetID.substring(httpBlaBla.length()); return "" + parseLong(snippetID); } public static volatile boolean licensed_yes = true; public static boolean licensed() { if (!licensed_yes) return false; ping_okInCleanUp(); return true; } public static void licensed_off() { licensed_yes = false; } public static PersistableThrowable persistableThrowable(Throwable e) { return e == null ? null : new PersistableThrowable(e); } public static Throwable innerException(Throwable e) { return getInnerException(e); } public static boolean endsWithIgnoreCase(String a, String b) { int la = l(a), lb = l(b); return la >= lb && regionMatchesIC(a, la - lb, b, 0, lb); } public static boolean endsWithIgnoreCase(String a, String b, Matches m) { if (!endsWithIgnoreCase(a, b)) return false; m.m = new String[] { substring(a, 0, l(a) - l(b)) }; return true; } public static BufferedReader bufferedReader(Reader r) { return r instanceof BufferedReader ? (BufferedReader) r : _registerIOWrap(new BufferedReader(r), r); } public static CloseableIterableIterator iteratorFromFunction_f0_autoCloseable(final F0 f, final AutoCloseable closeable) { class IFF2 extends CloseableIterableIterator { public A a; public boolean done = false; public boolean hasNext() { getNext(); return !done; } public A next() { getNext(); if (done) throw fail(); A _a = a; a = null; return _a; } public void getNext() { if (done || a != null) return; a = f.get(); done = a == null; } public void close() throws Exception { if (closeable != null) closeable.close(); } } ; return new IFF2(); } public static String readLineFromReaderWithClose(BufferedReader r) { try { String s = r.readLine(); if (s == null) r.close(); return s; } catch (Exception __e) { throw rethrow(__e); } } public static AutoCloseable _wrapIOCloseable(final AutoCloseable c) { return c == null ? null : new AutoCloseable() { public String toString() { return "c.close();\r\n _registerIO(c, null, false);"; } public void close() throws Exception { c.close(); _registerIO(c, null, false); } }; } public static A _registerIOWrap(A wrapper, Object wrapped) { return wrapper; } public static FileInputStream newFileInputStream(File path) throws IOException { return newFileInputStream(path.getPath()); } public static FileInputStream newFileInputStream(String path) throws IOException { FileInputStream f = new FileInputStream(path); _registerIO(f, path, true); return f; } public static int gzInputStream_defaultBufferSize = 65536; public static GZIPInputStream gzInputStream(File f) { try { return gzInputStream(new FileInputStream(f)); } catch (Exception __e) { throw rethrow(__e); } } public static GZIPInputStream gzInputStream(File f, int bufferSize) { try { return gzInputStream(new FileInputStream(f), bufferSize); } catch (Exception __e) { throw rethrow(__e); } } public static GZIPInputStream gzInputStream(InputStream in) { return gzInputStream(in, gzInputStream_defaultBufferSize); } public static GZIPInputStream gzInputStream(InputStream in, int bufferSize) { try { return _registerIOWrap(new GZIPInputStream(in, gzInputStream_defaultBufferSize), in); } catch (Exception __e) { throw rethrow(__e); } } public static Object collectionMutex(List l) { return l; } public static Object collectionMutex(Object o) { if (o instanceof List) return o; String c = className(o); if (eq(c, "java.util.TreeMap$KeySet")) c = className(o = getOpt(o, "m")); else if (eq(c, "java.util.HashMap$KeySet")) c = className(o = get_raw(o, "this$0")); if (eqOneOf(c, "java.util.TreeMap$AscendingSubMap", "java.util.TreeMap$DescendingSubMap")) c = className(o = get_raw(o, "m")); return o; } public static int findEndOfBracketPart2(List cnc, int i) { int j = i + 2, level = 1; while (j < cnc.size()) { if (eqOneOf(cnc.get(j), "{", "(", "[")) ++level; else if (eqOneOf(cnc.get(j), "}", ")", "]")) --level; if (level == 0) return j + 1; ++j; } return cnc.size(); } public static betterCIComparator_C betterCIComparator_instance; public static betterCIComparator_C betterCIComparator() { if (betterCIComparator_instance == null) betterCIComparator_instance = new betterCIComparator_C(); return betterCIComparator_instance; } public static class betterCIComparator_C implements Comparator { public int compare(String s1, String s2) { if (s1 == null) return s2 == null ? 0 : -1; if (s2 == null) return 1; int n1 = s1.length(); int n2 = s2.length(); int min = Math.min(n1, n2); for (int i = 0; i < min; i++) { char c1 = s1.charAt(i); char c2 = s2.charAt(i); if (c1 != c2) { c1 = Character.toUpperCase(c1); c2 = Character.toUpperCase(c2); if (c1 != c2) { c1 = Character.toLowerCase(c1); c2 = Character.toLowerCase(c2); if (c1 != c2) { return c1 - c2; } } } } return n1 - n2; } } public static int findEndOfCurlyBracketPart(List cnc, int i) { int j = i + 2, level = 1; while (j < cnc.size()) { if (eq(cnc.get(j), "{")) ++level; else if (eq(cnc.get(j), "}")) --level; if (level == 0) return j + 1; ++j; } return cnc.size(); } public static String structure_addTokenMarkers(String s) { return join(structure_addTokenMarkers(javaTok(s))); } public static List structure_addTokenMarkers(List tok) { TreeSet refs = new TreeSet(); for (int i = 1; i < l(tok); i += 2) { String t = tok.get(i); if (t.startsWith("t") && isInteger(t.substring(1))) refs.add(parseInt(t.substring(1))); } if (empty(refs)) return tok; for (int i : refs) { int idx = i * 2 + 1; if (idx >= l(tok)) continue; String t = ""; if (endsWithLetterOrDigit(tok.get(idx - 1))) t = " "; tok.set(idx, t + "m" + i + " " + tok.get(idx)); } return tok; } public static String jreplace(String s, String in, String out) { return jreplace(s, in, out, null); } public static String jreplace(String s, String in, String out, Object condition) { List tok = javaTok(s); return jreplace(tok, in, out, condition) ? join(tok) : s; } public static boolean jreplace(List tok, String in, String out) { return jreplace(tok, in, out, false, true, null); } public static boolean jreplace(List tok, String in, String out, Object condition) { return jreplace(tok, in, out, false, true, condition); } public static boolean jreplace(List tok, String in, String out, boolean ignoreCase, boolean reTok, Object condition) { String[] toks = javaTokForJFind_array(in); int lTokin = toks.length * 2 + 1; boolean anyChange = false; int i = -1; for (int n = 0; n < 10000; n++) { i = findCodeTokens(tok, i + 1, ignoreCase, toks, condition); if (i < 0) return anyChange; List subList = tok.subList(i - 1, i + lTokin - 1); String expansion = jreplaceExpandRefs(out, subList); int end = i + lTokin - 2; clearAllTokens(tok, i, end); tok.set(i, expansion); if (reTok) reTok(tok, i, end); i = end; anyChange = true; } throw fail("woot? 10000! " + quote(in) + " => " + quote(out)); } public static boolean jreplace_debug = false; public static ExpiringMap2 mechList_raw_cache = new ExpiringMap2(10000).setMap(ciMap()); public static Lock mechList_raw_lock = lock(); public static int mechList_raw_timeout = 60000; public static String mechList_raw(String name) { try { Lock __610 = mechList_raw_lock; lock(__610); try { if (empty(name)) return ""; String src = mechList_raw_cache.get(name); if (src != null) return src; src = mechList_raw_fresh(name); if (src != null) mechList_raw_cache.put(name, src); return src; } finally { unlock(__610); } } catch (Exception __e) { throw rethrow(__e); } } public static VF2 mechList_raw_listener = new VF2() { public void get(String msg, Object arg) { try { if (eq(msg, "mechChange") && arg instanceof String) { mechList_raw_cache.remove((String) arg); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(msg, 'mechChange) && arg instanceof S) {\r\n //print(\"Got change noti..."; } }; public static void _onLoad_mechList_raw() { add(vm_busListeners_live(), mechList_raw_listener); } public static void cleanMeUp_mechList_raw() { remove(vm_busListeners_live(), mechList_raw_listener); } public static List mechList_opt(String name) { return mechList_opt_tlft(name); } public static Map parseDoubleArrowMapCI(List l) { return parseDoubleArrowMap((Map) ciMap(), l); } public static Object callAndMake(String functionName, Object... args) { return callAndMake_orDirect(functionName, args); } public static Object pcallF(Object f, Object... args) { return pcallFunction(f, args); } public static A pcallF(F0 f) { try { return f == null ? null : f.get(); } catch (Throwable __e) { return null; } } public static B pcallF(F1 f, A a) { try { return f == null ? null : f.get(a); } catch (Throwable __e) { return null; } } public static void pcallF(VF1 f, A a) { try { if (f != null) f.get(a); } catch (Throwable __e) { _handleException(__e); } } public static Iterator iterator(Iterable c) { return c == null ? emptyIterator() : c.iterator(); } public static char charAt(String s, int i) { return s != null && i >= 0 && i < s.length() ? s.charAt(i) : '\0'; } public static boolean isLetterOrDigit(char c) { return Character.isLetterOrDigit(c); } public static List codeTokens(List tok) { return codeTokensOnly(tok); } public static String repeat(char c, int n) { n = Math.max(n, 0); char[] chars = new char[n]; for (int i = 0; i < n; i++) chars[i] = c; return new String(chars); } public static List repeat(A a, int n) { n = Math.max(n, 0); List l = new ArrayList(n); for (int i = 0; i < n; i++) l.add(a); return l; } public static List repeat(int n, A a) { return repeat(a, n); } public static Object interceptPrintInThisThread(Object f) { Object old = print_byThread().get(); print_byThread().set(f); return old; } public static int lengthOfString(String s) { return s == null ? 0 : s.length(); } public static String getStackTrace2(Throwable e) { return hideCredentials(getStackTrace(unwrapTrivialExceptionWraps(e)) + replacePrefix("java.lang.RuntimeException: ", "FAIL: ", hideCredentials(str(innerException2(e)))) + "\n"); } public static Map parseDoubleLeftArrowMapCI(List l) { return parseDoubleLeftArrowMap((Map) ciMap(), l); } public static long parseLong(String s) { if (empty(s)) return 0; return Long.parseLong(dropSuffix("L", s)); } public static long parseLong(Object s) { return Long.parseLong((String) s); } public static Throwable getInnerException(Throwable e) { if (e == null) return null; while (e.getCause() != null) e = e.getCause(); return e; } public static Throwable getInnerException(Runnable r) { return getInnerException(getException(r)); } public static boolean regionMatchesIC(String a, int offsetA, String b, int offsetB, int len) { return a != null && a.regionMatches(true, offsetA, b, offsetB, len); } public static void _registerIO(Object object, String path, boolean opened) { } public static String className(Object o) { return getClassName(o); } public static boolean isInteger(String s) { int n = l(s); if (n == 0) return false; int i = 0; if (s.charAt(0) == '-') if (++i >= n) return false; while (i < n) { char c = s.charAt(i); if (c < '0' || c > '9') return false; ++i; } return true; } public static Map javaTokForJFind_array_cache = synchronizedMRUCache(1000); public static String[] javaTokForJFind_array(String s) { String[] tok = javaTokForJFind_array_cache.get(s); if (tok == null) javaTokForJFind_array_cache.put(s, tok = codeTokensAsStringArray(jfind_preprocess(javaTok(s)))); return tok; } public static int findCodeTokens(List tok, String... tokens) { return findCodeTokens(tok, 1, false, tokens); } public static int findCodeTokens(List tok, boolean ignoreCase, String... tokens) { return findCodeTokens(tok, 1, ignoreCase, tokens); } public static int findCodeTokens(List tok, int startIdx, boolean ignoreCase, String... tokens) { return findCodeTokens(tok, startIdx, ignoreCase, tokens, null); } public static HashSet findCodeTokens_specials = lithashset("*", "", "", "", "\\*"); public static int findCodeTokens_bails, findCodeTokens_nonbails; public static interface findCodeTokens_Matcher { public boolean get(String token); } public static int findCodeTokens(List tok, int startIdx, boolean ignoreCase, String[] tokens, Object condition) { int end = tok.size() - tokens.length * 2 + 2, nTokens = tokens.length; int i = startIdx | 1; String firstToken = tokens[0]; if (!ignoreCase && !findCodeTokens_specials.contains(firstToken)) { while (i < end && !firstToken.equals(tok.get(i))) i += 2; } findCodeTokens_Matcher[] matchers = new findCodeTokens_Matcher[nTokens]; for (int j = 0; j < nTokens; j++) { String p = tokens[j]; findCodeTokens_Matcher matcher; if (p.equals("*")) matcher = t -> true; else if (p.equals("")) matcher = t -> isQuoted(t); else if (p.equals("")) matcher = t -> isIdentifier(t); else if (p.equals("")) matcher = t -> isInteger(t); else if (p.equals("\\*")) matcher = t -> t.equals("*"); else if (ignoreCase) matcher = t -> eqic(p, t); else matcher = t -> t.equals(p); matchers[j] = matcher; } outer: for (; i < end; i += 2) { for (int j = 0; j < nTokens; j++) if (!matchers[j].get(tok.get(i + j * 2))) continue outer; if (condition == null || checkTokCondition(condition, tok, i - 1)) return i; } return -1; } public static String jreplaceExpandRefs(String s, List tokref) { List tok = javaTok(s); for (int i = 1; i < l(tok); i += 2) { String t = tok.get(i); if (t.startsWith("$") && isInteger(t.substring(1))) { String x = tokref.get(-1 + parseInt(t.substring(1)) * 2); tok.set(i, x); } else if (t.equals("\\")) { tok.set(i, ""); i += 2; } } return join(tok); } public static void clearAllTokens(List tok) { for (int i = 0; i < tok.size(); i++) tok.set(i, ""); } public static void clearAllTokens(List tok, int i, int j) { for (; i < j; i++) tok.set(i, ""); } public static List reTok(List tok) { replaceCollection(tok, javaTok(tok)); return tok; } public static List reTok(List tok, int i) { return reTok(tok, i, i + 1); } public static List reTok(List tok, int i, int j) { i = i & ~1; j = min(l(tok), j | 1); if (i >= j) return tok; List t = javaTok(join(subList(tok, i, j))); replaceListPart(tok, i, j, t); return tok; } public static List reTok(List tok, IntRange r) { if (r != null) reTok(tok, r.start, r.end); return tok; } public static void lock(Lock lock) { try { ping(); if (lock == null) return; try { lock.lockInterruptibly(); } catch (InterruptedException e) { print("Locking interrupted! I probably deadlocked, oops."); printStackTrace(e); rethrow(e); } ping(); } catch (Exception __e) { throw rethrow(__e); } } public static void lock(Lock lock, String msg) { print("Locking: " + msg); lock(lock); } public static void lock(Lock lock, String msg, long timeout) { print("Locking: " + msg); lockOrFail(lock, timeout); } public static ReentrantLock lock() { return fairLock(); } public static String mechList_raw_fresh(String name) { return mechList_opt_raw_fresh(name); } public static void unlock(Lock lock, String msg) { if (lock == null) return; print("Unlocking: " + msg); lock.unlock(); } public static void unlock(Lock lock) { if (lock == null) return; lock.unlock(); } public static void add(BitSet bs, int i) { bs.set(i); } public static boolean add(Collection c, A a) { return c != null && c.add(a); } public static void add(Container c, Component x) { addToContainer(c, x); } public static Set vm_busListeners_live_cache; public static Set vm_busListeners_live() { if (vm_busListeners_live_cache == null) vm_busListeners_live_cache = vm_busListeners_live_load(); return vm_busListeners_live_cache; } public static Set vm_busListeners_live_load() { return vm_generalIdentityHashSet("busListeners"); } public static void remove(List l, int i) { if (l != null && i >= 0 && i < l(l)) l.remove(i); } public static void remove(Collection l, A a) { if (l != null) l.remove(a); } public static List mechList_opt_tlft(String name) { return mechList_opt_tlft_cached(name); } public static Map parseDoubleArrowMap(Map map, List l) { for (String s : l) { List x = splitAtDoubleArrow_bothDirections(s); if (l(x) == 2) map.put(first(x), second(x)); } return map; } public static Object callAndMake_orDirect(String functionName, List args) { return callAndMake_orDirect(functionName, toObjectArray(args)); } public static Object callAndMake_orDirect(String functionName, Object... args) { if (functionName == null) return null; if (hasMethod(mc(), functionName, args)) return callMC(functionName, args); else return makeAndCall_cached(functionName, args); } public static Object pcallFunction(Object f, Object... args) { try { return callFunction(f, args); } catch (Throwable __e) { _handleException(__e); } return null; } public static Iterator emptyIterator() { return Collections.emptyIterator(); } public static List codeTokensOnly(List tok) { int n = tok.size(); List l = emptyList(n / 2); for (int i = 1; i < n; i += 2) l.add(tok.get(i)); return l; } public static String hideCredentials(URL url) { return url == null ? null : hideCredentials(str(url)); } public static String hideCredentials(String url) { try { if (startsWithOneOf(url, "http://", "https://") && isAGIBlueDomain(hostNameFromURL(url))) return url; } catch (Throwable e) { print("HideCredentials", e); } return url.replaceAll("([&?])(_pass|key)=[^&\\s\"]*", "$1$2="); } public static String hideCredentials(Object o) { return hideCredentials(str(o)); } public static String getStackTrace(Throwable throwable) { lastException(throwable); return getStackTrace_noRecord(throwable); } public static String getStackTrace_noRecord(Throwable throwable) { StringWriter writer = new StringWriter(); throwable.printStackTrace(new PrintWriter(writer)); return hideCredentials(writer.toString()); } public static String getStackTrace() { return getStackTrace_noRecord(new Throwable()); } public static Throwable unwrapTrivialExceptionWraps(Throwable e) { if (e == null) return e; while (e.getClass() == RuntimeException.class && e.getCause() != null && eq(e.getMessage(), str(e.getCause()))) e = e.getCause(); return e; } public static String replacePrefix(String prefix, String replacement, String s) { if (!startsWith(s, prefix)) return s; return replacement + substring(s, l(prefix)); } public static Throwable innerException2(Throwable e) { if (e == null) return null; while (empty(e.getMessage()) && e.getCause() != null) e = e.getCause(); return e; } public static Map parseDoubleLeftArrowMap(Map map, List l) { for (String s : l) { List x = splitAtDoubleLeftArrow(s); if (l(x) == 2) map.put(second(x), first(x)); } return map; } public static Throwable getException(Runnable r) { try { callF(r); return null; } catch (Throwable e) { return e; } } public static String[] codeTokensAsStringArray(List tok) { int n = max(0, (l(tok) - 1) / 2); String[] out = new String[n]; for (int i = 0; i < n; i++) out[i] = tok.get(i * 2 + 1); return out; } public static int jfind(String s, String in) { return jfind(javaTok(s), in); } public static int jfind(List tok, String in) { return jfind(tok, 1, in); } public static int jfind(List tok, int startIdx, String in) { return jfind(tok, startIdx, in, null); } public static int jfind(List tok, String in, Object condition) { return jfind(tok, 1, in, condition); } public static int jfind(List tok, int startIdx, String in, Object condition) { return jfind(tok, startIdx, javaTokForJFind_array(in), condition); } public static int jfind(List tok, List tokin) { return jfind(tok, 1, tokin); } public static int jfind(List tok, int startIdx, List tokin) { return jfind(tok, startIdx, tokin, null); } public static int jfind(List tok, int startIdx, String[] tokinC, Object condition) { return findCodeTokens(tok, startIdx, false, tokinC, condition); } public static int jfind(List tok, int startIdx, List tokin, Object condition) { return jfind(tok, startIdx, codeTokensAsStringArray(tokin), condition); } public static List jfind_preprocess(List tok) { for (String type : litlist("quoted", "id", "int")) replaceSublist(tok, ll("<", "", type, "", ">"), ll("<" + type + ">")); replaceSublist(tok, ll("\\", "", "*"), ll("\\*")); return tok; } public static boolean isQuoted(String s) { if (isNormalQuoted(s)) return true; return isMultilineQuoted(s); } public static boolean isIdentifier(String s) { return isJavaIdentifier(s); } public static boolean checkTokCondition(Object condition, List tok, int i) { if (condition instanceof TokCondition) return ((TokCondition) condition).get(tok, i); return checkCondition(condition, tok, i); } public static void replaceCollection(Collection dest, Collection src) { dest.clear(); dest.addAll(src); } public static void replaceListPart(List l, int i, int j, List l2) { int j2 = i + l(l2); if (j2 == j) { copyListPart(l2, 0, l, i, l(l2)); return; } l.subList(i, j).clear(); l.addAll(i, l2); } public static A printStackTrace(A e) { print(getStackTrace(e)); return e; } public static void printStackTrace() { printStackTrace(new Throwable()); } public static void printStackTrace(String msg) { printStackTrace(new Throwable(msg)); } public static void printStackTrace(String msg, Throwable e) { printStackTrace(new Throwable(msg, e)); } public static void lockOrFail(Lock lock, long timeout) { try { ping(); if (!lock.tryLock(timeout, TimeUnit.MILLISECONDS)) { String s = "Couldn't acquire lock after " + timeout + " ms."; if (lock instanceof ReentrantLock) { ReentrantLock l = (ReentrantLock) lock; s += " Hold count: " + l.getHoldCount() + ", owner: " + call(l, "getOwner"); } throw fail(s); } ping(); } catch (Exception __e) { throw rethrow(__e); } } public static ReentrantLock fairLock() { return new ReentrantLock(true); } public static String mechList_opt_raw_fresh(String name) { Object readMode = mechMode().readMode; if (readMode instanceof File) { String md5Name = uniqueFileNameUsingMD5_80_v2(upper(name)); return loadTextFileFromZipFile((File) readMode, md5Name); } boolean useLocal = false, useRemote = true; if (eq(readMode, "mergeLocalAndRemote")) useLocal = true; else if (eq(readMode, "local")) { useLocal = true; useRemote = false; } String s = ""; if (useRemote) { if (eq(mechMode().readMode, "localCopies")) s = unnull(loadTextFile(remoteMechListMirrorFile(name))); else s = serverMechList_raw_fresh(name, true); } if (useLocal) s = appendNewLineIfNempty(s) + localMechList_opt_raw_fresh(name); return s; } public static void addToContainer(final Container a, final Component b) { if (a != null && b != null) { swing(new Runnable() { public void run() { try { a.add(b); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "a.add(b);"; } }); } } public static Map>> mechList_opt_tlft_cached_cache = ciMap(); public static Lock mechList_opt_tlft_cached_lock = lock(); public static List mechList_opt_tlft_cached(String name) { MechListDependentCache> listCache; { Lock __708 = mechList_opt_tlft_cached_lock; lock(__708); try { listCache = mechList_opt_tlft_cached_cache.get(name); if (listCache == null) mechList_opt_tlft_cached_cache.put(name, listCache = new MechListDependentCache(name, new F1>() { public List get(String x) { try { return tlft(x); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "tlft(x)"; } })); } finally { unlock(__708); } } return listCache.get(); } public static List splitAtDoubleArrow_bothDirections(String s) { return splitAtDoubleArrow_bothDirections(javaTok(s)); } public static List splitAtDoubleArrow_bothDirections(List tok) { List l = splitAtDoubleArrow(tok); if (l(l) != 2) l = reversedList(splitAtDoubleLeftArrow(tok)); return l; } public static Object[] toObjectArray(Collection c) { List l = asList(c); return l.toArray(new Object[l.size()]); } public static boolean hasMethod(Object o, String method, Object... args) { return findMethod_cached(o, method, args) != null; } public static ThreadLocal makeAndCall_initChild = new ThreadLocal(); public static Object makeAndCall_cached(String functionName, Object... args) { Class c = loadFunctions_cached(functionName); AutoCloseable closer = (AutoCloseable) (callF(makeAndCall_initChild.get(), c)); AutoCloseable __717 = closer; try { return preciseCall(c, functionName, args); } finally { _close(__717); } } public static Object callFunction(Object f, Object... args) { return callF(f, args); } public static boolean startsWithOneOf(String s, String... l) { for (String x : l) if (startsWith(s, x)) return true; return false; } public static boolean isAGIBlueDomain(String domain) { return domainIsUnder(domain, theAGIBlueDomain()); } public static String hostNameFromURL(String url) { try { return new URL(url).getHost(); } catch (Exception __e) { throw rethrow(__e); } } public static volatile PersistableThrowable lastException_lastException; public static PersistableThrowable lastException() { return lastException_lastException; } public static void lastException(Throwable e) { lastException_lastException = persistableThrowable(e); } public static List splitAtDoubleLeftArrow(String s) { return splitAtDoubleLeftArrow(javaTok(s)); } public static List splitAtDoubleLeftArrow(List tok) { List l = new ArrayList(); int i = 0; while (i < l(tok)) { int j = indexOfSubList(tok, ll("<", "", "="), i); if (i >= l(tok)) break; if (j < 0) j = l(tok); l.add(trimJoin(tok.subList(i, j))); i = j + 3; } return l; } public static ArrayList litlist(A... a) { ArrayList l = new ArrayList(a.length); for (A x : a) l.add(x); return l; } public static boolean isNormalQuoted(String s) { int l = l(s); if (!(l >= 2 && s.charAt(0) == '"' && lastChar(s) == '"')) return false; int j = 1; while (j < l) if (s.charAt(j) == '"') return j == l - 1; else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; return false; } public static boolean isMultilineQuoted(String s) { if (!startsWith(s, "[")) return false; int i = 1; while (i < s.length() && s.charAt(i) == '=') ++i; return i < s.length() && s.charAt(i) == '['; } public static boolean isJavaIdentifier(String s) { if (empty(s) || !Character.isJavaIdentifierStart(s.charAt(0))) return false; for (int i = 1; i < s.length(); i++) if (!Character.isJavaIdentifierPart(s.charAt(i))) return false; return true; } public static boolean checkCondition(Object condition, Object... args) { return isTrue(callF(condition, args)); } public static boolean checkCondition(IF1 condition, A arg) { return isTrue(callF(condition, arg)); } public static void copyListPart(List a, int i1, List b, int i2, int n) { if (a == null || b == null) return; for (int i = 0; i < n; i++) b.set(i2 + i, a.get(i1 + i)); } public static MechMode mechMode_value = new MechMode(); public static MechMode mechMode() { return mechMode_value; } public static String uniqueFileNameUsingMD5_80_v2(String fullName) { return uniqueFileNameUsingMD5_80_v2(fullName, md5(fullName)); } public static String uniqueFileNameUsingMD5_80_v2(String fullName, String md5) { return takeFirst(80 - 33, fileNameEncode(fullName)) + " - " + md5; } public static String upper(String s) { return s == null ? null : s.toUpperCase(); } public static char upper(char c) { return Character.toUpperCase(c); } public static String loadTextFileFromZipFile(File inZip, String fileName) { try { if (!fileExists(inZip)) return null; try { ZipFile zip = new ZipFile(inZip); try { return loadTextFileFromZipFile(zip, fileName); } finally { _close(zip); } } catch (Throwable e) { throw fail(f2s(inZip), e); } } catch (Exception __e) { throw rethrow(__e); } } public static String loadTextFileFromZipFile(ZipFile zip, String fileName) { try { ZipEntry entry = zip.getEntry(fileName); if (entry == null) return null; InputStream fin = zip.getInputStream(entry); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); copyStream(fin, baos); return fromUTF8(baos.toByteArray()); } finally { _close(fin); } } catch (Exception __e) { throw rethrow(__e); } } public static String loadTextFile(String fileName) { return loadTextFile(fileName, null); } public static String loadTextFile(File f, String defaultContents) { try { checkFileNotTooBigToRead(f); if (f == null || !f.exists()) return defaultContents; FileInputStream fileInputStream = new FileInputStream(f); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); return loadTextFile(inputStreamReader); } catch (Exception __e) { throw rethrow(__e); } } public static String loadTextFile(File fileName) { return loadTextFile(fileName, null); } public static String loadTextFile(String fileName, String defaultContents) { return fileName == null ? defaultContents : loadTextFile(newFile(fileName), defaultContents); } public static String loadTextFile(Reader reader) throws IOException { StringBuilder builder = new StringBuilder(); try { char[] buffer = new char[1024]; int n; while (-1 != (n = reader.read(buffer))) builder.append(buffer, 0, n); } finally { reader.close(); } return str(builder); } public static File remoteMechListMirrorFile(String listName) { return newFile(remoteMechListMirrorsDir(), uniqueFileNameUsingMD5_80_v2(upper(listName))); } public static boolean serverMechList_raw_fresh_verbose = false; public static String serverMechList_raw_fresh(String name) { return serverMechList_raw_fresh(name, false); } public static String serverMechList_raw_fresh(String name, boolean opt) { Lock __744 = downloadLock(); lock(__744); try { String text = null; try { text = loadTextFile(remoteMechListMirrorFile(name)); } catch (Throwable __e) { _handleException(__e); } Object[] params = muricaCredentialsPlus("md5", md5OrNull(text), "l", l(text), "opt", opt ? 1 : 0, "withStatus", 1); String url = "http://butter.botcompany.de:8080/mech/raw/list-text/" + urlencode(name); String page = postPageSilently(url, params); Map map = jsonDecodeMap(page); boolean same = eq(map.get("Same"), true); boolean appended = eq(map.get("Appended"), true); saveTextFile(remoteMechListMirrorMetaFile(name), struct(getMultipleKeys(map, "Name", "Status"))); if (!same) { if (appended) text += (String) map.get("Text"); else text = (String) map.get("Text"); saveTextFile(remoteMechListMirrorFile(name), text); File nameFile = remoteMechListMirrorNameFile(name); if (!fileExists(nameFile)) { String actualName = or((String) map.get("Name"), name); saveTextFile(nameFile, actualName); } } if (serverMechList_raw_fresh_verbose) print("Mech list " + name + ": " + (appended ? "appended" : same ? "same" : "downloaded") + ": " + n2(countLines(text), "line")); if (!same) vmBus_send("remoteMechListMirrorChanged", name); return text; } finally { unlock(__744); } } public static String appendNewLineIfNempty(String s) { return empty(s) ? "" : s + "\n"; } public static String localMechList_opt_raw_fresh(String name) { return unnull(loadTextFile(localMechListFile(name))); } public static Object swing(Object f) { return swingAndWait(f); } public static A swing(F0 f) { return (A) swingAndWait(f); } public static A swing(IF0 f) { return (A) swingAndWait(f); } public static List reversedList(Collection l) { List x = cloneList(l); Collections.reverse(x); return x; } public static Method findMethod_cached(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { _MethodCache cache = callOpt_getCache(((Class) o)); List methods = cache.cache.get(method); if (methods != null) for (Method m : methods) if (isStaticMethod(m) && findMethod_checkArgs(m, args, false)) return m; return null; } else { _MethodCache cache = callOpt_getCache(o.getClass()); List methods = cache.cache.get(method); if (methods != null) for (Method m : methods) if (findMethod_checkArgs(m, args, false)) return m; return null; } } catch (Exception __e) { throw rethrow(__e); } } public static HashMap, Class> loadFunctions_cache = new HashMap(); public static Lock loadFunctions_cached_lock = lock(); public static boolean loadFunctions_preferDiskCache = false; public static Class loadFunctions_cached(String... functions) { return loadFunctions_cached(asList(functions)); } public static Class loadFunctions_cached(Collection functions) { TreeSet functionSet = new TreeSet(functions); Lock __754 = loadFunctions_cached_lock; lock(__754); try { Class c = loadFunctions_cache.get(functionSet); if (c != null) return c; String cached = getTranspiledStandardFunctionsIfCached(functionSet); String transpilation = null; if (cached != null && (loadFunctions_preferDiskCache || standardFunctionsCacheUpToDate(functionSet))) transpilation = cached; if (transpilation != null) { print("Loading " + joinWithPlus(functionSet)); c = veryQuickJava_finish(transpilation, emptyList()); } if (c == null) c = loadFunctions(functionSet); loadFunctions_cache.put(functionSet, c); assertNotNull(loadFunctions_cache.get(functionSet)); return c; } finally { unlock(__754); } } public static void loadFunctions_cached_clearCache() { Lock __755 = loadFunctions_cached_lock; lock(__755); try { loadFunctions_cache.clear(); } finally { unlock(__755); } } public static void _onLoad_loadFunctions_cached() { ownResource(vmBus_onMessage("refreshedTranspiler", new Runnable() { public void run() { try { loadFunctions_clearCache(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "loadFunctions_clearCache();"; } })); } public static boolean preciseCall_debug = false; public static Object preciseCall(Object o, String method, Object... args) { try { if (o instanceof Class) { _MethodCache cache = callOpt_getCache(((Class) o)); List methods = cache.cache.get(method); Lowest best = new Lowest(); if (methods != null) for (Method m : methods) { { if (!(isStaticMethod(m))) continue; } int score = methodApplicabilityScore(m, args); if (score < Integer.MAX_VALUE) { if (preciseCall_debug) print("Method score: " + m + " " + score); best.put(m, score); } } Method m = best.get(); if (m != null) return invokeMethod(m, null, args); return call_withVarargs(((Class) o), method, args); } else throw todo(); } catch (Exception __e) { throw rethrow(__e); } } public static boolean domainIsUnder(String domain, String mainDomain) { return eqic(domain, mainDomain) || ewic(domain, "." + mainDomain); } public static String theAGIBlueDomain() { return "agi.blue"; } public static String md5(String text) { try { if (text == null) return "-"; return bytesToHex(md5_impl(toUtf8(text))); } catch (Exception __e) { throw rethrow(__e); } } public static String md5(byte[] data) { if (data == null) return "-"; return bytesToHex(md5_impl(data)); } public static byte[] md5_impl(byte[] data) { try { return MessageDigest.getInstance("MD5").digest(data); } catch (Exception __e) { throw rethrow(__e); } } public static String md5(File file) { return md5OfFile(file); } public static List takeFirst(List l, int n) { return l(l) <= n ? l : newSubListOrSame(l, 0, n); } public static List takeFirst(int n, List l) { return takeFirst(l, n); } public static String takeFirst(int n, String s) { return substring(s, 0, n); } public static String takeFirst(String s, int n) { return substring(s, 0, n); } public static List takeFirst(int n, Iterable i) { List l = new ArrayList(); Iterator it = i.iterator(); for (int _repeat_771 = 0; _repeat_771 < n; _repeat_771++) { if (it.hasNext()) l.add(it.next()); else break; } return l; } public static String fileNameEncode_safeChars = " ()[]#,!"; public static String fileNameEncode(String s) { StringBuilder buf = new StringBuilder(); int n = l(s); for (int i = 0; i < n; i++) { char c = s.charAt(i); if (contains(fileNameEncode_safeChars, c)) buf.append(c); else buf.append(urlencode(str(c))); } return str(buf); } public static boolean fileExists(String path) { return path != null && new File(path).exists(); } public static boolean fileExists(File f) { return f != null && f.exists(); } public static String f2s(File f) { return f == null ? null : f.getAbsolutePath(); } public static String f2s(String s) { return f2s(newFile(s)); } public static String f2s(java.nio.file.Path p) { return p == null ? null : f2s(p.toFile()); } public static void copyStream(InputStream in, OutputStream out) { try { byte[] buf = new byte[65536]; while (true) { int n = in.read(buf); if (n <= 0) return; out.write(buf, 0, n); } } catch (Exception __e) { throw rethrow(__e); } } public static String fromUTF8(byte[] bytes) { return fromUtf8(bytes); } public static ThreadLocal> checkFileNotTooBigToRead_tl = new ThreadLocal(); public static void checkFileNotTooBigToRead(File f) { callF(checkFileNotTooBigToRead_tl.get(), f); } public static File remoteMechListMirrorsDir() { return javaxDataDir("Remote Mech Lists"); } public static Lock downloadLock_lock = fairLock(); public static Lock downloadLock() { return downloadLock_lock; } public static Object[] muricaCredentialsPlus(Object... params) { return concatArrays(muricaCredentials(), params); } public static String md5OrNull(String s) { return s == null ? null : md5(s); } public static String urlencode(String x) { try { return URLEncoder.encode(unnull(x), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } public static String postPageSilently(String url, Object... params) { return doPostSilently(litmap(params), url); } public static Map jsonDecodeMap(String s) { Object o = jsonDecode(s); if (o instanceof List && empty((List) o)) return new HashMap(); if (o instanceof Map) return (Map) o; else throw fail("Not a JSON map: " + s); } public static File saveTextFile(String fileName, String contents) throws IOException { CriticalAction action = beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)"); try { File file = new File(fileName); mkdirsForFile(file); String tempFileName = fileName + "_temp"; File tempFile = new File(tempFileName); if (contents != null) { if (tempFile.exists()) try { String saveName = tempFileName + ".saved." + now(); copyFile(tempFile, new File(saveName)); } catch (Throwable e) { printStackTrace(e); } FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath()); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); } if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (contents != null) if (!tempFile.renameTo(file)) throw new IOException("Can't rename " + tempFile + " to " + file); vmBus_send("wroteFile", file); return file; } finally { action.done(); } } public static File saveTextFile(File fileName, String contents) { try { saveTextFile(fileName.getPath(), contents); return fileName; } catch (Exception __e) { throw rethrow(__e); } } public static File remoteMechListMirrorMetaFile(String listName) { return javaxDataDir("Remote Mech Lists/" + uniqueFileNameUsingMD5_80_v2(upper(listName)) + ".meta"); } public static Map getMultipleKeys(Map map, A... keys) { Map map2 = similarEmptyMap(map); if (map != null) for (A key : keys) map2.put(key, map.get(key)); return map2; } public static File remoteMechListMirrorNameFile(String listName) { return javaxDataDir("Remote Mech Lists/" + uniqueFileNameUsingMD5_80_v2(upper(listName)) + ".name"); } public static String n2(long l) { return formatWithThousands(l); } public static String n2(Collection l) { return n2(l(l)); } public static String n2(double l, String singular) { return n2(l, singular, singular + "s"); } public static String n2(double l, String singular, String plural) { if (fraction(l) == 0) return n2((long) l, singular, plural); else return l + " " + plural; } public static String n2(long l, String singular, String plural) { return n_fancy2(l, singular, plural); } public static String n2(long l, String singular) { return n_fancy2(l, singular, singular + "s"); } public static String n2(Collection l, String singular) { return n2(l(l), singular); } public static String n2(Collection l, String singular, String plural) { return n_fancy2(l, singular, plural); } public static String n2(Map m, String singular, String plural) { return n_fancy2(m, singular, plural); } public static String n2(Map m, String singular) { return n2(l(m), singular); } public static String n2(Object[] a, String singular) { return n2(l(a), singular); } public static String n2(Object[] a, String singular, String plural) { return n_fancy2(a, singular, plural); } public static String n2(MultiSet ms, String singular, String plural) { return n_fancy2(ms, singular, plural); } public static void vmBus_send(String msg, Object... args) { Object arg = vmBus_wrapArgs(args); pcallFAll(vm_busListeners_live(), msg, arg); pcallFAll(vm_busListenersByMessage_live().get(msg), msg, arg); } public static void vmBus_send(String msg) { vmBus_send(msg, (Object) null); } public static File localMechListFile(String listName) { return newFile(localMechListsDir(), uniqueFileNameUsingMD5_80_v2(listName) + ".text"); } public static void swingAndWait(Runnable r) { try { if (isAWTThread()) r.run(); else EventQueue.invokeAndWait(addThreadInfoToRunnable(r)); } catch (Exception __e) { throw rethrow(__e); } } public static Object swingAndWait(final Object f) { if (isAWTThread()) return callF(f); else { final Var result = new Var(); swingAndWait(new Runnable() { public void run() { try { result.set(callF(f)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "result.set(callF(f));"; } }); return result.get(); } } public static Method findMethod(Object o, String method, Object... args) { return findMethod_cached(o, method, args); } public static boolean findMethod_checkArgs(Method m, Object[] args, boolean debug) { Class[] types = m.getParameterTypes(); if (types.length != args.length) { if (debug) System.out.println("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) if (!(args[i] == null || isInstanceX(types[i], args[i]))) { if (debug) System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]); return false; } return true; } public static String getTranspiledStandardFunctionsIfCached(TreeSet functionSet) { try { File f = transpiledStandardFunctions_file(functionSet); return loadTextFile(f); } catch (Throwable __e) { return null; } } public static boolean standardFunctionsCacheUpToDate(TreeSet functionSet) { File f = transpiledStandardFunctions_file(functionSet); long date = recommendedTranspilationDateForStandardFunctions(functionSet); return date != 0 && f.lastModified() >= date; } public static String joinWithPlus(Collection c) { return join("+", c); } public static String joinWithPlus(String... c) { return join("+", c); } public static Class veryQuickJava_finish(String src, List libs) { libs = cloneList(libs); src = findTranslators2(src, libs); String dehlibs = join(" ", libs); File bytecode = null; bytecode = javaCompile_overInternalBot(src, dehlibs); return hotwireCore(concatLists(ll(bytecode), loadLibraries(libs))); } public static boolean loadFunctions_debug = false; public static Object loadFunctions_veryQuickJava; public static Class loadFunctions(String... functions) { return loadFunctions(asList(functions)); } public static Class loadFunctions(Collection functions) { print("Compiling " + joinWithPlus(functions)); TreeSet functionSet = new TreeSet(functions); StringBuilder buf = new StringBuilder(); for (String s : functionSet) buf.append("please include function " + assertIdentifier(s) + ".\n"); long time = sysNow(); Class c; if (loadFunctions_veryQuickJava != null) c = (Class) callF(loadFunctions_veryQuickJava, str(buf)); else { veryQuickJava_transpiled.set(""); c = veryQuickJava(str(buf)); saveTranspiledStandardFunctions(functionSet, getAndClearThreadLocal(veryQuickJava_transpiled)); } if (loadFunctions_debug) done2_always(time, "loadFunctions: " + join(" ", functions)); return c; } public static A assertNotNull(A a) { assertTrue(a != null); return a; } public static A assertNotNull(String msg, A a) { assertTrue(msg, a != null); return a; } public static void ownResource(AutoCloseable c) { _registerAutoCloseable(c); } public static AutoCloseable vmBus_onMessage(final String msg, final VF1 onMessage) { Map map = vm_busListenersByMessage_live(); synchronized (map) { Set listeners = map.get(msg); if (listeners == null) map.put(msg, listeners = syncIdentityHashSet()); return tempAdd(listeners, new VF2() { public void get(String _msg, Object arg) { try { callF(onMessage, arg); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(onMessage, arg)"; } }); } } public static AutoCloseable vmBus_onMessage(String msg, final VF2 onMessage) { return vmBus_onMessage(msg, new VF1() { public void get(Object[] o) { try { callF(onMessage, first(o), second(o)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(onMessage, first(o), second(o));"; } }); } public static AutoCloseable vmBus_onMessage(String msg, Runnable onMessage) { return vmBus_onMessage(msg, runnableToVF1(onMessage)); } public static void loadFunctions_clearCache() { callOptMC("loadFunctions_cached_clearCache"); } public static int methodApplicabilityScore(Method m, Object[] args) { Class[] types = m.getParameterTypes(); if (types.length != args.length) return Integer.MAX_VALUE; int score = 0; for (int i = 0; i < types.length; i++) { Object a = args[i]; Class c = types[i]; if (a == null) ++score; else if (c == a.getClass()) { } else if (isInstanceX(c, a)) ++score; else return Integer.MAX_VALUE; } return score; } public static RuntimeException todo() { throw new RuntimeException("TODO"); } public static RuntimeException todo(Object msg) { throw new RuntimeException("TODO: " + msg); } public static String bytesToHex(byte[] bytes) { return bytesToHex(bytes, 0, bytes.length); } public static String bytesToHex(byte[] bytes, int ofs, int len) { StringBuilder stringBuilder = new StringBuilder(len * 2); for (int i = 0; i < len; i++) { String s = "0" + Integer.toHexString(bytes[ofs + i]); stringBuilder.append(s.substring(s.length() - 2, s.length())); } return stringBuilder.toString(); } public static byte[] toUtf8(String s) { try { return s.getBytes("UTF-8"); } catch (Exception __e) { throw rethrow(__e); } } public static boolean md5OfFile_verbose = false; public static String md5OfFile(String path) { return md5OfFile(newFile(path)); } public static String md5OfFile(File f) { try { if (!f.exists()) return "-"; if (md5OfFile_verbose) print("Getting MD5 of " + f); MessageDigest md5 = MessageDigest.getInstance("MD5"); FileInputStream in = new FileInputStream(f); try { byte[] buf = new byte[65536]; int l; while (true) { l = in.read(buf); if (l <= 0) break; md5.update(buf, 0, l); } return bytesToHex(md5.digest()); } finally { _close(in); } } catch (Exception __e) { throw rethrow(__e); } } public static List newSubListOrSame(List l, int startIndex) { return newSubListOrSame(l, startIndex, l(l)); } public static List newSubListOrSame(List l, int startIndex, int endIndex) { if (l == null) return null; int n = l(l); startIndex = max(0, startIndex); endIndex = min(n, endIndex); if (startIndex >= endIndex) return ll(); if (startIndex == 0 && endIndex == n) return l; return cloneList(l.subList(startIndex, endIndex)); } public static List newSubListOrSame(List l, IntRange r) { return newSubListOrSame(l, r.start, r.end); } public static boolean contains(Collection c, Object o) { return c != null && c.contains(o); } public static boolean contains(Object[] x, Object o) { if (x != null) for (Object a : x) if (eq(a, o)) return true; return false; } public static boolean contains(String s, char c) { return s != null && s.indexOf(c) >= 0; } public static boolean contains(String s, String b) { return s != null && s.indexOf(b) >= 0; } public static boolean contains(BitSet bs, int i) { return bs != null && bs.get(i); } public static String fromUtf8(byte[] bytes) { try { return bytes == null ? null : new String(bytes, "UTF-8"); } catch (Exception __e) { throw rethrow(__e); } } public static File javaxDataDir_dir; public static File javaxDataDir() { return javaxDataDir_dir != null ? javaxDataDir_dir : new File(userHome(), "JavaX-Data"); } public static File javaxDataDir(String... subs) { return newFile(javaxDataDir(), subs); } public static Object[] concatArrays(Object[]... arrays) { int l = 0; for (Object[] a : arrays) l += l(a); Object[] x = new Object[l]; int i = 0; for (Object[] a : arrays) if (a != null) { System.arraycopy(a, 0, x, i, l(a)); i += l(a); } return x; } public static Object[] muricaCredentials() { String pass = muricaPassword(); return nempty(pass) ? new Object[] { "_pass", pass } : new Object[0]; } public static String doPostSilently(Map urlParameters, String url) { return doPostSilently(makePostData(urlParameters), url); } public static String doPostSilently(String urlParameters, String url) { doPost_silently.set(true); return doPost(urlParameters, url); } public static HashMap litmap(Object... x) { HashMap map = new HashMap(); litmap_impl(map, x); return map; } public static void litmap_impl(Map map, Object... x) { if (x != null) for (int i = 0; i < x.length - 1; i += 2) if (x[i + 1] != null) map.put(x[i], x[i + 1]); } public static boolean jsonDecode_useOrderedMaps = true; public static Object jsonDecode(final String text) { final List tok = jsonTok(text); if (l(tok) == 1) return null; class Y { public int i = 1; public Object parse() { String t = tok.get(i); if (t.startsWith("\"")) { String s = unquote(tok.get(i)); i += 2; return s; } if (t.equals("{")) return parseMap(); if (t.equals("[")) return this.parseList(); if (t.equals("null")) { i += 2; return null; } if (t.equals("false")) { i += 2; return false; } if (t.equals("true")) { i += 2; return true; } boolean minus = false; if (t.equals("-")) { minus = true; i += 2; t = get(tok, i); } if (isInteger(t)) { i += 2; if (eq(get(tok, i), ".")) { String x = t + "." + get(tok, i + 2); i += 4; double d = parseDouble(x); if (minus) d = -d; return d; } else { long l = parseLong(t); if (minus) l = -l; return l != (int) l ? new Long(l) : new Integer((int) l); } } throw new RuntimeException("Unknown token " + (i + 1) + ": " + t + ": " + text); } public Object parseList() { consume("["); List list = new ArrayList(); while (!tok.get(i).equals("]")) { list.add(parse()); if (tok.get(i).equals(",")) i += 2; } consume("]"); return list; } public Object parseMap() { consume("{"); Map map = jsonDecode_useOrderedMaps ? new LinkedHashMap() : new TreeMap(); while (!tok.get(i).equals("}")) { String key = unquote(tok.get(i)); i += 2; consume(":"); Object value = parse(); map.put(key, value); if (tok.get(i).equals(",")) i += 2; } consume("}"); return map; } public void consume(String s) { if (!tok.get(i).equals(s)) { String prevToken = i - 2 >= 0 ? tok.get(i - 2) : ""; String nextTokens = join(tok.subList(i, Math.min(i + 4, tok.size()))); throw fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")"); } i += 2; } } return new Y().parse(); } public static List beginCriticalAction_inFlight = synchroList(); public static class CriticalAction { public String description; public CriticalAction() { } public CriticalAction(String description) { this.description = description; } public void done() { beginCriticalAction_inFlight.remove(this); } } public static CriticalAction beginCriticalAction(String description) { ping(); CriticalAction c = new CriticalAction(description); beginCriticalAction_inFlight.add(c); return c; } public static void cleanMeUp_beginCriticalAction() { int n = 0; while (nempty(beginCriticalAction_inFlight)) { int m = l(beginCriticalAction_inFlight); if (m != n) { n = m; try { print("Waiting for " + n2(n, "critical actions") + ": " + join(", ", collect(beginCriticalAction_inFlight, "description"))); } catch (Throwable __e) { _handleException(__e); } } sleepInCleanUp(10); } } public static File mkdirsForFile(File file) { File dir = file.getParentFile(); if (dir != null) { dir.mkdirs(); if (!dir.isDirectory()) if (dir.isFile()) throw fail("Please delete the file " + f2s(dir) + " - it is supposed to be a directory!"); else throw fail("Unknown IO exception during mkdirs of " + f2s(file)); } return file; } public static String mkdirsForFile(String path) { mkdirsForFile(new File(path)); return path; } public static long now_virtualTime; public static long now() { return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis(); } public static File copyFile(File src, File dest) { try { FileInputStream inputStream = new FileInputStream(src.getPath()); FileOutputStream outputStream = newFileOutputStream(dest.getPath()); try { copyStream(inputStream, outputStream); inputStream.close(); } finally { outputStream.close(); } return dest; } catch (Exception __e) { throw rethrow(__e); } } public static FileOutputStream newFileOutputStream(File path) throws IOException { return newFileOutputStream(path.getPath()); } public static FileOutputStream newFileOutputStream(String path) throws IOException { return newFileOutputStream(path, false); } public static FileOutputStream newFileOutputStream(File path, boolean append) throws IOException { return newFileOutputStream(path.getPath(), append); } public static FileOutputStream newFileOutputStream(String path, boolean append) throws IOException { mkdirsForFile(path); FileOutputStream f = new FileOutputStream(path, append); _registerIO(f, path, true); return f; } public static Map similarEmptyMap(Map m) { if (m instanceof TreeMap) return new TreeMap(((TreeMap) m).comparator()); if (m instanceof LinkedHashMap) return new LinkedHashMap(); return new HashMap(); } public static String formatWithThousands(long l) { return formatWithThousandsSeparator(l); } public static double fraction(double d) { return d % 1; } public static String n_fancy2(long l, String singular, String plural) { return formatWithThousandsSeparator(l) + " " + trim(l == 1 ? singular : plural); } public static String n_fancy2(Collection l, String singular, String plural) { return n_fancy2(l(l), singular, plural); } public static String n_fancy2(Map m, String singular, String plural) { return n_fancy2(l(m), singular, plural); } public static String n_fancy2(Object[] a, String singular, String plural) { return n_fancy2(l(a), singular, plural); } public static String n_fancy2(MultiSet ms, String singular, String plural) { return n_fancy2(l(ms), singular, plural); } public static Object vmBus_wrapArgs(Object... args) { return empty(args) ? null : l(args) == 1 ? args[0] : args; } public static Map vm_busListenersByMessage_live_cache; public static Map vm_busListenersByMessage_live() { if (vm_busListenersByMessage_live_cache == null) vm_busListenersByMessage_live_cache = vm_busListenersByMessage_live_load(); return vm_busListenersByMessage_live_cache; } public static Map vm_busListenersByMessage_live_load() { return vm_generalHashMap("busListenersByMessage"); } public static File localMechListsDir() { return javaxDataDir("Mech Lists"); } public static Runnable addThreadInfoToRunnable(final Object r) { final Object info = _threadInfo(); return info == null ? asRunnable(r) : new Runnable() { public void run() { try { _inheritThreadInfo(info); callF(r); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "_inheritThreadInfo(info); callF(r);"; } }; } public static File transpiledStandardFunctions_file(TreeSet functionSet) { String name = uniqueFileNameUsingMD5_80_v2(join("+", functionSet)); File dir = javaxCachesDir("Transpiled Standard Functions"); saveTextFileUnlessExists(newFile(dir, name + ".functions"), lines(functionSet)); return newFile(dir, name + ".java"); } public static long recommendedTranspilationDateForStandardFunctions(Collection sfNames) { long date = 0; for (String name : sfNames) { long d = recommendedTranspilationDateForStandardFunction(name); if (d == 0) return 0; date = max(date, d); } return date; } public static String findTranslators2(String src, List libsOut) { return join(findTranslators2(javaTok(src), libsOut)); } public static List findTranslators2(List tok, List libsOut) { int i; while ((i = jfind(tok, "!")) >= 0) { setAdd(libsOut, tok.get(i + 2)); clearTokens(tok, i, i + 3); } return tok; } public static File javaCompile_overInternalBot(String src) { return javaCompile_overInternalBot(src, ""); } public static synchronized File javaCompile_overInternalBot(String src, String dehlibs) { return CompilerBot.compile(src, dehlibs); } public static Class hotwireCore(List files) { try { JavaXClassLoader classLoader = hotwire_makeClassLoader(files); Class theClass = classLoader.loadClass("main"); setOpt(theClass, "__javax", getJavaX()); if (getOpt(theClass, "programID") == null) setOpt(theClass, "programID", "#3999999"); if (!_inCore()) hotwire_copyOver(theClass); return theClass; } catch (Exception __e) { throw rethrow(__e); } } public static List loadLibraries(List snippetIDs) { return map("loadLibrary", snippetIDs); } public static String assertIdentifier(String s) { return assertIsIdentifier(s); } public static String assertIdentifier(String msg, String s) { return assertIsIdentifier(msg, s); } public static long sysNow() { ping(); return System.nanoTime() / 1000000; } public static Class veryQuickJava(CharSequence mainJava) { return veryQuickJava3(str(mainJava)); } public static void saveTranspiledStandardFunctions(TreeSet functionSet, String transpiledSrc) { try { saveTextFile(transpiledStandardFunctions_file(functionSet), transpiledSrc); } catch (Throwable __e) { _handleException(__e); } } public static A getAndClearThreadLocal(ThreadLocal tl) { A a = tl.get(); tl.set(null); return a; } public static long done2_always(long startTime, String desc) { long time = sysNow() - startTime; print(desc + " [" + time + " ms]"); return time; } public static long done2_always(String desc, long startTime) { return done2_always(startTime, desc); } public static long done2_always(long startTime) { return done2_always(startTime, ""); } public static Set _registerAutoCloseable_set = synchroHashSet(); public static void _registerAutoCloseable(AutoCloseable c) { addIfNotNull(_registerAutoCloseable_set, c); } public static void cleanMeUp__registerAutoCloseable() { closeAutoCloseables(getAndClearList(_registerAutoCloseable_set)); } public static Set syncIdentityHashSet() { return (Set) synchronizedSet(identityHashSet()); } public static AutoCloseable tempAdd(final Collection l, final A a) { if (l == null || l.contains(a)) return null; l.add(a); return new AutoCloseable() { public String toString() { return "l.remove(a);"; } public void close() throws Exception { l.remove(a); } }; } public static A third(List l) { return _get(l, 2); } public static A third(Iterable l) { if (l == null) return null; Iterator it = iterator(l); for (int _repeat_916 = 0; _repeat_916 < 2; _repeat_916++) { if (!it.hasNext()) return null; it.next(); } return it.hasNext() ? it.next() : null; } public static A third(A[] bla) { return bla == null || bla.length <= 2 ? null : bla[2]; } public static VF1 runnableToVF1(final Runnable r) { return r == null ? null : new VF1() { public void get(A a) { try { r.run(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "r.run()"; } }; } public static Object callOptMC(String method, Object... args) { return callOpt(mc(), method, args); } public static File loadLibrary(String snippetID) { return loadBinarySnippet(snippetID); } public static String _userHome; public static String userHome() { if (_userHome == null) return actualUserHome(); return _userHome; } public static File userHome(String path) { return new File(userDir(), path); } public static volatile boolean muricaPassword_pretendNotAuthed = false; public static String muricaPassword() { if (muricaPassword_pretendNotAuthed) return null; return trim(loadTextFile(muricaPasswordFile())); } public static String makePostData(Map map) { StringBuilder buf = new StringBuilder(); for (Map.Entry e : map.entrySet()) { String key = (String) (e.getKey()); Object val = e.getValue(); if (val != null) { String value = str(val); if (nempty(buf)) buf.append("&"); buf.append(urlencode(key)).append("=").append(urlencode((value))); } } return str(buf); } public static String makePostData(Object... params) { StringBuilder buf = new StringBuilder(); int n = l(params); for (int i = 0; i + 1 < n; i += 2) { String key = (String) (params[i]); Object val = params[i + 1]; if (val != null) { String value = str(val); if (nempty(buf)) buf.append("&"); buf.append(urlencode(key)).append("=").append(urlencode((value))); } } return str(buf); } public static ThreadLocal doPost_silently = new ThreadLocal(); public static ThreadLocal doPost_timeout = new ThreadLocal(); public static ThreadLocal> doPost_extraHeaders = new ThreadLocal(); public static String doPost(Map urlParameters, String url) { return doPost(makePostData(urlParameters), url); } public static String doPost(String urlParameters, String url) { try { URL _url = new URL(url); ping(); return doPost(urlParameters, _url.openConnection(), _url); } catch (Exception __e) { throw rethrow(__e); } } public static String doPost(String urlParameters, URLConnection conn, URL url) { try { boolean silently = isTrue(optParam(doPost_silently)); Long timeout = optParam(doPost_timeout); Map extraHeaders = optPar(doPost_extraHeaders); setHeaders(conn); for (String key : keys(extraHeaders)) conn.setRequestProperty(key, extraHeaders.get(key)); int l = lUtf8(urlParameters); if (!silently) print("Sending POST request: " + hideCredentials(url) + " (" + l + " bytes)"); if (timeout != null) setURLConnectionTimeouts(conn, timeout); ((HttpURLConnection) conn).setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Length", str(l)); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); writer.write(urlParameters); writer.flush(); String contents = loadPage_utf8(conn, url, false); writer.close(); return contents; } catch (Exception __e) { throw rethrow(__e); } } public static List jsonTok(String s) { List tok = new ArrayList(); int l = l(s); int i = 0; while (i < l) { int j = i; char c; String cc; while (j < l) { c = s.charAt(j); cc = s.substring(j, Math.min(j + 2, l)); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (cc.equals("/*")) { do ++j; while (j < l && !s.substring(j, Math.min(j + 2, l)).equals("*/")); j = Math.min(j + 2, l); } else if (cc.equals("//")) { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } tok.add(s.substring(i, j)); i = j; if (i >= l) break; c = s.charAt(i); if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j + 1 < l) j += 2; else ++j; } } else if (Character.isLetter(c)) do ++j; while (j < l && Character.isLetter(s.charAt(j))); else if (Character.isDigit(c)) do ++j; while (j < l && Character.isDigit(s.charAt(j))); else ++j; tok.add(s.substring(i, j)); i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } public static double parseDouble(String s) { return Double.parseDouble(s); } public static List collect(Collection c, String field) { return collectField(c, field); } public static List collect(String field, Collection c) { return collectField(c, field); } public static void sleepInCleanUp(long ms) { try { if (ms < 0) return; Thread.sleep(ms); } catch (Exception __e) { throw rethrow(__e); } } public static String formatWithThousandsSeparator(long l) { return NumberFormat.getInstance(new Locale("en_US")).format(l); } public static Runnable asRunnable(Object o) { return toRunnable(o); } public static void _inheritThreadInfo(Object info) { _threadInheritInfo(info); } public static File javaxCachesDir_dir; public static File javaxCachesDir() { return javaxCachesDir_dir != null ? javaxCachesDir_dir : new File(userHome(), "JavaX-Caches"); } public static File javaxCachesDir(String sub) { return newFile(javaxCachesDir(), sub); } public static void saveTextFileUnlessExists(File f, String contents) { if (f != null && !f.exists()) saveTextFile(f, contents); } public static long recommendedTranspilationDateForStandardFunction(String sfName) { return toLong(mechMap("Recommended SF Transpilation Date").get(sfName)); } public static boolean setAdd(Collection c, A a) { if (c == null || c.contains(a)) return false; c.add(a); return true; } public static void clearTokens(List tok) { clearAllTokens(tok); } public static void clearTokens(List tok, int i, int j) { clearAllTokens(tok, i, j); } public static void clearTokens(List tok, IntRange r) { clearAllTokens(tok, r.start, r.end); } public static JavaXClassLoader hotwire_makeClassLoader(List files) { Collection toShare = hotwire_classesToShare(); return nempty(toShare) ? new JavaXClassLoaderWithParent2(null, files, myClassLoader(), cloneList(toShare)) : new JavaXClassLoader(null, files); } public static String assertIsIdentifier(String s) { if (!isIdentifier(s)) throw fail("Not an identifier: " + quote(s)); return s; } public static String assertIsIdentifier(String msg, String s) { if (!isIdentifier(s)) throw fail(msg + " - Not an identifier: " + quote(s)); return s; } public static boolean veryQuickJava_silent = true; public static boolean veryQuickJava_useCompilerBot = true; public static ThreadLocal veryQuickJava_transpiled = new ThreadLocal(); public static Object veryQuickJava3_cacheFunction; public static Class veryQuickJava3(String mainJava) { return veryQuickJava3(mainJava, emptyList()); } public static Class veryQuickJava3(String mainJava, List libs) { Class c = (Class) (callF(veryQuickJava3_cacheFunction, mainJava, libs)); if (c != null) return c; transpileRaw_silent = veryQuickJava_silent; String src = transpileRaw(mainJava); if (veryQuickJava_transpiled.get() != null) veryQuickJava_transpiled.set(src); return veryQuickJava_finish(src, libs); } public static Set synchroHashSet() { return Collections.synchronizedSet(new HashSet()); } public static void closeAutoCloseables(Collection l) { if (l != null) for (AutoCloseable c : l) { try { c.close(); } catch (Throwable __e) { _handleException(__e); } } } public static List getAndClearList(Collection l) { if (l == null) return emptyList(); synchronized (collectionMutex(l)) { List out = cloneList(l); l.clear(); return out; } } public static Set synchronizedSet() { return synchroHashSet(); } public static Set synchronizedSet(Set set) { return Collections.synchronizedSet(set); } public static Set identityHashSet() { return Collections.newSetFromMap(new IdentityHashMap()); } public static A _get(List l, int idx) { return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null; } public static Object _get(Object o, String field) { return get(o, field); } public static Object _get(String field, Object o) { return get(o, field); } public static A _get(A[] l, int idx) { return idx >= 0 && idx < l(l) ? l[idx] : null; } public static File loadBinarySnippet(String snippetID) { try { IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return rl.loadLibrary(snippetID); long id = parseSnippetID(snippetID); if (isImageServerSnippet(id)) return loadImageAsFile(snippetID); File f = DiskSnippetCache_getLibrary(id); if (fileSize(f) == 0) f = loadDataSnippetToFile(snippetID); return f; } catch (Exception __e) { throw rethrow(__e); } } public static String actualUserHome_value; public static String actualUserHome() { if (actualUserHome_value == null) { if (isAndroid()) actualUserHome_value = "/storage/emulated/0/"; else actualUserHome_value = System.getProperty("user.home"); } return actualUserHome_value; } public static File actualUserHome(String sub) { return newFile(new File(actualUserHome()), sub); } public static File userDir() { return new File(userHome()); } public static File userDir(String path) { return new File(userHome(), path); } public static File muricaPasswordFile() { return new File(javaxSecretDir(), "murica/muricaPasswordFile"); } public static A optParam(ThreadLocal tl, A defaultValue) { return optPar(tl, defaultValue); } public static A optParam(ThreadLocal tl) { return optPar(tl); } public static Object optParam(String name, Map params) { return mapGet(params, name); } public static A optParam(Object[] opt, String name, A defaultValue) { int n = l(opt); if (n == 1 && opt[0] instanceof Map) { Map map = (Map) (opt[0]); return map.containsKey(name) ? (A) map.get(name) : defaultValue; } if (!even(l(opt))) throw fail("Odd parameter length"); for (int i = 0; i < l(opt); i += 2) if (eq(opt[i], name)) return (A) opt[i + 1]; return defaultValue; } public static Object optParam(Object[] opt, String name) { return optParam(opt, name, null); } public static A optPar(ThreadLocal tl, A defaultValue) { A a = tl.get(); if (a != null) { tl.set(null); return a; } return defaultValue; } public static A optPar(ThreadLocal tl) { return optPar(tl, null); } public static Object optPar(Object[] params, String name) { return optParam(params, name); } public static Object optPar(String name, Object[] params) { return optParam(params, name); } public static Object optPar(String name, Map params) { return optParam(name, params); } public static A optPar(Object[] params, String name, A defaultValue) { return optParam(params, name, defaultValue); } public static A optPar(String name, Object[] params, A defaultValue) { return optParam(params, name, defaultValue); } public static void setHeaders(URLConnection con) throws IOException { String computerID = getComputerID_quick(); if (computerID != null) try { con.setRequestProperty("X-ComputerID", computerID); con.setRequestProperty("X-OS", System.getProperty("os.name") + " " + System.getProperty("os.version")); } catch (Throwable e) { } } public static int lUtf8(String s) { return l(utf8(s)); } public static URLConnection setURLConnectionTimeouts(URLConnection con, long timeout) { con.setConnectTimeout(toInt(timeout)); con.setReadTimeout(toInt(timeout)); if (con.getConnectTimeout() != timeout || con.getReadTimeout() != timeout) print("Warning: Timeouts not set by JDK."); return con; } public static List collectField(Collection c, String field) { List l = new ArrayList(); if (c != null) for (Object a : c) l.add(getOpt(a, field)); return l; } public static List collectField(String field, Collection c) { return collectField(c, field); } public static long toLong(Object o) { if (o instanceof Number) return ((Number) o).longValue(); if (o instanceof String) return parseLong((String) o); return 0; } public static Map mechMap(String listName) { return mechMapCI(listName); } public static Set hotwire_classesToShare = synchroSet(); public static Set hotwire_classesToShare() { return hotwire_classesToShare; } public static ClassLoader myClassLoader() { return _getClass(mc()).getClassLoader(); } public static boolean transpileRaw_silent = true; public static boolean transpileRaw_useDiskCache = false; public static Class transpileRaw_trans; public static boolean transpileRaw_verySilent = false; public static boolean transpileRaw_dontCopyFromCreator = false; public static Lock transpileRaw_lock = lock(); public static ThreadLocal transpileRaw_asInclude = new ThreadLocal(); public static String transpileRaw(String mainJava) { return transpileRaw(mainJava, false, transpileRaw_useDiskCache); } public static String transpileRaw(String mainJava, boolean fragment) { return transpileRaw(mainJava, fragment, transpileRaw_useDiskCache); } public static String transpileRaw(String mainJava, boolean fragment, boolean useDiskCache) { mainJava = dropTranslators(mainJava); if (!transpileRaw_dontCopyFromCreator) transpileRaw_copyFromCreator(); Lock __1018 = transpileRaw_lock; lock(__1018); try { File cacheFile = null; if (useDiskCache) { cacheFile = new File(javaxCodeDir(), "Transpilations/" + uniqueFileNameUsingMD5_80_v2(mainJava) + ".java"); { String __1022 = loadTextFile(cacheFile); if (!empty(__1022)) return __1022; } } AutoCloseable __1020 = transpileRaw_verySilent ? null : tempLoadingAnim("Transpiling..."); try { transpileRaw_translator(); setOpt(transpileRaw_trans, "localStuffOnly", fragment); setOpt(transpileRaw_trans, "asInclude", isTrue(transpileRaw_asInclude.get())); set(transpileRaw_trans, "mainJava", mainJava); set(transpileRaw_trans, "print_byThread", print_byThread); if (!transpileRaw_verySilent) { print("Running translator " + getOpt(transpileRaw_trans, "programID")); if (!transpileRaw_silent) { print("Translator ID: " + systemHashCode(transpileRaw_trans) + ", print_byThread=" + getTL((ThreadLocal) getOpt(transpileRaw_trans, "print_byThread"))); call(transpileRaw_trans, "print", "test"); } } callMain(transpileRaw_trans); String main = (String) (get(transpileRaw_trans, "mainJava")); if (useDiskCache) { saveTextFile(new File(cacheFile.getPath() + "x"), mainJava); saveTextFile(cacheFile, main); } return main; } finally { _close(__1020); } } finally { unlock(__1018); } } public static Class transpileRaw_translator() { if (transpileRaw_trans == null) { transpileRaw_trans = hotwireSharingLibraries_silently(defaultJavaXTranslatorID()); makeDependent(transpileRaw_trans); } setOpt(transpileRaw_trans, "print_silent", transpileRaw_silent); return transpileRaw_trans; } public static void transpileRaw_copyFromCreator() { Lock __1019 = transpileRaw_lock; lock(__1019); try { if (transpileRaw_trans != null) return; Object c = creator(); if (c == null) return; Class trans = (Class) (getOpt(c, "transpileRaw_trans")); Lock lock = (Lock) (getOpt(c, "transpileRaw_lock")); if (trans != null && lock != null) { print("Using creator's transpiler: " + getProgramID(c) + " => " + programID()); transpileRaw_lock = lock; transpileRaw_trans = trans; } } finally { unlock(__1019); } } public static void cleanMeUp_transpileRaw() { transpileRaw_trans = null; } public static IResourceLoader vm_getResourceLoader() { return proxy(IResourceLoader.class, vm_generalMap_get("_officialResourceLoader")); } public static boolean isImageServerSnippet(long id) { return id >= 1100000 && id < 1200000; } public static File loadImageAsFile(String snippetIDOrURL) { try { if (isURL(snippetIDOrURL)) throw fail("not implemented"); if (!isSnippetID(snippetIDOrURL)) throw fail("Not a URL or snippet ID: " + snippetIDOrURL); String snippetID = "" + parseSnippetID(snippetIDOrURL); File file = imageSnippetCacheFile(snippetID); if (fileSize(file) > 0) return file; String imageURL = snippetImageURL_noHttps(snippetID); System.err.println("Loading image: " + imageURL); byte[] data = loadBinaryPage(imageURL); saveBinaryFile(file, data); return file; } catch (Exception __e) { throw rethrow(__e); } } public static File DiskSnippetCache_file(long snippetID) { return new File(getGlobalCache(), "data_" + snippetID + ".jar"); } public static File DiskSnippetCache_getLibrary(long snippetID) throws IOException { File file = DiskSnippetCache_file(snippetID); return file.exists() ? file : null; } public static void DiskSnippetCache_putLibrary(long snippetID, byte[] data) throws IOException { saveBinaryFile(DiskSnippetCache_file(snippetID), data); } public static byte[] loadDataSnippetImpl(String snippetID) throws IOException { byte[] data; try { URL url = new URL(dataSnippetLink(snippetID)); print("Loading library: " + hideCredentials(url)); try { data = loadBinaryPage(url.openConnection()); } catch (RuntimeException e) { data = null; } if (data == null || data.length == 0) { url = new URL(tb_mainServer() + "/blobs/" + parseSnippetID(snippetID)); print("Loading library: " + hideCredentials(url)); data = loadBinaryPage(url.openConnection()); } print("Bytes loaded: " + data.length); } catch (FileNotFoundException e) { throw new IOException("Binary snippet #" + snippetID + " not found or not public"); } return data; } public static long fileSize(String path) { return getFileSize(path); } public static long fileSize(File f) { return getFileSize(f); } public static File loadDataSnippetToFile(String snippetID) { try { snippetID = fsI(snippetID); IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return rl.loadLibrary(snippetID); File f = DiskSnippetCache_file(parseSnippetID(snippetID)); List urlsTried = new ArrayList(); List errors = new ArrayList(); try { URL url = addAndReturn(urlsTried, new URL(dataSnippetLink(snippetID))); print("Loading library: " + hideCredentials(url)); try { loadBinaryPageToFile(openConnection(url), f); if (fileSize(f) == 0) throw fail(); } catch (Throwable e) { errors.add(e); url = addAndReturn(urlsTried, new URL(tb_mainServer() + "/blobs/" + psI(snippetID))); print("Trying other server: " + hideCredentials(url)); loadBinaryPageToFile(openConnection(url), f); print("Got bytes: " + fileSize(f)); } if (fileSize(f) == 0) throw fail(); System.err.println("Bytes loaded: " + fileSize(f)); } catch (Throwable e) { errors.add(e); throw fail("Binary snippet " + snippetID + " not found or not public. URLs tried: " + allToString(urlsTried) + ", errors: " + allToString(errors)); } return f; } catch (Exception __e) { throw rethrow(__e); } } public static File javaxSecretDir_dir; public static File javaxSecretDir() { return javaxSecretDir_dir != null ? javaxSecretDir_dir : new File(userHome(), "JavaX-Secret"); } public static File javaxSecretDir(String sub) { return newFile(javaxSecretDir(), sub); } public static B mapGet(Map map, A a) { return map == null || a == null ? null : map.get(a); } public static B mapGet(A a, Map map) { return map == null || a == null ? null : map.get(a); } public static boolean even(int i) { return (i & 1) == 0; } public static boolean even(long i) { return (i & 1) == 0; } public static String getComputerID_quick() { return computerID(); } public static byte[] utf8(String s) { return toUtf8(s); } public static Set synchroSet() { return synchroHashSet(); } public static Set synchroSet(Set set) { return Collections.synchronizedSet(set); } public static Class _getClass(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { return null; } } public static Class _getClass(Object o) { return o == null ? null : o instanceof Class ? (Class) o : o.getClass(); } public static Class _getClass(Object realm, String name) { try { return getClass(realm).getClassLoader().loadClass(classNameToVM(name)); } catch (ClassNotFoundException e) { return null; } } public static String dropTranslators(String src) { return findTranslators2(src, null); } public static List dropTranslators(List tok) { return findTranslators2(tok, null); } public static File javaxCodeDir_dir; public static File javaxCodeDir() { return javaxCodeDir_dir != null ? javaxCodeDir_dir : new File(userHome(), "JavaX-Code"); } public static File javaxCodeDir(String sub) { return newFile(javaxCodeDir(), sub); } public static AutoCloseable tempLoadingAnim(String msg) { return tempDisposeWindow(loadingAnim(msg)); } public static A set(A o, String field, Object value) { if (o == null) return null; if (o instanceof Class) set((Class) o, field, value); else try { Field f = set_findField(o.getClass(), field); makeAccessible(f); smartSet(f, o, value); } catch (Exception e) { throw new RuntimeException(e); } return o; } public static void set(Class c, String field, Object value) { if (c == null) return; try { Field f = set_findStaticField(c, field); makeAccessible(f); smartSet(f, null, value); } catch (Exception e) { throw new RuntimeException(e); } } public static Field set_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()); } public static Field set_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()); } public static int systemHashCode(Object o) { return identityHashCode(o); } public static Object getTL(Object o, String name) { return getThreadLocal(o, name); } public static A getTL(ThreadLocal tl) { return getThreadLocal(tl); } public static A getTL(ThreadLocal tl, A defaultValue) { return getThreadLocal(tl, defaultValue); } public static Class hotwireSharingLibraries_silently(String progID) { AutoCloseable __1070 = temp_loadPage_silent(); try { return hotwireSharingLibraries(progID); } finally { _close(__1070); } } public static String defaultJavaXTranslatorID_value = "#759"; public static String defaultJavaXTranslatorID() { return defaultJavaXTranslatorID_value; } public static Object makeDependent_postProcess; public static void makeDependent(Object c) { if (c == null) return; assertTrue("Not a class", c instanceof Class); dependentClasses(); hotwire_classes.add(new WeakReference(c)); Object local_log = getOpt(mc(), "local_log"); if (local_log != null) setOpt(c, "local_log", local_log); Object print_byThread = getOpt(mc(), "print_byThread"); if (print_byThread != null) setOpt(c, "print_byThread", print_byThread); callF(makeDependent_postProcess, c); } public static WeakReference creator_class; public static Object creator() { return creator_class == null ? null : creator_class.get(); } public static boolean loadBufferedImage_useImageCache = true; public static BufferedImage loadBufferedImage(String snippetIDOrURLOrFile) { try { ping(); if (snippetIDOrURLOrFile == null) return null; if (isURL(snippetIDOrURLOrFile)) return imageIO_readURL(snippetIDOrURLOrFile); if (isAbsolutePath(snippetIDOrURLOrFile)) return loadBufferedImage(new File(snippetIDOrURLOrFile)); if (!isSnippetID(snippetIDOrURLOrFile)) throw fail("Not a URL or snippet ID or file: " + snippetIDOrURLOrFile); String snippetID = "" + parseSnippetID(snippetIDOrURLOrFile); IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return loadBufferedImage(rl.loadLibrary(snippetID)); File dir = imageSnippetsCacheDir(); if (loadBufferedImage_useImageCache) { dir.mkdirs(); File file = new File(dir, snippetID + ".png"); if (file.exists() && file.length() != 0) try { return ImageIO.read(file); } catch (Throwable e) { e.printStackTrace(); } } String imageURL = snippetImageURL_http(snippetID); print("Loading image: " + imageURL); BufferedImage image = imageIO_readURL(imageURL); if (loadBufferedImage_useImageCache) { File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis()); ImageIO.write(image, "png", tempFile); tempFile.renameTo(new File(dir, snippetID + ".png")); } return image; } catch (Exception __e) { throw rethrow(__e); } } public static BufferedImage loadBufferedImage(File file) { try { return file.isFile() ? ImageIO.read(file) : null; } catch (Exception __e) { throw rethrow(__e); } } public static A proxy(Class intrface, final Object target) { if (target == null) return null; if (isInstance(intrface, target)) return (A) target; return (A) java.lang.reflect.Proxy.newProxyInstance(intrface.getClassLoader(), new Class[] { intrface }, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) { return call(target, method.getName(), unnull(args)); } }); } public static A proxy(Object target, Class intrface) { return proxy(intrface, target); } public static Object vm_generalMap_get(Object key) { return vm_generalMap().get(key); } public static boolean isURL(String s) { return startsWithOneOf(s, "http://", "https://", "file:"); } public static File imageSnippetCacheFile(String snippetID) { File dir = imageSnippetsCacheDir(); if (!loadBufferedImage_useImageCache) return null; return new File(dir, parseSnippetID(snippetID) + ".png"); } public static String snippetImageURL_noHttps(String snippetID) { return snippetImageURL_noHttps(snippetID, "png"); } public static String snippetImageURL_noHttps(String snippetID, String contentType) { return snippetImageURL(snippetID, contentType).replace("https://www.botcompany.de:8443/", "http://www.botcompany.de:8080/").replace("https://botcompany.de/", "http://botcompany.de/"); } public static ThreadLocal>> loadBinaryPage_responseHeaders = new ThreadLocal(); public static ThreadLocal> loadBinaryPage_extraHeaders = new ThreadLocal(); public static byte[] loadBinaryPage(String url) { try { print("Loading " + url); return loadBinaryPage(loadPage_openConnection(new URL(url))); } catch (Exception __e) { throw rethrow(__e); } } public static byte[] loadBinaryPage(URLConnection con) { try { Map extraHeaders = getAndClearThreadLocal(loadBinaryPage_extraHeaders); setHeaders(con); for (String key : keys(extraHeaders)) con.setRequestProperty(key, extraHeaders.get(key)); return loadBinaryPage_noHeaders(con); } catch (Exception __e) { throw rethrow(__e); } } public static byte[] loadBinaryPage_noHeaders(URLConnection con) { try { ByteArrayOutputStream buf = new ByteArrayOutputStream(); InputStream inputStream = con.getInputStream(); loadBinaryPage_responseHeaders.set(con.getHeaderFields()); long len = 0; try { len = con.getContentLength(); } catch (Throwable e) { printStackTrace(e); } int n = 0; while (true) { int ch = inputStream.read(); if (ch < 0) break; buf.write(ch); if (++n % 100000 == 0) println(" " + n + (len != 0 ? "/" + len : "") + " bytes loaded."); } inputStream.close(); return buf.toByteArray(); } catch (Exception __e) { throw rethrow(__e); } } public static byte[] saveBinaryFile(String fileName, byte[] contents) { try { File file = new File(fileName); File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = fileName + "_temp"; FileOutputStream fileOutputStream = newFileOutputStream(tempFileName); fileOutputStream.write(contents); fileOutputStream.close(); if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (!new File(tempFileName).renameTo(file)) throw new IOException("Can't rename " + tempFileName + " to " + fileName); vmBus_send("wroteFile", file); return contents; } catch (Exception __e) { throw rethrow(__e); } } public static byte[] saveBinaryFile(File fileName, byte[] contents) { return saveBinaryFile(fileName.getPath(), contents); } public static File getGlobalCache() { File file = new File(javaxCachesDir(), "Binary Snippets"); file.mkdirs(); return file; } public static String dataSnippetLink(String snippetID) { long id = parseSnippetID(snippetID); if (id >= 1100000 && id < 1200000) return imageServerURL() + id; if (id >= 1400000 && id < 1500000) return "http://butter.botcompany.de:8080/files/" + id + "?_pass=" + muricaPassword(); if (id >= 1200000 && id < 1300000) { String pw = muricaPassword(); if (empty(pw)) throw fail("Please set 'murica password by running #1008829"); return "http://butter.botcompany.de:8080/1008823/raw/" + id + "?_pass=" + pw; } else return "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + id + "&contentType=application/binary"; } public static String tb_mainServer_default = "http://code.botcompany.de:8081"; public static Object tb_mainServer_override; public static String tb_mainServer() { if (tb_mainServer_override != null) return (String) callF(tb_mainServer_override); return trim(loadTextFile(tb_mainServer_file(), tb_mainServer_default)); } public static File tb_mainServer_file() { return getProgramFile("#1001638", "mainserver.txt"); } public static boolean tb_mainServer_isDefault() { return eq(tb_mainServer(), tb_mainServer_default); } public static String fsI(String id) { return formatSnippetID(id); } public static String fsI(long id) { return formatSnippetID(id); } public static A addAndReturn(Collection c, A a) { if (c != null) c.add(a); return a; } public static void loadBinaryPageToFile(String url, File file) { try { print("Loading " + url); loadBinaryPageToFile(openConnection(new URL(url)), file); } catch (Exception __e) { throw rethrow(__e); } } public static void loadBinaryPageToFile(URLConnection con, File file) { try { setHeaders(con); loadBinaryPageToFile_noHeaders(con, file); } catch (Exception __e) { throw rethrow(__e); } } public static void loadBinaryPageToFile_noHeaders(URLConnection con, File file) { try { File ftemp = new File(f2s(file) + "_temp"); FileOutputStream buf = newFileOutputStream(mkdirsFor(ftemp)); try { InputStream inputStream = con.getInputStream(); long len = 0; try { len = con.getContentLength(); } catch (Throwable e) { printStackTrace(e); } String pat = " {*}" + (len != 0 ? "/" + len : "") + " bytes loaded."; copyStreamWithPrints(inputStream, buf, pat); inputStream.close(); buf.close(); file.delete(); renameFile_assertTrue(ftemp, file); } finally { if (buf != null) buf.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static URLConnection openConnection(String url) { try { return openConnection(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } public static URLConnection openConnection(URL url) { try { ping(); callOpt(javax(), "recordOpenURLConnection", str(url)); return url.openConnection(); } catch (Exception __e) { throw rethrow(__e); } } public static long psI(String snippetID) { return parseSnippetID(snippetID); } public static List allToString(Iterable c) { List l = new ArrayList(); for (Object o : unnull(c)) l.add(str(o)); return l; } public static List allToString(Object[] c) { List l = new ArrayList(); for (Object o : unnull(c)) l.add(str(o)); return l; } public static String _computerID; public static Lock computerID_lock = lock(); public static String computerID() { if (_computerID == null) { Lock __1112 = computerID_lock; lock(__1112); try { if (_computerID != null) return _computerID; File file = computerIDFile(); _computerID = loadTextFile(file.getPath()); if (_computerID == null) { _computerID = loadTextFile(userDir(".tinybrain/computer-id")); if (_computerID == null) _computerID = makeRandomID(12, new SecureRandom()); saveTextFile(file, _computerID); } } finally { unlock(__1112); } } return _computerID; } public static Class getClass(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { return null; } } public static Class getClass(Object o) { return o instanceof Class ? (Class) o : o.getClass(); } public static Class getClass(Object realm, String name) { try { try { return getClass(realm).getClassLoader().loadClass(classNameToVM(name)); } catch (ClassNotFoundException e) { return null; } } catch (Exception __e) { throw rethrow(__e); } } public static String classNameToVM(String name) { return name.replace(".", "$"); } public static AutoCloseable tempDisposeWindow(final Window w) { return new AutoCloseable() { public void close() { disposeWindow(w); } }; } public static JWindow loadingAnim() { return showLoadingAnimation(); } public static JWindow loadingAnim(String text) { return showLoadingAnimation(text); } public static Field makeAccessible(Field f) { try { f.setAccessible(true); } catch (Throwable e) { vmBus_send("makeAccessible_error", e, f); } return f; } public static Method makeAccessible(Method m) { try { m.setAccessible(true); } catch (Throwable e) { vmBus_send("makeAccessible_error", e, m); } return m; } public static Constructor makeAccessible(Constructor c) { try { c.setAccessible(true); } catch (Throwable e) { vmBus_send("makeAccessible_error", e, c); } return c; } public static void smartSet(Field f, Object o, Object value) throws Exception { try { f.set(o, value); } catch (Exception e) { Class type = f.getType(); if (type == int.class && value instanceof Long) value = ((Long) value).intValue(); if (type == LinkedHashMap.class && value instanceof Map) { f.set(o, asLinkedHashMap((Map) value)); return; } try { if (f.getType() == Concept.Ref.class) { f.set(o, ((Concept) o).new Ref((Concept) value)); return; } if (o instanceof Concept.Ref) { f.set(o, ((Concept.Ref) o).get()); return; } } catch (Throwable _e) { } throw e; } } public static int identityHashCode(Object o) { return System.identityHashCode(o); } public static AutoCloseable temp_loadPage_silent() { return tempSetThreadLocal(loadPage_silent, true); } public static Class hotwireSharingLibraries(String progID) { try { Pair p = CompilerBot.compileSnippet2(progID); File jar = p.a; assertTrue(f2s(jar), jar.isFile()); List files = ll(jar); String dehlibs = unnull(loadTextFileFromZip(jar, "libraries")); List myLibraries = myLibraryFiles(); Matcher matcher = Pattern.compile("\\d+").matcher(dehlibs); while (matcher.find()) { String libID = matcher.group(); File lib = loadLibrary(libID); if (myLibraries.contains(lib)) { } else { files.add(lib); } } JavaXClassLoaderWithParent classLoader = new JavaXClassLoaderWithParent(progID, files, myClassLoader()); return hotwire_finish(classLoader, progID, p.b); } catch (Exception __e) { throw rethrow(__e); } } public static List dependentClasses() { return cleanUpAndGetWeakReferencesList(hotwire_classes); } public static List> hotwire_classes = synchroList(); public static Class hotwireDependent(String src) { Class c = hotwire(src); makeDependent(c); return c; } public static BufferedImage imageIO_readURL(String url) { try { if (startsWith(url, "https:")) disableCertificateValidation(); return ImageIO.read(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } public static boolean isAbsolutePath(String s) { return s != null && new File(s).isAbsolute(); } public static boolean isAbsolutePath(File f) { return f != null && f.isAbsolute(); } public static File imageSnippetsCacheDir() { return javaxCachesDir("Image-Snippets"); } public static String snippetImageURL_http(String snippetID) { return snippetImageURL_http(snippetID, "png"); } public static String snippetImageURL_http(String snippetID, String contentType) { return replacePrefix("https://", "http://", snippetImageURL(snippetID, contentType)).replace(":8443", ":8080"); } public static boolean isInstance(Class type, Object arg) { return type.isInstance(arg); } public static Map vm_generalMap_map; public static Map vm_generalMap() { if (vm_generalMap_map == null) vm_generalMap_map = (Map) get(javax(), "generalMap"); return vm_generalMap_map; } public static String snippetImageURL(long snippetID) { return snippetImageURL(fsI(snippetID)); } public static String snippetImageURL(String snippetID) { return snippetImageURL(snippetID, "png"); } public static String snippetImageURL(String snippetID, String contentType) { if (isURL(snippetID)) return snippetID; long id = parseSnippetID(snippetID); String url; if (id == 1000010 || id == 1000012) url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + id; else if (isImageServerSnippet(id)) url = imageServerLink(id); else url = "https://www.botcompany.de:8443/img/" + id; return url; } public static int loadPage_defaultTimeout = 60000; public static ThreadLocal loadPage_charset = new ThreadLocal(); public static boolean loadPage_allowGzip = true, loadPage_debug; public static boolean loadPage_anonymous = false; public static int loadPage_verboseness = 100000; public static int loadPage_retries = 1; public static ThreadLocal loadPage_silent = new ThreadLocal(); public static volatile int loadPage_forcedTimeout; public static ThreadLocal loadPage_forcedTimeout_byThread = new ThreadLocal(); public static ThreadLocal>> loadPage_responseHeaders = new ThreadLocal(); public static ThreadLocal> loadPage_extraHeaders = new ThreadLocal(); public static String loadPageSilently(String url) { try { return loadPageSilently(new URL(loadPage_preprocess(url))); } catch (Exception __e) { throw rethrow(__e); } } public static String loadPageSilently(URL url) { try { if (url.getProtocol().equals("https")) disableCertificateValidation(); if (!networkAllowanceTest(str(url))) throw fail("Not allowed: " + url); IOException e = null; for (int tries = 0; tries < loadPage_retries; tries++) try { URLConnection con = loadPage_openConnection(url); return loadPage(con, url); } catch (IOException _e) { e = _e; if (loadPageThroughProxy_enabled) { print("Trying proxy because of: " + e); try { return loadPageThroughProxy(str(url)); } catch (Throwable e2) { print(" " + exceptionToStringShort(e2)); } } else if (loadPage_debug) print(exceptionToStringShort(e)); if (tries < loadPage_retries - 1) sleepSeconds(1); } throw e; } catch (Exception __e) { throw rethrow(__e); } } public static String loadPage_preprocess(String url) { if (url.startsWith("tb/")) url = tb_mainServer() + "/" + url; if (url.indexOf("://") < 0) url = "http://" + url; return url; } public static String loadPage(String url) { try { url = loadPage_preprocess(url); if (!isTrue(loadPage_silent.get())) printWithTime("Loading: " + hideCredentials(url)); return loadPageSilently(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } public static String loadPage(URL url) { return loadPage(url.toExternalForm()); } public static String loadPage(URLConnection con, URL url) throws IOException { return loadPage(con, url, true); } public static String loadPage(URLConnection con, URL url, boolean addHeaders) throws IOException { Map extraHeaders = getAndClearThreadLocal(loadPage_extraHeaders); if (addHeaders) try { if (!loadPage_anonymous) setHeaders(con); if (loadPage_allowGzip) con.setRequestProperty("Accept-Encoding", "gzip"); con.setRequestProperty("X-No-Cookies", "1"); for (String key : keys(extraHeaders)) con.setRequestProperty(key, extraHeaders.get(key)); } catch (Throwable e) { } vm_generalSubMap("URLConnection per thread").put(currentThread(), con); loadPage_responseHeaders.set(con.getHeaderFields()); InputStream in = null; try { in = urlConnection_getInputStream(con); if (loadPage_debug) print("Put stream in map: " + currentThread()); String contentType = con.getContentType(); if (contentType == null) { throw new IOException("Page could not be read: " + hideCredentials(url)); } String charset = loadPage_charset == null ? null : loadPage_charset.get(); if (charset == null) charset = loadPage_guessCharset(contentType); if ("gzip".equals(con.getContentEncoding())) { if (loadPage_debug) print("loadPage: Using gzip."); in = newGZIPInputStream(in); } Reader r; try { r = new InputStreamReader(in, unquote(charset)); } catch (UnsupportedEncodingException e) { print(toHex(utf8(charset))); throw e; } boolean silent = isTrue(loadPage_silent.get()); StringBuilder buf = new StringBuilder(); int n = 0; while (true) { int ch = r.read(); if (ch < 0) break; buf.append((char) ch); ++n; if (!silent && (n % loadPage_verboseness) == 0) print(" " + n + " chars read"); } return buf.toString(); } finally { if (loadPage_debug) print("loadPage done"); vm_generalSubMap("URLConnection per thread").remove(currentThread()); if (in != null) in.close(); } } public static String loadPage_guessCharset(String contentType) { Matcher m = regexpMatcher("text/[a-z]+;\\s*charset=([^\\s]+)\\s*", contentType); String match = m.matches() ? m.group(1) : null; if (loadPage_debug) print("loadPage: contentType=" + contentType + ", match: " + match); return or(match, "UTF-8"); } public static URLConnection loadPage_openConnection(URL url) { URLConnection con = openConnection(url); int timeout = toInt(loadPage_forcedTimeout_byThread.get()); if (timeout == 0) timeout = loadPage_forcedTimeout; if (timeout != 0) setURLConnectionTimeouts(con, loadPage_forcedTimeout); else setURLConnectionDefaultTimeouts(con, loadPage_defaultTimeout); return con; } public static A println(A a) { return print(a); } public static String imageServerURL() { return or2(trim(loadTextFile(javaxDataDir("image-server-url.txt"))), "http://botcompany.de/images/raw/"); } public static File getProgramFile(String progID, String fileName) { if (new File(fileName).isAbsolute()) return new File(fileName); return new File(getProgramDir(progID), fileName); } public static File getProgramFile(String fileName) { return getProgramFile(getProgramID(), fileName); } public static File mkdirsFor(File file) { return mkdirsForFile(file); } public static void copyStreamWithPrints(InputStream in, OutputStream out, String pat) { try { byte[] buf = new byte[65536]; int total = 0; while (true) { int n = in.read(buf); if (n <= 0) return; out.write(buf, 0, n); if ((total + n) / 100000 > total / 100000) print(pat.replace("{*}", str(roundDownTo(total, 100000)))); total += n; } } catch (Exception __e) { throw rethrow(__e); } } public static void renameFile_assertTrue(File a, File b) { try { if (!a.exists()) throw fail("Source file not found: " + f2s(a)); if (b.exists()) throw fail("Target file exists: " + f2s(b)); mkdirsForFile(b); if (!a.renameTo(b)) throw fail("Can't rename " + f2s(a) + " to " + f2s(b)); } catch (Exception __e) { throw rethrow(__e); } } public static File computerIDFile() { return javaxDataDir("Basic Info/computer-id.txt"); } public static String makeRandomID(int length) { return makeRandomID(length, defaultRandomGenerator()); } public static String makeRandomID(int length, Random random) { char[] id = new char[length]; for (int i = 0; i < id.length; i++) id[i] = (char) ((int) 'a' + random.nextInt(26)); return new String(id); } public static String makeRandomID(Random r, int length) { return makeRandomID(length, r); } public static void disposeWindow(final Window window) { if (window != null) { swing(new Runnable() { public void run() { try { window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); myFrames_list.remove(window); window.dispose(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); //..."; } }); } } public static void disposeWindow(final Component c) { disposeWindow(getWindow(c)); } public static void disposeWindow(Object o) { if (o != null) disposeWindow(((Component) o)); } public static void disposeWindow() { disposeWindow(heldInstance(Component.class)); } public static JWindow showLoadingAnimation() { return showLoadingAnimation("Hold on user..."); } public static JWindow showLoadingAnimation(String text) { try { return showAnimationInTopRightCorner("#1003543", text); } catch (Throwable __e) { return null; } } public static LinkedHashMap asLinkedHashMap(Map map) { if (map instanceof LinkedHashMap) return (LinkedHashMap) map; LinkedHashMap m = new LinkedHashMap(); if (map != null) synchronized (collectionMutex(map)) { m.putAll(map); } return m; } public static String loadTextFileFromZip(File inZip, String fileName) { return loadTextFileFromZipFile(inZip, fileName); } public static List myLibraryFiles() { return (List) get(myClassLoader(), "files"); } public static Class hotwire_finish(ClassLoader classLoader, String progID, String javaSource) { return hotwire_finish(classLoader, progID, javaSource, "main"); } public static Class hotwire_finish(ClassLoader classLoader, String progID, String javaSource, String mainClass) { try { Class theClass = classLoader.loadClass(mainClass); Class j = getJavaX(); setOpt(theClass, "myJavaSource_code", javaSource); synchronized (j) { call(j, "setVars", theClass, progID); callOpt(j, "addInstance", progID, theClass); } hotwire_copyOver(theClass); vmBus_send("hotwireFinished", theClass, mc()); return theClass; } catch (Exception __e) { throw rethrow(__e); } } public static List cleanUpAndGetWeakReferencesList(List> l) { if (l == null) return null; synchronized (l) { List out = new ArrayList(); for (int i = 0; i < l(l); i++) { A a = l.get(i).get(); if (a == null) l.remove(i--); else out.add(a); } return out; } } public static Map myFrames_list = weakHashMap(); public static List myFrames() { return swing(new F0>() { public List get() { try { return keysList(myFrames_list); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret keysList(myFrames_list);"; } }); } public static volatile boolean disableCertificateValidation_attempted = false; public static void disableCertificateValidation() { try { if (disableCertificateValidation_attempted) return; disableCertificateValidation_attempted = true; try { TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(hv); } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public static String imageServerLink(String md5OrID) { if (possibleMD5(md5OrID)) return "https://botcompany.de/images/md5/" + md5OrID; return imageServerLink(parseSnippetID(md5OrID)); } public static String imageServerLink(long id) { return "https://botcompany.de/images/" + id; } public static boolean networkAllowanceTest(String url) { return isAllowed("networkAllowanceTest", url); } public static final boolean loadPageThroughProxy_enabled = false; public static String loadPageThroughProxy(String url) { return null; } public static String exceptionToStringShort(Throwable e) { lastException(e); e = getInnerException(e); String msg = hideCredentials(unnull(e.getMessage())); if (msg.indexOf("Error") < 0 && msg.indexOf("Exception") < 0) return baseClassName(e) + prependIfNempty(": ", msg); else return msg; } public static void sleepSeconds(double s) { if (s > 0) sleep(round(s * 1000)); } public static A printWithTime(A a) { return printWithTime("", a); } public static A printWithTime(String s, A a) { print(hmsWithColons() + ": " + s, a); return a; } public static Map vm_generalSubMap(Object name) { synchronized (get(javax(), "generalMap")) { Map map = (Map) (vm_generalMap_get(name)); if (map == null) vm_generalMap_put(name, map = synchroMap()); return map; } } public static InputStream urlConnection_getInputStream(URLConnection con) throws IOException { UnknownHostException lastException = null; for (int _repeat_1206 = 0; _repeat_1206 < 2; _repeat_1206++) { try { if (con instanceof HttpURLConnection) if (((HttpURLConnection) con).getResponseCode() == 500) throw new IOException(joinNemptiesWithColonSpace("Server code 500", tryToReadErrorStreamFromURLConnection(((HttpURLConnection) con)))); return con.getInputStream(); } catch (UnknownHostException e) { lastException = e; print("Retrying because of: " + e); continue; } } throw lastException; } public static String toHex(byte[] bytes) { return bytesToHex(bytes); } public static String toHex(byte[] bytes, int ofs, int len) { return bytesToHex(bytes, ofs, len); } public static Matcher regexpMatcher(String pat, String s) { return compileRegexp(pat).matcher(unnull(s)); } public static URLConnection setURLConnectionDefaultTimeouts(URLConnection con, long timeout) { if (con.getConnectTimeout() == 0) { con.setConnectTimeout(toInt(timeout)); if (con.getConnectTimeout() != timeout) print("Warning: URL connect timeout not set by JDK."); } if (con.getReadTimeout() == 0) { con.setReadTimeout(toInt(timeout)); if (con.getReadTimeout() != timeout) print("Warning: URL read timeout not set by JDK."); } return con; } public static String or2(String a, String b) { return nempty(a) ? a : b; } public static String or2(String a, String b, String c) { return or2(or2(a, b), c); } public static File getProgramDir() { return programDir(); } public static File getProgramDir(String snippetID) { return programDir(snippetID); } public static int roundDownTo(int x, int n) { return x / n * n; } public static long roundDownTo(long x, long n) { return x / n * n; } public static Random defaultRandomGenerator() { return ThreadLocalRandom.current(); } public static Window getWindow(Object o) { if (!(o instanceof Component)) return null; Component c = (Component) o; while (c != null) { if (c instanceof Window) return (Window) c; c = c.getParent(); } return null; } public static A heldInstance(Class c) { List l = holdInstance_l.get(); for (int i = l(l) - 1; i >= 0; i--) { Object o = l.get(i); if (isInstanceOf(o, c)) return (A) o; } throw fail("No instance of " + className(c) + " held"); } public static boolean showAnimationInTopRightCorner_alwaysOnTop = true; public static boolean showAnimationInTopRightCorner_on = true; public static JWindow showAnimationInTopRightCorner(String imageID, String text) { if (isHeadless() || !showAnimationInTopRightCorner_on) return null; return showAnimationInTopRightCorner(imageIcon(imageID), text); } public static JWindow showAnimationInTopRightCorner(final Image image, final String text) { if (image == null || isHeadless() || !showAnimationInTopRightCorner_on) return null; return showAnimationInTopRightCorner(imageIcon(image), text); } public static JWindow showAnimationInTopRightCorner(final ImageIcon imageIcon, final String text) { if (isHeadless() || !showAnimationInTopRightCorner_on) return null; return (JWindow) swingAndWait(new F0() { public Object get() { try { JLabel label = new JLabel(imageIcon); if (nempty(text)) { label.setText(text); label.setVerticalTextPosition(SwingConstants.BOTTOM); label.setHorizontalTextPosition(SwingConstants.CENTER); } final JWindow window = showInTopRightCorner(label); onClick(label, new Runnable() { public void run() { try { window.dispose(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "window.dispose()"; } }); if (showAnimationInTopRightCorner_alwaysOnTop) window.setAlwaysOnTop(true); return window; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JLabel label = new JLabel(imageIcon);\r\n if (nempty(text)) {\r\n label.s..."; } }); } public static JWindow showAnimationInTopRightCorner(final String imageID) { return showAnimationInTopRightCorner(imageID, ""); } public static JWindow showAnimationInTopRightCorner(String imageID, double seconds) { return showAnimationInTopRightCorner(imageID, "", seconds); } public static JWindow showAnimationInTopRightCorner(String imageID, String text, double seconds) { if (isHeadless()) return null; return disposeWindowAfter(iround(seconds * 1000), showAnimationInTopRightCorner(imageID, text)); } public static JWindow showAnimationInTopRightCorner(BufferedImage img, String text, double seconds) { return disposeWindowAfter(iround(seconds * 1000), showAnimationInTopRightCorner(img, text)); } public static ThreadLocal> holdInstance_l = new ThreadLocal(); public static AutoCloseable holdInstance(Object o) { if (o == null) return null; listThreadLocalAdd(holdInstance_l, o); return new AutoCloseable() { public void close() { listThreadLocalPopLast(holdInstance_l); } }; } public static Map weakHashMap() { return newWeakHashMap(); } public static List keysList(Map map) { return cloneListSynchronizingOn(keys(map), map); } public static List keysList(MultiSet ms) { return ms == null ? null : keysList(ms.map); } public static boolean possibleMD5(String s) { return isMD5(s); } public static volatile Object isAllowed_function; public static volatile boolean isAllowed_all = true; public static boolean isAllowed(String askingMethod, Object... args) { Object f = vm_generalMap_get("isAllowed_function"); if (f != null && !isTrue(callF(f, askingMethod, args))) return false; return isAllowed_all || isTrue(callF(isAllowed_function, askingMethod, args)); } public static String baseClassName(String className) { return substring(className, className.lastIndexOf('.') + 1); } public static String baseClassName(Object o) { return baseClassName(getClassName(o)); } public static String prependIfNempty(String prefix, String s) { return empty(s) ? s : prefix + s; } public static volatile boolean sleep_noSleep = false; public static void sleep(long ms) { ping(); if (ms < 0) return; if (isAWTThread() && ms > 100) throw fail("Should not sleep on AWT thread"); try { Thread.sleep(ms); } catch (Exception e) { throw new RuntimeException(e); } } public static void sleep() { try { if (sleep_noSleep) throw fail("nosleep"); print("Sleeping."); sleepQuietly(); } catch (Exception __e) { throw rethrow(__e); } } public static long round(double d) { return Math.round(d); } public static String hmsWithColons() { return hmsWithColons(now()); } public static String hmsWithColons(long time) { return new SimpleDateFormat("HH:mm:ss").format(time); } public static Object vm_generalMap_put(Object key, Object value) { return mapPutOrRemove(vm_generalMap(), key, value); } public static String joinNemptiesWithColonSpace(String... strings) { return joinNempties(": ", strings); } public static String joinNemptiesWithColonSpace(Collection strings) { return joinNempties(": ", strings); } public static String tryToReadErrorStreamFromURLConnection(URLConnection conn) { try { if (conn instanceof HttpURLConnection) return stream2string(((HttpURLConnection) conn).getErrorStream()); return null; } catch (Throwable __e) { return null; } } public static Map compileRegexp_cache = syncMRUCache(10); public static java.util.regex.Pattern compileRegexp(String pat) { java.util.regex.Pattern p = compileRegexp_cache.get(pat); if (p == null) { compileRegexp_cache.put(pat, p = java.util.regex.Pattern.compile(pat)); } return p; } public static File programDir_mine; public static File programDir() { return programDir(getProgramID()); } public static File programDir(String snippetID) { boolean me = sameSnippetID(snippetID, programID()); if (programDir_mine != null && me) return programDir_mine; File dir = new File(javaxDataDir(), formatSnippetIDOpt(snippetID)); if (me) { String c = caseID(); if (nempty(c)) dir = newFile(dir, c); } return dir; } public static File programDir(String snippetID, String subPath) { return new File(programDir(snippetID), subPath); } public static boolean isInstanceOf(Object o, Class type) { return type.isInstance(o); } public static int imageIcon_cacheSize = 10; public static boolean imageIcon_verbose = false; public static Map imageIcon_cache; public static Lock imageIcon_lock = lock(); public static ThreadLocal imageIcon_fixGIF = new ThreadLocal(); public static ImageIcon imageIcon(String imageID) { try { if (imageID == null) return null; Lock __1262 = imageIcon_lock; lock(__1262); try { if (imageIcon_cache == null) imageIcon_cache = new MRUCache(imageIcon_cacheSize); imageID = fsI(imageID); ImageIcon ii = imageIcon_cache.get(imageID); if (ii == null) { if (imageIcon_verbose) print("Loading image icon: " + imageID); File f = loadBinarySnippet(imageID); Boolean b = imageIcon_fixGIF.get(); if (!isFalse(b)) ii = new ImageIcon(loadBufferedImageFixingGIFs(f)); else ii = new ImageIcon(f.toURI().toURL()); } else imageIcon_cache.remove(imageID); imageIcon_cache.put(imageID, ii); return ii; } finally { unlock(__1262); } } catch (Exception __e) { throw rethrow(__e); } } public static ImageIcon imageIcon(File f) { try { return new ImageIcon(f.toURI().toURL()); } catch (Exception __e) { throw rethrow(__e); } } public static ImageIcon imageIcon(Image img) { return new ImageIcon(img); } public static ImageIcon imageIcon(RGBImage img) { return imageIcon(img.getBufferedImage()); } public static JWindow showInTopRightCorner(Component c) { return swing(() -> { JWindow w = new JWindow(); w.add(c); w.pack(); moveToTopRightCorner(w); w.setVisible(true); return w; }); } public static A onClick(final A c, final Object runnable) { if (c != null) { swing(new Runnable() { public void run() { try { c.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { callF(runnable, e); } }); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.addMouseListener(new MouseAdapter {\r\n public void mouseClicked(MouseEv..."; } }); } return c; } public static void onClick(JButton btn, final Object runnable) { onEnter(btn, runnable); } public static A disposeWindowAfter(int delay, final A w) { if (w != null) swingLater(delay, new Runnable() { public void run() { try { w.dispose(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "w.dispose();"; } }); return w; } public static A disposeWindowAfter(A w, double seconds) { return disposeWindowAfter(toMS_int(seconds), w); } public static A disposeWindowAfter(double seconds, A w) { return disposeWindowAfter(w, seconds); } public static int iround(double d) { return (int) Math.round(d); } public static int iround(Number n) { return iround(toDouble(n)); } public static void listThreadLocalAdd(ThreadLocal> tl, A a) { List l = tl.get(); if (l == null) tl.set(l = new ArrayList()); l.add(a); } public static A listThreadLocalPopLast(ThreadLocal> tl) { List l = tl.get(); if (l == null) return null; A a = popLast(l); if (empty(l)) tl.set(null); return a; } public static ArrayList cloneListSynchronizingOn(Collection l, Object mutex) { if (l == null) return new ArrayList(); synchronized (mutex) { return new ArrayList(l); } } public static boolean isMD5(String s) { return l(s) == 32 && isLowerHexString(s); } public static Object sleepQuietly_monitor = new Object(); public static void sleepQuietly() { try { assertFalse(isAWTThread()); synchronized (sleepQuietly_monitor) { sleepQuietly_monitor.wait(); } } catch (Exception __e) { throw rethrow(__e); } } public static B mapPutOrRemove(Map map, A key, B value) { if (map != null && key != null) if (value != null) return map.put(key, value); else return map.remove(key); return null; } public static String joinNempties(String sep, String... strings) { return joinStrings(sep, strings); } public static String joinNempties(String sep, Collection strings) { return joinStrings(sep, strings); } public static String stream2string(InputStream in) { return utf8streamToString(in); } public static Map syncMRUCache(int size) { return synchroMap(new MRUCache(size)); } public static boolean sameSnippetID(String a, String b) { if (!isSnippetID(a) || !isSnippetID(b)) return false; return parseSnippetID(a) == parseSnippetID(b); } public static volatile String caseID_caseID; public static String caseID() { return caseID_caseID; } public static void caseID(String id) { caseID_caseID = id; } public static boolean loadBufferedImageFixingGIFs_debug = false; public static ThreadLocal> loadBufferedImageFixingGIFs_output = new ThreadLocal(); public static Image loadBufferedImageFixingGIFs(File file) { try { if (!file.exists()) return null; if (!isGIF(file)) return ImageIO.read(file); if (loadBufferedImageFixingGIFs_debug) print("loadBufferedImageFixingGIFs" + ": checking gif"); ImageReader reader = ImageIO.getImageReadersByFormatName("gif").next(); reader.setInput(ImageIO.createImageInputStream(file)); int numImages = reader.getNumImages(true); IIOMetadata imageMetaData = reader.getImageMetadata(0); String metaFormatName = imageMetaData.getNativeMetadataFormatName(); boolean foundBug = false; for (int i = 0; i < numImages && !foundBug; i++) { IIOMetadataNode root = (IIOMetadataNode) reader.getImageMetadata(i).getAsTree(metaFormatName); int nNodes = root.getLength(); for (int j = 0; j < nNodes; j++) { org.w3c.dom.Node node = root.item(j); if (node.getNodeName().equalsIgnoreCase("GraphicControlExtension")) { String delay = ((IIOMetadataNode) node).getAttribute("delayTime"); if (Integer.parseInt(delay) == 0) { foundBug = true; } break; } } } if (loadBufferedImageFixingGIFs_debug) print("loadBufferedImageFixingGIFs" + ": " + f2s(file) + " foundBug=" + foundBug); Image image; if (!foundBug) { image = Toolkit.getDefaultToolkit().createImage(f2s(file)); } else { ByteArrayOutputStream baoStream = new ByteArrayOutputStream(); { ImageOutputStream ios = ImageIO.createImageOutputStream(baoStream); try { ImageWriter writer = ImageIO.getImageWriter(reader); writer.setOutput(ios); writer.prepareWriteSequence(null); for (int i = 0; i < numImages; i++) { BufferedImage frameIn = reader.read(i); IIOMetadataNode root = (IIOMetadataNode) reader.getImageMetadata(i).getAsTree(metaFormatName); int nNodes = root.getLength(); for (int j = 0; j < nNodes; j++) { org.w3c.dom.Node node = root.item(j); if (node.getNodeName().equalsIgnoreCase("GraphicControlExtension")) { String delay = ((IIOMetadataNode) node).getAttribute("delayTime"); if (Integer.parseInt(delay) == 0) { ((IIOMetadataNode) node).setAttribute("delayTime", "10"); } break; } } IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(frameIn), null); metadata.setFromTree(metadata.getNativeMetadataFormatName(), root); IIOImage frameOut = new IIOImage(frameIn, null, metadata); writer.writeToSequence(frameOut, writer.getDefaultWriteParam()); } writer.endWriteSequence(); } finally { _close(ios); } } byte[] data = baoStream.toByteArray(); setVar(loadBufferedImageFixingGIFs_output.get(), data); if (loadBufferedImageFixingGIFs_debug) print("Data size: " + l(data)); image = Toolkit.getDefaultToolkit().createImage(data); } return image; } catch (Exception __e) { throw rethrow(__e); } } public static int moveToTopRightCorner_inset = 20; public static A moveToTopRightCorner(A a) { return moveToTopRightCorner(moveToTopRightCorner_inset, moveToTopRightCorner_inset, a); } public static A moveToTopRightCorner(int insetX, int insetY, A a) { Window w = getWindow(a); if (w != null) w.setLocation(getScreenSize().width - w.getWidth() - insetX, insetY); return a; } public static JTextField onEnter(final JTextField tf, final Object action) { if (action == null || tf == null) return tf; tf.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try { tf.selectAll(); callF(action); } catch (Throwable __e) { messageBox(__e); } } }); return tf; } public static JButton onEnter(JButton btn, final Object action) { if (action == null || btn == null) return btn; btn.addActionListener(actionListener(action)); return btn; } public static JList onEnter(JList list, Object action) { list.addKeyListener(enterKeyListener(rCallOnSelectedListItem(list, action))); return list; } public static JComboBox onEnter(final JComboBox cb, final Object action) { { swing(new Runnable() { public void run() { try { if (cb.isEditable()) { JTextField text = (JTextField) cb.getEditor().getEditorComponent(); onEnter(text, action); } else { cb.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "enter"); cb.getActionMap().put("enter", abstractAction("", new Runnable() { public void run() { try { cb.hidePopup(); callF(action); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "cb.hidePopup(); callF(action);"; } })); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (cb.isEditable()) {\r\n JTextField text = (JTextField) cb.getEditor().g..."; } }); } return cb; } public static JTable onEnter(final JTable table, final Object action) { table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter"); table.getActionMap().put("Enter", new AbstractAction() { public void actionPerformed(ActionEvent e) { callF(action, table.getSelectedRow()); } }); return table; } public static JTextField onEnter(Object action, JTextField tf) { return onEnter(tf, action); } public static void swingLater(long delay, final Object r) { javax.swing.Timer timer = new javax.swing.Timer(toInt(delay), actionListener(wrapAsActivity(r))); timer.setRepeats(false); timer.start(); } public static void swingLater(Object r) { SwingUtilities.invokeLater(toRunnable(r)); } public static int toMS_int(double seconds) { return toInt_checked((long) (seconds * 1000)); } public static double toDouble(Object o) { if (o instanceof Number) return ((Number) o).doubleValue(); if (o instanceof BigInteger) return ((BigInteger) o).doubleValue(); if (o == null) return 0.0; throw fail(o); } public static A popLast(List l) { return liftLast(l); } public static List popLast(int n, List l) { return liftLast(n, l); } public static boolean isLowerHexString(String s) { for (int i = 0; i < l(s); i++) { char c = s.charAt(i); if (c >= '0' && c <= '9' || c >= 'a' && c <= 'f') { } else return false; } return true; } public static String joinStrings(String sep, String... strings) { return joinStrings(sep, Arrays.asList(strings)); } public static String joinStrings(String sep, Collection strings) { StringBuilder buf = new StringBuilder(); for (String s : unnull(strings)) if (nempty(s)) { if (nempty(buf)) buf.append(sep); buf.append(s); } return str(buf); } public static String utf8streamToString(InputStream in) { return readerToString(utf8bufferedReader(in)); } public static byte[] isGIF_magic = bytesFromHex("47494638"); public static boolean isGIF(byte[] data) { return byteArrayStartsWith(data, isGIF_magic); } public static boolean isGIF(File f) { return isGIF(loadBeginningOfBinaryFile(f, l(isGIF_magic))); } public static void setVar(IVar v, A value) { if (v != null) v.set(value); } public static Dimension getScreenSize() { return Toolkit.getDefaultToolkit().getScreenSize(); } public static void messageBox(final String msg) { if (headless()) print(msg); else { swing(new Runnable() { public void run() { try { JOptionPane.showMessageDialog(null, msg, "JavaX", JOptionPane.INFORMATION_MESSAGE); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "JOptionPane.showMessageDialog(null, msg, \"JavaX\", JOptionPane.INFORMATION_MES..."; } }); } } public static void messageBox(Throwable e) { printStackTrace(e); messageBox(hideCredentials(innerException2(e))); } public static ActionListener actionListener(final Object runnable) { return actionListener(runnable, null); } public static ActionListener actionListener(final Object runnable, final Object instanceToHold) { if (runnable instanceof ActionListener) return (ActionListener) runnable; final Object info = _threadInfo(); return new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent _evt) { try { _threadInheritInfo(info); AutoCloseable __1323 = holdInstance(instanceToHold); try { callF(runnable); } finally { _close(__1323); } } catch (Throwable __e) { messageBox(__e); } } }; } public static KeyListener enterKeyListener(final Object action) { return new KeyAdapter() { public void keyPressed(KeyEvent ke) { if (ke.getKeyCode() == KeyEvent.VK_ENTER) pcallF(action); } }; } public static Runnable rCallOnSelectedListItem(final JList list, final Object action) { return new Runnable() { public void run() { try { pcallF(action, getSelectedItem(list)); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcallF(action, getSelectedItem(list))"; } }; } public static AbstractAction abstractAction(String name, final Object runnable) { return new AbstractAction(name) { public void actionPerformed(ActionEvent evt) { pcallF(runnable); } }; } public static int toInt_checked(long l) { if (l != (int) l) throw fail("Too large for int: " + l); return (int) l; } public static A liftLast(List l) { if (empty(l)) return null; int i = l(l) - 1; A a = l.get(i); l.remove(i); return a; } public static List liftLast(int n, List l) { int i = l(l) - n; List part = cloneSubList(l, i); removeSubList(l, i); return part; } public static String readerToString(Reader r) { try { try { StringBuilder buf = new StringBuilder(); int n = 0; while (true) { int ch = r.read(); if (ch < 0) break; buf.append((char) ch); ++n; } return buf.toString(); } finally { r.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static byte[] bytesFromHex(String s) { return hexToBytes(s); } public static boolean byteArrayStartsWith(byte[] a, byte[] b) { if (a == null || b == null) return false; if (a.length < b.length) return false; for (int i = 0; i < b.length; i++) if (a[i] != b[i]) return false; return true; } public static byte[] loadBeginningOfBinaryFile(File file, int maxBytes) { return loadBinaryFilePart(file, 0, maxBytes); } public static boolean headless() { return isHeadless(); } public static String getSelectedItem(JList l) { return (String) l.getSelectedValue(); } public static String getSelectedItem(JComboBox cb) { return strOrNull(cb.getSelectedItem()); } public static byte[] hexToBytes(String s) { if (odd(l(s))) throw fail("Hex string has odd length: " + quote(shorten(10, s))); int n = l(s) / 2; byte[] bytes = new byte[n]; for (int i = 0; i < n; i++) { int a = parseHexChar(s.charAt(i * 2)); int b = parseHexChar(s.charAt(i * 2 + 1)); if (a < 0 || b < 0) throw fail("Bad hex byte: " + quote(substring(s, i * 2, i * 2 + 2)) + " at " + i * 2 + "/" + l(s)); bytes[i] = (byte) ((a << 4) | b); } return bytes; } public static byte[] loadBinaryFilePart(File file, long start, long end) { try { RandomAccessFile raf = new RandomAccessFile(file, "r"); int n = toInt(min(raf.length(), end - start)); byte[] buffer = new byte[n]; try { raf.seek(start); raf.readFully(buffer, 0, n); return buffer; } finally { raf.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static String strOrNull(Object o) { return o == null ? null : str(o); } public static int shorten_default = 100; public static String shorten(String s) { return shorten(s, shorten_default); } public static String shorten(String s, int max) { return shorten(s, max, "..."); } public static String shorten(String s, int max, String shortener) { if (s == null) return ""; if (max < 0) return s; return s.length() <= max ? s : substring(s, 0, min(s.length(), max - l(shortener))) + shortener; } public static String shorten(int max, String s) { return shorten(s, max); } public static int parseHexChar(char c) { if (c >= '0' && c <= '9') return charDiff(c, '0'); if (c >= 'a' && c <= 'f') return charDiff(c, 'a') + 10; if (c >= 'A' && c <= 'F') return charDiff(c, 'A') + 10; return -1; } public static int charDiff(char a, char b) { return (int) a - (int) b; } public static int charDiff(String a, char b) { return charDiff(stringToChar(a), b); } public static char stringToChar(String s) { if (l(s) != 1) throw fail("bad stringToChar: " + s); return firstChar(s); } public static char firstChar(String s) { return s.charAt(0); } public static final class _MethodCache { public final Class c; public final HashMap> cache = new HashMap(); public _MethodCache(Class c) { this.c = c; _init(); } public void _init() { Class _c = c; while (_c != null) { for (Method m : _c.getDeclaredMethods()) if (!reflection_isForbiddenMethod(m)) multiMapPut(cache, m.getName(), makeAccessible(m)); _c = _c.getSuperclass(); } for (Class intf : allInterfacesImplementedBy(c)) for (Method m : intf.getDeclaredMethods()) if (m.isDefault() && !reflection_isForbiddenMethod(m)) multiMapPut(cache, m.getName(), makeAccessible(m)); } public Method findMethod(String method, Object[] args) { try { List m = cache.get(method); if (m == null) return null; int n = m.size(); for (int i = 0; i < n; i++) { Method me = m.get(i); if (call_checkArgs(me, args, false)) return me; } return null; } catch (Exception __e) { throw rethrow(__e); } } public Method findStaticMethod(String method, Object[] args) { try { List m = cache.get(method); if (m == null) return null; int n = m.size(); for (int i = 0; i < n; i++) { Method me = m.get(i); if (isStaticMethod(me) && call_checkArgs(me, args, false)) return me; } return null; } catch (Exception __e) { throw rethrow(__e); } } } public abstract static class VF2 { public abstract void get(A a, B b); } public static class Rule_StringToStrings { public String in; public List out; public Set vars; public Rule_StringToStrings() { } public Rule_StringToStrings(String in, List out, Set vars) { this.vars = vars; this.out = out; this.in = in; } public boolean equals(Object o) { if (!(o instanceof Rule_StringToStrings)) return false; Rule_StringToStrings x = (Rule_StringToStrings) o; return eq(in, x.in) && eq(out, x.out) && eq(vars, x.vars); } public int hashCode() { int h = 1731632947; h = boostHashCombine(h, _hashCode(in)); h = boostHashCombine(h, _hashCode(out)); h = boostHashCombine(h, _hashCode(vars)); return h; } public Rule_StringToStrings(String in) { this.in = in; } public Rule_StringToStrings(String in, List out) { this.out = out; this.in = in; } public String toString() { return "Rule_StringToStrings: " + in + " :=\n" + (vars == null ? "" : indentx("vars " + curly(joinWithComma(vars))) + "\n") + indentx(pnlToString("out ", out)); } } public static class Var implements IVar { public Var() { } public Var(A v) { this.v = v; } public A v; public synchronized void set(A a) { if (v != a) { v = a; notifyAll(); } } public synchronized A get() { return v; } public synchronized boolean has() { return v != null; } public synchronized void clear() { v = null; } public String toString() { return str(get()); } } public static class MechListDependentCache extends Cache { public String listName, lastListContents; public MechListDependentCache() { } public MechListDependentCache(String listName, Object maker) { this.maker = maker; this.listName = listName; } public A make() { return (A) callF(maker, lastListContents); } public A get() { if (hasLock(lock)) return value; { String listContents = mL_raw(listName); Lock __1378 = lock; lock(__1378); try { if (!eqOrSame(listContents, lastListContents)) clear(); lastListContents = listContents; } finally { unlock(__1378); } } return super.get(); } } public abstract static class TokCondition { public abstract boolean get(List tok, int i); } public static class MechListDependentCaches { public Map> byList = ciMap(); public Object maker; public transient Lock lock = lock(); public MechListDependentCaches(Object maker) { this.maker = maker; } public A get(String name) { MechListDependentCache listCache; { Lock __1383 = lock; lock(__1383); try { listCache = byList.get(name); if (listCache == null) byList.put(name, listCache = new MechListDependentCache(name, maker)); } finally { unlock(__1383); } } return listCache.get(); } } public static class MultiMap { public Map> data = new HashMap>(); public MultiMap() { } public MultiMap(boolean useTreeMap) { if (useTreeMap) data = new TreeMap(); } public MultiMap(MultiMap map) { putAll(map); } public MultiMap(Map> data) { this.data = data; } public void put(A key, B value) { synchronized (data) { List list = data.get(key); if (list == null) data.put(key, list = _makeEmptyList()); list.add(value); } } public void add(A key, B value) { put(key, value); } public void addAll(A key, Collection values) { putAll(key, values); } public void addAllIfNotThere(A key, Collection values) { synchronized (data) { for (B value : values) setPut(key, value); } } public void setPut(A key, B value) { synchronized (data) { if (!containsPair(key, value)) put(key, value); } } public boolean containsPair(A key, B value) { synchronized (data) { return get(key).contains(value); } } public void putAll(A key, Collection values) { synchronized (data) { if (nempty(values)) getActual(key).addAll(values); } } public void removeAll(A key, Collection values) { synchronized (data) { for (B value : values) remove(key, value); } } public List get(A key) { synchronized (data) { List list = data.get(key); return list == null ? Collections.emptyList() : list; } } public List getActual(A key) { synchronized (data) { List list = data.get(key); if (list == null) data.put(key, list = _makeEmptyList()); return list; } } public void clean(A key) { synchronized (data) { List list = data.get(key); if (list != null && list.isEmpty()) data.remove(key); } } public Set keySet() { synchronized (data) { return data.keySet(); } } public Set keys() { synchronized (data) { return data.keySet(); } } public void remove(A key) { synchronized (data) { data.remove(key); } } public void remove(A key, B value) { synchronized (data) { List list = data.get(key); if (list != null) { list.remove(value); if (list.isEmpty()) data.remove(key); } } } public void clear() { synchronized (data) { data.clear(); } } public boolean containsKey(A key) { synchronized (data) { return data.containsKey(key); } } public B getFirst(A key) { synchronized (data) { List list = get(key); return list.isEmpty() ? null : list.get(0); } } public void addAll(MultiMap map) { putAll(map); } public void putAll(MultiMap map) { synchronized (data) { for (A key : map.keySet()) putAll(key, map.get(key)); } } public void putAll(Map map) { synchronized (data) { if (map != null) for (Map.Entry e : map.entrySet()) put(e.getKey(), e.getValue()); } } public int keysSize() { synchronized (data) { return l(data); } } public int size() { synchronized (data) { int n = 0; for (List l : data.values()) n += l(l); return n; } } public List reverseGet(B b) { synchronized (data) { List l = new ArrayList(); for (A key : data.keySet()) if (data.get(key).contains(b)) l.add(key); return l; } } public Map> asMap() { synchronized (data) { return cloneMap(data); } } public boolean isEmpty() { synchronized (data) { return data.isEmpty(); } } public List _makeEmptyList() { return new ArrayList(); } public Collection> allLists() { synchronized (data) { return new ArrayList(data.values()); } } public List allValues() { return concatLists(values(data)); } public Object mutex() { return data; } public String toString() { return "mm" + str(data); } } public static class Lowest { public A best; public double score; public transient Object onChange; public synchronized boolean isNewBest(double score) { return best == null || score < this.score; } public synchronized double bestScore() { return best == null ? Double.NaN : score; } public double score() { return bestScore(); } public synchronized float floatScore() { return best == null ? Float.NaN : (float) score; } public synchronized float floatScoreOr(float defaultValue) { return best == null ? defaultValue : (float) score; } public boolean put(A a, double score) { boolean change = false; synchronized (this) { if (a != null && isNewBest(score)) { best = a; this.score = score; change = true; } } if (change) pcallF(onChange); return change; } public synchronized A get() { return best; } public synchronized boolean has() { return best != null; } public synchronized Pair pair() { return best == null ? null : new Pair(best, bestScore()); } } public static class _PrintIndent extends F1 { public String prefix; public boolean beginningOfLine = true; public _PrintIndent() { } public _PrintIndent(String prefix) { this.prefix = prefix; } public Boolean get(String s) { if (empty(s)) return false; if (beginningOfLine) print_raw(prefix); boolean nl = s.endsWith("\n"); if (nl) s = dropLast(s); s = s.replace("\n", "\n" + prefix); print_raw(s); if (nl) print_raw("\n"); beginningOfLine = nl; return false; } } public static interface IResourceLoader { public String loadSnippet(String snippetID); public String getTranspiled(String snippetID); public int getSnippetType(String snippetID); public String getSnippetTitle(String snippetID); public File loadLibrary(String snippetID); public File pathToJavaXJar(); public File getSnippetJar(String snippetID, String transpiledSrc); } public static class CompilerBot { public static boolean verbose = false; public static File compileSnippet(String snippetID) { return compileSnippet(snippetID, ""); } public static Pair compileSnippet2(String snippetID) { return compileSnippet2(snippetID, ""); } public static File compileSnippet(String snippetID, String javaTarget) { return compileSnippet2(snippetID, javaTarget).a; } public static Pair compileSnippet2(String snippetID, String javaTarget) { String transpiledSrc = getServerTranspiled2(snippetID); int i = transpiledSrc.indexOf('\n'); String libs = transpiledSrc.substring(0, Math.max(0, i)); if (verbose) print("Compiling snippet: " + snippetID + ". Libs: " + libs); transpiledSrc = transpiledSrc.substring(i + 1); return pair(compile(transpiledSrc, libs, javaTarget, snippetID), transpiledSrc); } public static File compile(String src) { return compile(src, ""); } public static File compile(String src, String libs) { return compile(src, libs, null); } public static File compile(String src, String dehlibs, String javaTarget) { return compile(src, dehlibs, javaTarget, null); } public static File compile(String src, String dehlibs, String javaTarget, String progID) { if (verbose) print("Compiling " + l(src) + " chars"); String md5 = md5(dehlibs + "\n" + src + "\n" + fsIOpt(progID)); File jar = getJarFile(md5); if (jar == null || jar.length() <= 22) { print("Have to compile: " + progID + " / " + md5); List mainClass = findMainClass(javaTok(src)); boolean canRename = mainClass != null && useDummyMainClasses() && isSnippetID(progID) && !tok_classHasModifier(mainClass, "public"); if (verbose) print("useRenaming: " + useDummyMainClasses() + ", canRename: " + canRename + ", progID: " + progID); javaCompileToJar_optionalRename(src, dehlibs, jar, canRename ? progID : null, progID); } else { if (verbose) print("Getting classes from cache (" + jar.getAbsolutePath() + ", " + jar.length() + " bytes)"); touchFile(jar); } return jar; } public static File getJarFile(String md5) { assertTrue(isMD5(md5)); String sub = "JavaX-Caches/#1002203/" + md5 + ".jar"; File f = actualUserDir(sub); return fileExists(f) ? f : userDir(sub); } } public static class Pair implements Comparable> { public A a; public B b; public Pair() { } public Pair(A a, B b) { this.b = b; this.a = a; } public int hashCode() { return hashCodeFor(a) + 2 * hashCodeFor(b); } public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Pair)) return false; Pair t = (Pair) o; return eq(a, t.a) && eq(b, t.b); } public String toString() { return "<" + a + ", " + b + ">"; } public int compareTo(Pair p) { if (p == null) return 1; int i = ((Comparable) a).compareTo(p.a); if (i != 0) return i; return ((Comparable) b).compareTo(p.b); } } public static class AI_BottomUpParser1 { public Map> wordsToCategories; public boolean splitSplittables = true; public String productionsText; public List words; public List rawWords; public List cnc; public LinkedHashSet allObjects = new LinkedHashSet(); public long changes, iterations; public MultiMap productionsByA = new MultiMap(); public MultiMap subClasses = new MultiMap(); public boolean mergeGroupsInSamePosition = true; public HashMap groupsByPosition = new HashMap(); public boolean verbose_callPlausibilityFunction = false; public Map> categoriesAtWordIndex; public int maxDepth = 100; public boolean maxDepthReached = false; public Runnable afterIteration, afterParse; public class Updatable { public void update() { } public void setField(String field, Object value) { if (eq(get(this, field), value)) return; set(this, field, value); change(); } } public class Expectation { public String ifClass; public Runnable action; public Expectation() { } public Expectation(String ifClass, Runnable action) { this.action = action; this.ifClass = ifClass; } } public class Word extends Updatable { public String text; public int wordFrom, wordTo; public LinkedHashSet prev = new LinkedHashSet(); public LinkedHashSet next = new LinkedHashSet(); public LinkedHashSet> constituents = new LinkedHashSet(); public List expectations = new ArrayList(); public List fulfilledExpectations = new ArrayList(); public TreeSet classes = new TreeSet(); public int classesConvertedToTraits; public LinkedHashSet groups = new LinkedHashSet(); public List traits = new ArrayList(); public Word() { } public Word(String text, int wordFrom, int wordTo) { this.wordTo = wordTo; this.wordFrom = wordFrom; this.text = text; classes.add(quote(lower(text))); } public void update() { if (text != null) for (String c : unnull(wordsToCategories.get(text))) addClass(c); for (Expectation e : cloneList(expectations)) { if (classes.contains(e.ifClass)) { moveElementFromCollectionToCollection(e, expectations, fulfilledExpectations); change(); callF(e.action); } } if (l(classes) > classesConvertedToTraits) { for (final String c : dropFirst(classesConvertedToTraits, classes)) addTraitsForClass(c); classesConvertedToTraits = l(classes); } for (Trait t : iterateListConcurrently(traits)) t.update(); } public boolean isGroup() { return nempty(constituents); } public boolean addClass(String c) { if (!classes.addAll(makeHull_optimized(subClasses, c))) return false; change(); return true; } public void addExpectation(Expectation e) { expectations.add(e); change(); } public void addTraitsForClass(String c) { for (WeightedProduction p : productionsByA.get(c)) addTrait(new LinkWithTo(p.b, p.c, p.plausibilityFunction)); } public void addTrait(Trait t) { set(t, "w", this); traits.add(t); } public String toString() { return textAndClasses(this); } public boolean hasClass(String c) { return contains(classes, c); } public String text() { return text; } } public Word makeGroup(Word a, Word b, String newClass) { List list = null; Word g = null; if (mergeGroupsInSamePosition) g = groupsByPosition.get(new IntRange(a.wordFrom, b.wordTo)); else { list = ll(a, b); for (Word _g : a.groups) if (contains(_g.constituents, list)) { g = _g; break; } } if (list == null) list = ll(a, b); if (g != null) { g.constituents.add(list); if (g.addClass(newClass)) { } return g; } g = new Word(joinWithSpace(a.text, b.text), a.wordFrom, b.wordTo); allObjects.add(g); if (mergeGroupsInSamePosition) groupsByPosition.put(new IntRange(a.wordFrom, b.wordTo), g); g.addClass(newClass); g.constituents.add(list); for (Word w : list) w.groups.add(g); g.prev.addAll(a.prev); g.next.addAll(b.next); for (Word prev : a.prev) prev.next.add(g); for (Word next : b.next) next.prev.add(g); return g; } public class Trait extends Updatable { public Word w; } public class LinkWithTo extends Trait { public String linkWith, linkTo; public String plausibilityFunction; public int expectationsSentToNext; public LinkWithTo() { } public LinkWithTo(String linkWith, String linkTo, String plausibilityFunction) { this.plausibilityFunction = plausibilityFunction; this.linkTo = linkTo; this.linkWith = linkWith; } public void update() { if (l(w.next) > expectationsSentToNext) { for (final Word next : dropFirst(expectationsSentToNext, w.next)) next.addExpectation(new Expectation(linkWith, new Runnable() { public void run() { try { if (ai_parser_activateStandardFunctions_get() && plausibilityFunction != null) { loadFunctions_preferDiskCache(); Object result = pcallAndMake(plausibilityFunction, w.text, next.text); if (verbose_callPlausibilityFunction) print("Called plausibility function " + plausibilityFunction + ": " + w.text + " + " + next.text + " => " + result); if (isFalse(result)) return; } makeGroup(w, next, linkTo); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (ai_parser_activateStandardFunctions_get() && plausibilityFunction != null..."; } })); expectationsSentToNext = l(w.next); } } } public void parse(List tok) { cnc = (tok); rawWords = codeTokens(cnc); _parse(); } public AI_BottomUpParser1 parse(final String sentence) { rawWords = words_withAngleBrackets(sentence); _parse(); return this; } public void _parse() { if (words != null) throw fail("only call once"); if (splitSplittables) ai_splitSplittables(rawWords); if (wordsToCategories == null) wordsToCategories = ai_wordToCategories(); parseProductions(); words = new ArrayList(); for (int i = 0; i < l(rawWords); i++) { Word w = new Word(rawWords.get(i), i, i + 1); words.add(w); if (isAngleBracketed(w.text)) w.addClass(w.text); else if (isQuoted(w.text)) w.addClass(""); else if (isInteger(w.text)) w.addClass(""); for (String cat : unnull(mapGet(categoriesAtWordIndex, i))) w.addClass(cat); } for (int i = 0; i < l(words) - 1; i++) linkWords(words.get(i), words.get(i + 1)); addAll(allObjects, words); long lastChanges; do { lastChanges = changes; for (Updatable w : cloneList(allObjects)) w.update(); ++iterations; callF(afterIteration); } while (licensed() && lastChanges != changes); callF(afterParse); } public void printWordsAndGroups() { for (Word w : words) print(" " + textAndClasses(w)); print(); List groups = groups(); print(); print(n2(groups, "group")); for (Word g : groups) print("Group: " + groupedTextAndClasses(g)); } public void printConstituentsOfFullGroup() { Word g = fullGroup(); if (g == null) return; print(); pnl(allGroupings(g)); } public List words() { return instancesOf(Word.class, allObjects); } public List groups() { return filter(words(), new F1() { public Boolean get(Word w) { try { return w.isGroup(); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "w.isGroup()"; } }); } public List fullGroups() { return filterByFields(words(), "wordFrom", 0, "wordTo", l(words)); } public Word fullGroup() { return findByFields(words(), "wordFrom", 0, "wordTo", l(words)); } public Set fullClasses() { TreeSet set = new TreeSet(); for (Word g : fullGroups()) set.addAll(g.classes); return set; } public String bracketStuff(Word w) { return " (" + joinWithComma(w.classes) + (empty(w.constituents) ? "" : ", " + n2(w.constituents, "grouping")) + ")"; } public String textAndClasses(Word w) { return w.text + bracketStuff(w); } public String groupedTextAndClasses(Word w) { return grouped(w) + bracketStuff(w); } public String fullGrouped() { return grouped(fullGroup()); } public void linkWords(Word a, Word b) { a.next.add(b); b.prev.add(a); } public void change() { ++changes; } public void parseProductions() { for (WeightedProduction p : productionsText == null ? ai_buParser_parseWeightedProductions() : ai_buParser_parseWeightedProductions(productionsText)) if (p.b != null) productionsByA.put(p.a, p); else subClasses.put(p.a, p.c); } public String grouped(Word g) { if (g == null) return null; if (empty(g.constituents)) return g.text; return groupedConstituents(first(g.constituents)); } public String groupedConstituents(List constituents) { List l = new ArrayList(); for (Word w : constituents) l.add(curlyBraceIfMultipleTokens(grouped(w))); return joinWithSpace(l); } public Set allGroupings(Word g) { if (empty(g.constituents)) return litorderedset(g.text); LinkedHashSet set = new LinkedHashSet(); for (List l : g.constituents) set.add(groupedConstituents(l)); return set; } } public static class JavaXClassLoader extends URLClassLoader { public String progID; public List files; public Set triedToLoad = synchroSet(); public Set loadedClasses = synchroSet(); public boolean retired = false; public Object retiredMarker; public JavaXClassLoader(String progID, List files) { this(progID, files, getSystemClassLoader()); } public JavaXClassLoader(String progID, List files, ClassLoader parent) { super(new URL[0], parent); this.progID = progID; this.files = files; { try { for (File f : files) addURL(f.toURI().toURL()); } catch (Exception __e) { throw rethrow(__e); } } fixACCInClassLoader(this); } public Class findClass(String name) throws ClassNotFoundException { boolean triedBefore = !triedToLoad.add(name); try { Class c = super.findClass(name); loadedClasses.add(c); if (eq(name, "main")) callOpt(javax(), "registerAMainClass", c); return c; } catch (ClassNotFoundException e) { throw new ClassNotFoundException("Class " + name + " not found in " + joinWithComma(map("f2s", files)) + " (progID=" + progID + ")" + (triedBefore ? ", tried to load before" : ""), e); } } public String toString() { return "JavaXClassLoader[" + systemHashCodeHex(this) + "] - " + progID; } } public static class JavaXClassLoaderWithParent extends JavaXClassLoader { public ClassLoader virtualParent; public JavaXClassLoaderWithParent(String progID, List files, ClassLoader virtualParent) { super(progID, files); this.virtualParent = virtualParent; } public Class findClass(String name) throws ClassNotFoundException { if (virtualParent != null && !eq(name, "main") && !name.startsWith("main$")) { try { return virtualParent.loadClass(name); } catch (ClassNotFoundException e) { } } return super.findClass(name); } } public abstract static class IterableIterator implements Iterator, Iterable { public Iterator iterator() { return this; } public void remove() { unsupportedOperation(); } } public abstract static class RandomAccessAbstractList extends AbstractList implements RandomAccess { } public static class MRUCache extends LinkedHashMap { public int maxSize = 10; public MRUCache() { } public MRUCache(int maxSize) { this.maxSize = maxSize; } public boolean removeEldestEntry(Map.Entry eldest) { return size() > maxSize; } public Object _serialize() { return ll(maxSize, cloneLinkedHashMap(this)); } public static MRUCache _deserialize(List l) { MRUCache m = new MRUCache(); m.maxSize = (int) first(l); m.putAll((LinkedHashMap) second(l)); return m; } } public static interface IF0 { public A get(); } public abstract static class CloseableIterableIterator extends IterableIterator implements AutoCloseable { public void close() throws Exception { } } public static class ExpiringMap2 extends AbstractMap { public Map> byKey = new HashMap(); public PriorityBlockingQueue> queue = new PriorityBlockingQueue(); public long standardExpiryTime; public boolean renewOnOverwrite = true, renewOnGet; public Object onChange; public ExpiringMap2() { } public ExpiringMap2(long standardExpiryTime) { this.standardExpiryTime = standardExpiryTime; } public ExpiringMap2(long standardExpiryTime, Object onChange) { this.onChange = onChange; this.standardExpiryTime = standardExpiryTime; } public synchronized boolean clean() { boolean changes = false; Pair p; while ((p = queue.peek()) != null && sysTime() >= p.a) { p = queue.poll(); Pair v = byKey.get(p.b); if (v != null) { byKey.remove(p.b); changes = true; change(); } } return changes; } public void change() { callF(onChange); } public synchronized B put(A a, B b) { clean(); long timeout = sysTime() + standardExpiryTime; Pair p = byKey.get(a); if (p != null && renewOnOverwrite) queue.remove(new Pair(p.a, a)); byKey.put(a, pair(timeout, b)); change(); if (p == null || renewOnOverwrite) queue.add(new Pair(timeout, a)); return pairB(p); } public synchronized B remove(Object a) { clean(); Pair p = byKey.get(a); if (p == null) return null; queue.remove(new Pair(p.a, a)); byKey.remove(a); change(); return p.b; } public synchronized B get(Object a) { clean(); Pair p = byKey.get(a); if (renewOnGet && p != null) { queue.remove(new Pair(p.a, a)); long timeout = sysTime() + standardExpiryTime; byKey.put((A) a, pair(timeout, p.b)); queue.add(new Pair(timeout, a)); } return pairB(p); } public synchronized Set> entrySet() { clean(); return synchronizedSet(mapValues("pairB", byKey).entrySet()); } public synchronized Set keySet() { clean(); return synchronizedSet(byKey.keySet()); } public synchronized int size() { clean(); return byKey.size(); } public void setStandardExpiryTime(long ms) { standardExpiryTime = ms; } public synchronized ExpiringMap2 setMap(Map innerMap) { byKey = innerMap; return this; } } public static class JavaXClassLoaderWithParent2 extends JavaXClassLoader { public ClassLoader virtualParent; public List classesToSkip; public JavaXClassLoaderWithParent2(String progID, List files, ClassLoader virtualParent, List classesToSkip) { super(progID, files); this.virtualParent = virtualParent; this.classesToSkip = classesToSkip; } public Class findClass(String name) throws ClassNotFoundException { if (shouldDelegate(name)) { Class c = virtualParent.loadClass(name); if (c != null) return c; } return super.findClass(name); } public boolean shouldDelegate(String name) { for (String s : classesToSkip) if (eq(name, s) || startsWith(name, s + "$")) return true; return false; } } public static interface IF2 { public C get(A a, B b); } public static interface IVar { public void set(A a); public A get(); public default boolean has() { return get() != null; } public default void clear() { set(null); } } public static class MechMode { public Object readMode; public Object writeMode; public MechMode() { } public MechMode(Object readMode, Object writeMode) { this.writeMode = writeMode; this.readMode = readMode; } public String toString() { return "MechMode(" + readMode + ", " + writeMode + ")"; } public boolean equals(Object o) { if (!(o instanceof MechMode)) return false; MechMode x = (MechMode) o; return eq(readMode, x.readMode) && eq(writeMode, x.writeMode); } public int hashCode() { int h = -866263200; h = boostHashCombine(h, _hashCode(readMode)); h = boostHashCombine(h, _hashCode(writeMode)); return h; } } public static class WeightedProduction { public String a, b, c; public double weight; public String simplificationHint, plausibilityFunction; public WeightedProduction() { } public WeightedProduction(String a, String b, String c, double weight) { this.weight = weight; this.c = c; this.b = b; this.a = a; } public WeightedProduction(String a, String c, double weight) { this.weight = weight; this.c = c; this.a = a; } public String toString() { return a + (b == null ? "" : " + " + b) + " = " + c + appendSquareBracketStuff(simplificationHint); } } public static final class IntRange { public int start, end; public IntRange() { } public IntRange(int start, int end) { this.end = end; this.start = start; } public boolean equals(Object o) { return stdEq2(this, o); } public int hashCode() { return stdHash2(this); } public int length() { return end - start; } public static String _fieldOrder = "start end"; public String toString() { return "[" + start + ";" + end + "]"; } } public static class Cache { public Object maker; public A value; public long loaded; public static boolean debug = false; public long changeCount; public Lock lock = lock(); public Cache() { } public Cache(Object maker) { this.maker = maker; } public A get() { if (hasLock(lock)) return value; Lock __1428 = lock; lock(__1428); try { if (loaded == 0) { value = make(); changeCount++; loaded = sysNow(); } return value; } finally { unlock(__1428); } } public void clear() { Lock __1429 = lock; lock(__1429); try { if (debug && loaded != 0) print("Clearing cache"); value = null; changeCount++; loaded = 0; } finally { unlock(__1429); } } public void clear(double seconds) { Lock __1430 = lock; lock(__1430); try { if (seconds != 0 && loaded != 0 && sysNow() >= loaded + seconds * 1000) clear(); } finally { unlock(__1430); } } public void set(A a) { Lock __1431 = lock; lock(__1431); try { value = a; ++changeCount; loaded = sysNow(); } finally { unlock(__1431); } } public A make() { return (A) callF(maker); } } public static B pairB(Pair p) { return p == null ? null : p.b; } public static boolean reflection_isForbiddenMethod(Method m) { return m.getDeclaringClass() == Object.class && eqOneOf(m.getName(), "finalize", "clone", "registerNatives"); } public static void multiMapPut(Map> map, A a, B b) { List l = map.get(a); if (l == null) map.put(a, l = new ArrayList()); l.add(b); } public static void multiMapPut(MultiMap mm, A key, B value) { if (mm != null && key != null && value != null) mm.put(key, value); } public static Set allInterfacesImplementedBy(Class c) { if (c == null) return null; HashSet set = new HashSet(); allInterfacesImplementedBy_find(c, set); return set; } public static void allInterfacesImplementedBy_find(Class c, Set set) { if (c.isInterface() && !set.add(c)) return; do { for (Class intf : c.getInterfaces()) allInterfacesImplementedBy_find(intf, set); } while ((c = c.getSuperclass()) != null); } public static boolean call_checkArgs(Method m, Object[] args, boolean debug) { Class[] types = m.getParameterTypes(); if (types.length != args.length) { if (debug) print("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) { Object arg = args[i]; if (!(arg == null ? !types[i].isPrimitive() : isInstanceX(types[i], arg))) { if (debug) print("Bad parameter " + i + ": " + arg + " vs " + types[i]); return false; } } return true; } public static Method findStaticMethod(Class c, String method, Object... args) { Class _c = c; while (c != null) { for (Method m : c.getDeclaredMethods()) { if (!m.getName().equals(method)) continue; if ((m.getModifiers() & Modifier.STATIC) == 0 || !findStaticMethod_checkArgs(m, args)) continue; return m; } c = c.getSuperclass(); } return null; } public static boolean findStaticMethod_checkArgs(Method m, Object[] args) { Class[] types = m.getParameterTypes(); if (types.length != args.length) return false; for (int i = 0; i < types.length; i++) if (!(args[i] == null || isInstanceX(types[i], args[i]))) return false; return true; } public static int boostHashCombine(int a, int b) { return a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2)); } public static int _hashCode(Object a) { return a == null ? 0 : a.hashCode(); } public static String indentx(String s) { return indentx(indent_default, s); } public static String indentx(int n, String s) { return dropSuffix(repeat(' ', n), indent(n, s)); } public static String indentx(String indent, String s) { return dropSuffix(indent, indent(indent, s)); } public static String curly(String s) { return optionalCurlyBrace(s); } public static String pnlToString(String prefix, Iterable l) { return hijackPrint(new Runnable() { public void run() { try { pnl(prefix, l); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pnl(prefix, l)"; } }); } public static String pnlToString(final Iterable l) { return hijackPrint(new Runnable() { public void run() { try { pnl(l); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pnl(l)"; } }); } public static String pnlToString(final A[] l) { return hijackPrint(new Runnable() { public void run() { try { pnl(l); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pnl(l)"; } }); } public static String pnlToString(final Map map) { return hijackPrint(new Runnable() { public void run() { try { pnl(map); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pnl(map)"; } }); } public static String pnlToString(MultiMap map) { return pnlToString(multiMapToMap(map)); } public static boolean hasLock(Lock lock) { return ((ReentrantLock) lock).isHeldByCurrentThread(); } public static String mL_raw(String name) { return mechList_raw(name); } public static boolean eqOrSame(Object a, Object b) { return a == null ? b == null : a == b || b != null && a.equals(b); } public static void clear(Collection c) { if (c != null) c.clear(); } public static Map putAll(Map a, Map b) { if (a != null && b != null) a.putAll(b); return a; } public static void put(Map map, A a, B b) { if (map != null) map.put(a, b); } public static void put(List l, int i, A a) { if (l != null && i >= 0 && i < l(l)) l.set(i, a); } public static Set keySet(Map map) { return map == null ? new HashSet() : map.keySet(); } public static Set keySet(Object map) { return keys((Map) map); } public static Set keySet(MultiSet ms) { return ms.keySet(); } public static Set keySet(MultiMap mm) { return mm.keySet(); } public static A reverseGet(List l, int idx) { if (l == null || idx < 0) return null; int n = l(l); return idx < n ? l.get(n - 1 - idx) : null; } public static Map cloneMap(Map map) { if (map == null) return new HashMap(); synchronized (map) { return map instanceof TreeMap ? new TreeMap((TreeMap) map) : map instanceof LinkedHashMap ? new LinkedHashMap(map) : new HashMap(map); } } public static Collection values(Map map) { return map == null ? emptyList() : map.values(); } public static Collection values(MultiMap mm) { return mm == null ? emptyList() : concatLists(values(mm.data)); } public static float score(Scored s) { return s == null ? 0 : s.score(); } public static boolean getServerTranspiled2_allowLocalFallback = true, getServerTranspiled2_localFallbackVerbose = true; public static Map getServerTranspiled2_tempCache; public static String getServerTranspiled2(String id) { IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return rl.getTranspiled(id); id = fsIOpt(id); String transpiled = mapGet(getServerTranspiled2_tempCache, id); if (transpiled != null) return transpiled; if (getServerTranspiled2_tempCache != null) print("CACHE FAIL on " + id); transpiled = loadCachedTranspilation(id); String md5 = null; if (machineIsOffline() || isOfflineMode() || isLocalSnippet(id)) return transpiled; if (transpiled != null) md5 = md5(transpiled); String transpiledSrc; try { transpiledSrc = getServerTranspiled(formatSnippetID(id), md5); } catch (Throwable e) { if (!getServerTranspiled2_allowLocalFallback) rethrow(e); printExceptionShort(e); if (getServerTranspiled2_localFallbackVerbose) print("Fallback to local code"); return transpiled; } if (eq(transpiledSrc, "SAME")) { if (!isTrue(loadPage_silent.get())) print("SAME"); return mapPut_returnValue(getServerTranspiled2_tempCache, id, transpiled); } return mapPut_returnValue(getServerTranspiled2_tempCache, id, transpiledSrc); } public static String fsIOpt(String s) { return formatSnippetIDOpt(s); } public static List findMainClass(List tok) { for (List c : reversedList(allClasses(tok))) { String name = getClassDeclarationName(c); if (eq(name, "main") || name.startsWith("x")) return c; } return findBlock("m {", tok); } public static boolean useDummyMainClasses() { return true; } public static boolean tok_classHasModifier(List classDecl, String modifier) { if (classDecl == null) return false; int i = classDecl.indexOf("class"); return subList(classDecl, 0, i).contains(modifier); } public static boolean javaCompileToJar_useRAMDisk = false; public static File javaCompileToJar_optionalRename(String src, File destJar, String progIDForRename) { return javaCompileToJar_optionalRename(src, "", destJar, progIDForRename); } public static synchronized File javaCompileToJar_optionalRename(String src, String dehlibs, File destJar, String progIDForRename) { return javaCompileToJar_optionalRename(src, dehlibs, destJar, progIDForRename, null); } public static synchronized File javaCompileToJar_optionalRename(String src, String dehlibs, File destJar, String progIDForRename, String progID) { String javaTarget = null; String dummyClass = "main"; if (progIDForRename != null) { dummyClass = dummyMainClassName(progIDForRename); src += "\nclass " + dummyClass + "{}"; } String md5 = md5(src); File jar = destJar; Class j = getJavaX(); if (javaTarget != null) setOpt(j, "javaTarget", javaTarget); File srcDir = tempDir(); String className = "main"; String fileName = dummyClass + ".java"; List tok = javaTok(src); String packageName = tok_packageName(tok); if (packageName != null) fileName = packageName.replace(".", "/") + "/" + tok_firstClassName(tok) + ".java"; File mainJava = new File(srcDir, fileName); saveTextFile(mainJava, src); File classesDir = javaCompileToJar_useRAMDisk ? tempDirPossiblyInRAMDisk() : tempDir(); try { List libraries = new ArrayList(); Matcher m = Pattern.compile("\\d+").matcher(dehlibs); while (m.find()) { String libID = m.group(); assertTrue(isSnippetID(libID)); libraries.add(loadLibraryOrSrcLib(libID)); } libraries.add(pathToJavaxJar()); String compilerOutput; try { compilerOutput = (String) call(j, "compileJava", srcDir, libraries, classesDir); } catch (Throwable e) { compilerOutput = (String) get(getJavaX(), "javaCompilerOutput"); compilerOutput = indentx("> ", cleanJavaCompilerOutput(compilerOutput)); if (!swic(e.getMessage(), "Java compiler returned errors.")) compilerOutput = appendWithNewLine(compilerOutput, str(e)); throw fail(compilerOutput, e); } compilerOutput = cleanJavaCompilerOutput("Annotation processing got disabled, since it requires a 1.6 compliant JVM"); if (nempty(compilerOutput)) { print("Compiler said: " + compilerOutput); } if (!anyFileWithExtensionInDir(classesDir, ".class")) throw fail("No classes generated"); saveTextFile(new File(classesDir, "main.java"), src); if (nempty(dehlibs)) saveTextFile(new File(classesDir, "libraries"), dehlibs); saveTextFile(new File(classesDir, "progID"), progID); dir2zip_recurse_verbose = false; int n = dir2zip_recurse(classesDir, jar); return jar; } finally { if (isInRAMDisk(classesDir)) deleteDirectory(classesDir); } } public static void touchFile(File file) { try { closeRandomAccessFile(newRandomAccessFile(mkdirsForFile(file), "rw")); } catch (Exception __e) { throw rethrow(__e); } } public static File actualUserDir() { return new File(actualUserHome()); } public static File actualUserDir(String path) { return new File(actualUserHome(), path); } public static int hashCodeFor(Object a) { return a == null ? 0 : a.hashCode(); } public static void change() { callOpt(getOptMC("mainConcepts"), "allChanged"); } public static String lower(String s) { return s == null ? null : s.toLowerCase(); } public static char lower(char c) { return Character.toLowerCase(c); } public static void moveElementFromCollectionToCollection(A a, Collection src, Collection target) { if (src != null) src.remove(a); if (target != null) target.add(a); } public static String[] dropFirst(int n, String[] a) { return drop(n, a); } public static String[] dropFirst(String[] a) { return drop(1, a); } public static Object[] dropFirst(Object[] a) { return drop(1, a); } public static List dropFirst(List l) { return dropFirst(1, l); } public static List dropFirst(int n, Iterable i) { return dropFirst(n, toList(i)); } public static List dropFirst(Iterable i) { return dropFirst(toList(i)); } public static List dropFirst(int n, List l) { return n <= 0 ? l : new ArrayList(l.subList(Math.min(n, l.size()), l.size())); } public static List dropFirst(List l, int n) { return dropFirst(n, l); } public static String dropFirst(int n, String s) { return substring(s, n); } public static String dropFirst(String s, int n) { return substring(s, n); } public static String dropFirst(String s) { return substring(s, 1); } public static IterableIterator iterateListConcurrently(List l) { return concurrentlyIterateList(l); } public static LinkedHashSet makeHull_optimized(MultiMap map, String e) { LinkedHashSet l = new LinkedHashSet(); makeHull_optimized_impl(map, e, l); return l; } public static void makeHull_optimized_impl(MultiMap map, String e, LinkedHashSet dest) { if (dest.contains(e)) return; dest.add(e); for (String f : map.get(e)) makeHull_optimized_impl(map, f, dest); } public static boolean ai_parser_activateStandardFunctions_flag = true; public static void ai_parser_activateStandardFunctions() { ai_parser_activateStandardFunctions_flag = true; loadFunctions_preferDiskCache(); } public static boolean ai_parser_activateStandardFunctions_get() { return ai_parser_activateStandardFunctions_flag; } public static void loadFunctions_preferDiskCache() { loadFunctions_preferDiskCache = true; } public static Object pcallAndMake(String functionName, Object... args) { try { return callAndMake(functionName, args); } catch (Throwable __e) { _handleException(__e); } return null; } public static List words_withAngleBrackets(String s) { return codeTokens(dropPunctuation(javaTokWithAngleBrackets(s))); } public static List ai_splitSplittables(List tok) { MultiMap splits = mechMultiMapCI(ai_language() + " splittables => split"); for (int i = 0; i < l(tok); i++) { List l = splits.get(tok.get(i)); if (nempty(l)) replaceSubList(tok, i, i + 1, words(first(l))); } return tok; } public static String ai_splitSplittables(String s) { return join(ai_splitSplittables_cnc(javaTok(s))); } public static TimedCache>>> ai_wordToCategories_cache = new TimedCache("ciMap", 10.0); public static Map> ai_wordToCategories() { Map>> languages = ai_wordToCategories_cache.get(); Map> map = languages.get(ai_language()); if (map == null) languages.put(ai_language(), map = ai_wordToCategories_make()); return map; } public static Map> ai_wordToCategories_make() { Map> catMap = ai_wordCategoriesWithElements(); final Map> map = ciMap(); for (String cat : keys(catMap)) for (String word : catMap.get(cat)) { Set set = map.get(word); if (set == null) map.put(word, set = new LinkedHashSet()); set.add(cat); } return map; } public static Set ai_wordToCategories(String word) { return ai_wordToCategories().get(word); } public static boolean isAngleBracketed(String s) { return s.startsWith("<") && s.endsWith(">"); } public static A pnl(String prefix, A l) { printNumberedLines(prefix, l); return l; } public static A pnl(A l) { printNumberedLines(l); return l; } public static A pnl(A map) { printNumberedLines(map); return map; } public static A[] pnl(A[] a) { printNumberedLines(a); return a; } public static String pnl(String s) { printNumberedLines(lines(s)); return s; } public static List words(String s) { return codeTokens(dropPunctuation(javaTok(s))); } public static List instancesOf(Iterable i, Class c) { return collectInstances(i, c); } public static List instancesOf(Class c, Iterable i) { return collectInstances(c, i); } public static List filter(Iterable c, Object pred) { if (pred instanceof F1) return filter(c, (F1) pred); List x = new ArrayList(); if (c != null) for (Object o : c) if (isTrue(callF(pred, o))) x.add(o); return x; } public static List filter(Object pred, Iterable c) { return filter(c, pred); } public static List filter(Iterable c, F1 pred) { List x = new ArrayList(); if (c != null) for (B o : c) if (pred.get(o)) x.add(o); return x; } public static List filter(F1 pred, Iterable c) { return filter(c, pred); } public static List filter(Iterable c, IF1 pred) { List x = new ArrayList(); if (c != null) for (B o : c) if (pred.get(o)) x.add(o); return x; } public static List filter(IF1 pred, Iterable c) { return filter(c, pred); } public static List filterByFields(Collection c, Object... data) { if (c == null) return null; List l = new ArrayList(); for (A x : c) if (checkFields(x, data)) l.add(x); return l; } public static A findByFields(Iterable c, Object... data) { if (c != null) for (A x : c) if (checkFields(x, data)) return x; return null; } public static List ai_buParser_parseWeightedProductions() { return ai_buParser_parseWeightedProductions(mL_raw(ai_language() + " bottom-up groupings")); } public static List ai_buParser_parseWeightedProductions(String text) { List l = new ArrayList(); for (String s : tlftj(text)) { s = trim(javaDropAllComments(s)); List meta = trimAll(tok_splitAtComma(trailingAngleBracketStuff(s))); s = dropTrailingAngleBracketStuff(s); List tok = javaTokWithAngleBracketsC(s); WeightedProduction p = null; double weight = 1.0; String f = null; for (String met : meta) if (startsWithDigit(met)) weight = parseDouble(met); else if (startsWith(met, "ai")) f = met; if (l(tok) == 5) p = new WeightedProduction(tok.get(0), tok.get(2), tok.get(4), weight); else if (l(tok) == 3) p = new WeightedProduction(tok.get(0), tok.get(2), weight); if (p != null) { p.plausibilityFunction = f; for (String met : meta) if (eqOneOf(met, "L", "R", "B")) { p.simplificationHint = met; } l.add(p); } } return l; } public static String curlyBraceIfMultipleTokens(String s) { return curlyBraceIfMoreThanOneToken(s); } public static LinkedHashSet litorderedset(A... items) { LinkedHashSet set = new LinkedHashSet(); for (A a : items) set.add(a); return set; } public static void fixACCInClassLoader(Object o) { try { AccessControlContext acc = vm_globalACC(); if (acc != null) replaceACCInClassLoader(o, acc); } catch (Throwable __e) { _handleException(__e); } } public static HashMap findClass_cache = new HashMap(); public static Class findClass(String name) { synchronized (findClass_cache) { if (findClass_cache.containsKey(name)) return findClass_cache.get(name); if (!isJavaIdentifier(name)) return null; Class c; try { c = Class.forName("main$" + name); } catch (ClassNotFoundException e) { c = null; } findClass_cache.put(name, c); return c; } } public static String systemHashCodeHex(Object o) { return intToHex(identityHashCode(o)); } public static UnsupportedOperationException unsupportedOperation() { throw new UnsupportedOperationException(); } public static LinkedHashMap cloneLinkedHashMap(Map map) { return map == null ? new LinkedHashMap() : new LinkedHashMap(map); } public static long sysTime() { return sysNow(); } public static Map mapValues(Object func, Map map) { Map m = similarEmptyMap(map); for (Object key : keys(map)) m.put(key, callF(func, map.get(key))); return m; } public static Map mapValues(Map map, IF1 f) { return mapValues(f, map); } public static Map mapValues(Map map, Object func) { return mapValues(func, map); } public static String appendSquareBracketStuff(String b) { return appendSquareBracketed(b); } public static String appendSquareBracketStuff(String a, String b) { return appendSquareBracketed(a, b); } public static boolean stdEq2(Object a, Object b) { if (a == null) return b == null; if (b == null) return false; if (a.getClass() != b.getClass()) return false; for (String field : allFields(a)) if (neq(getOpt(a, field), getOpt(b, field))) return false; return true; } public static int stdHash2(Object a) { if (a == null) return 0; return stdHash(a, toStringArray(allFields(a))); } public static int indent_default = 2; public static String indent(int indent) { return repeat(' ', indent); } public static String indent(int indent, String s) { return indent(repeat(' ', indent), s); } public static String indent(String indent, String s) { return indent + s.replace("\n", "\n" + indent); } public static String indent(String s) { return indent(indent_default, s); } public static List indent(String indent, List lines) { List l = new ArrayList(); if (lines != null) for (String s : lines) l.add(indent + s); return l; } public static String optionalCurlyBrace(String s) { return isCurlyBraced(s) ? s : curlyBrace(s); } public static String hijackPrint(Object r) { final StringBuilder buf = new StringBuilder(); Object old = interceptPrintInThisThread(new F1() { public Boolean get(String s) { buf.append(s); return false; } }); try { callF(r); return str(buf); } finally { interceptPrintInThisThread(old); } } public static Map> multiMapToMap(MultiMap m) { return m == null ? null : m.data; } public static String loadCachedTranspilation(String id) { try { return loadTextFilePossiblyGZipped(getCachedTranspilationFile(id)); } catch (Throwable __e) { return null; } } public static boolean machineIsOffline() { return isFalse(callF(vmGeneralMap_get("areWeOnline"))); } public static boolean isOfflineMode() { return eq("1", trim(loadProgramTextFile("#1005806", "offline-mode"))); } public static boolean isLocalSnippet(String snippetID) { return isLocalSnippetID(snippetID); } public static boolean isLocalSnippet(long snippetID) { return isLocalSnippetID(snippetID); } public static String getServerTranspiled(String snippetID) { return getServerTranspiled(snippetID, null); } public static boolean getServerTranspiled_printStackTrace = false; public static String getServerTranspiled(String snippetID, String expectedMD5) { try { if (getServerTranspiled_printStackTrace) printStackTrace(); long id = parseSnippetID(snippetID); String text = loadPage_utf8(tb_mainServer() + "/tb-int/get-transpiled.php?raw=1&withlibs=1&id=" + id + "&utf8=1" + (l(expectedMD5) > 1 ? "&md5=" + urlencode(expectedMD5) : "") + standardCredentials()); if (nempty(text) && neq(text, "SAME")) saveTranspiledCode(snippetID, text); return text; } catch (Exception __e) { throw rethrow(__e); } } public static void printExceptionShort(Throwable e) { print(exceptionToStringShort(e)); } public static B mapPut_returnValue(Map map, A key, B value) { mapPut(map, key, value); return value; } public static HashSet allClasses_keywords = lithashset("class", "interface", "enum", "sclass", "sinterface", "record", "srecord", "asclass"); public static List> allClasses(List tok) { List> l = new ArrayList(); int n = tok.size(); HashSet _allClasses_keywords = allClasses_keywords; for (int i = 1; i < n; i += 2) { String t = tok.get(i); if ("{".equals(t)) i = findEndOfBlock(tok, i) - 1; else if (_allClasses_keywords.contains(t) && (tok_isJavaxMetaCommandLeftOf(tok, i) || !eqGetOneOf(tok, i - 2, ".", "include"))) { int j = i; while (j < n && !tok.get(j).equals("{")) j += 2; j = findEndOfBlock(tok, j) + 1; i = leftScanModifiers(tok, i); l.add(tok.subList(i - 1, Math.min(n, j))); i = j - 2; } } return l; } public static List> allClasses(String text) { return allClasses(javaTok(text)); } public static String getClassDeclarationName(List tok) { if (tok != null) for (int i = 1; i + 2 < tok.size(); i += 2) if (allClasses_keywords.contains(tok.get(i)) && isIdentifier(tok.get(i + 2))) { if (eqGet(tok, i + 2, "noeq")) i += 2; return tok.get(i + 2); } return null; } public static List findBlock(String pat, List tok) { List tokpat = javaTok(pat); int i = findCodeTokens(tok, toStringArray(codeTokensOnly(tokpat))); if (i < 0) return null; int bracketIdx = i + tokpat.size() - 3; assertEquals("{", tok.get(bracketIdx)); int endIdx = findEndOfBlock(tok, bracketIdx); return subList(tok, i - 1, endIdx + 1); } public static String dummyMainClassName(String progID) { return "m" + psI(progID); } public static File tempDir() { return makeTempDir(); } public static String tok_packageName(List tok) { int i = jfind(tok, "package"); if (i < 0) return ""; i += 2; int j = jfind(tok, i, ";"); if (j < 0) return ""; return join(codeTokensOnly(subList(tok, i - 1, j))); } public static String tok_firstClassName(List tok) { int i = jfind(tok, "class "); return i < 0 ? null : tok.get(i + 2); } public static File tempDirPossiblyInRAMDisk() { File f = linux_fileInRamDisk(aGlobalID()); if (f != null) { f.mkdirs(); return f; } return makeTempDir(); } public static boolean loadLibraryOrSrcLib_srcLibsEnabled = true; public static ThreadLocal> loadLibraryOrSrcLib_tempCache = new ThreadLocal(); public static File loadLibraryOrSrcLib(String snippetID) { try { long id = parseSnippetID(snippetID); if (loadLibraryOrSrcLib_tempCache.get() != null) { File f = loadLibraryOrSrcLib_tempCache.get().get(id); if (f != null) return f; } boolean srcLib = loadLibraryOrSrcLib_srcLibsEnabled && isMarkedAsSrcLib(snippetID); if (srcLib) { File f = pairA(hotwire_compile(snippetID)); mapPut(loadLibraryOrSrcLib_tempCache.get(), id, f); } File f = DiskSnippetCache_getLibrary(id); if (fileSize(f) != 0) return f; try { return loadDataSnippetToFile(snippetID); } catch (Throwable e) { if (loadLibraryOrSrcLib_srcLibsEnabled && nempty(loadSnippet(snippetID))) { markAsSrcLib(snippetID); return pairA(hotwire_compile(snippetID)); } throw rethrow(e); } } catch (Exception __e) { throw rethrow(__e); } } public static File pathToJavaxJar() { try { IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return rl.pathToJavaXJar(); int x = latestInstalledJavaX(); File xfile = new File(userHome(), ".javax/x" + Math.max(x, 30) + ".jar"); if (!xfile.isFile()) { print("Saving " + f2s(xfile)); String url = x30JarServerURL(); byte[] data = loadBinaryPage(url); if (data.length < 1000000) throw fail("Could not load " + url); saveBinaryFile(xfile.getPath(), data); } return xfile; } catch (Exception __e) { throw rethrow(__e); } } public static String cleanJavaCompilerOutput(String s) { return dropPrefixTrim("Annotation processing got disabled, since it requires a 1.6 compliant JVM", s); } public static boolean swic(String a, String b) { return startsWithIgnoreCase(a, b); } public static boolean swic(String a, String b, Matches m) { if (!swic(a, b)) return false; m.m = new String[] { substring(a, l(b)) }; return true; } public static String appendWithNewLine(String a, String b) { if (empty(b)) return a; if (empty(a)) return b; return addSuffix(a, "\n") + b; } public static boolean anyFileWithExtensionInDir(File dir, String ext) { return nempty(filesWithExtension(ext, findAllFiles_noDirs(dir))); } public static boolean dir2zip_recurse_verbose = false; public static int dir2zip_recurse(File inDir, File zip) { return dir2zip_recurse(inDir, zip, ""); } public static int dir2zip_recurse(File inDir, File zip, String outPrefix) { try { mkdirsForFile(zip); FileOutputStream fout = newFileOutputStream(zip); ZipOutputStream outZip = new ZipOutputStream(fout); try { return dir2zip_recurse(inDir, outZip, outPrefix, 0); } finally { outZip.close(); } } catch (Exception __e) { throw rethrow(__e); } } public static int dir2zip_recurse(File inDir, ZipOutputStream outZip) { return dir2zip_recurse(inDir, outZip, "", 0); } public static int dir2zip_recurse(File inDir, ZipOutputStream outZip, String outPrefix, int level) { try { if (++level >= 20) throw fail("woot? 20 levels in zip?"); List files = new ArrayList(); for (File f : listFiles(inDir)) files.add(f); int n = 0; sortFilesByName(files); for (File f : files) { if (f.isDirectory()) { print("dir2zip_recurse: Scanning " + f.getAbsolutePath()); n += dir2zip_recurse(f, outZip, outPrefix + f.getName() + "/", level); } else { if (dir2zip_recurse_verbose) print("Copying " + f.getName()); outZip.putNextEntry(new ZipEntry(outPrefix + f.getName())); InputStream fin = new FileInputStream(f); copyStream(fin, outZip); fin.close(); ++n; } } return n; } catch (Exception __e) { throw rethrow(__e); } } public static boolean isInRAMDisk(File f) { return startsWith(f2s(f), "/dev/shm"); } public static void deleteDirectory(File dir) { deleteDirectory(dir, false, false); } public static void deleteDirectory(File dir, boolean verbose, boolean testRun) { deleteAllFilesInDirectory(dir, verbose, testRun); if (verbose) print((testRun ? "Would delete " : "Deleting ") + dir.getAbsolutePath()); if (!testRun) dir.delete(); } public static void closeRandomAccessFile(RandomAccessFile f) { if (f != null) try { f.close(); callJavaX("dropIO", f); } catch (Throwable e) { printStackTrace(e); } } public static RandomAccessFile newRandomAccessFile(File path, String mode) throws IOException { boolean forWrite = mode.indexOf('w') >= 0; if (forWrite) mkdirsForFile(path); RandomAccessFile f = new RandomAccessFile(path, mode); callJavaX("registerIO", f, path, forWrite); return f; } public static Object getOptMC(String field) { return getOpt(mc(), field); } public static String[] drop(int n, String[] a) { n = Math.min(n, a.length); String[] b = new String[a.length - n]; System.arraycopy(a, n, b, 0, b.length); return b; } public static Object[] drop(int n, Object[] a) { n = Math.min(n, a.length); Object[] b = new Object[a.length - n]; System.arraycopy(a, n, b, 0, b.length); return b; } public static ArrayList toList(A[] a) { return asList(a); } public static ArrayList toList(int[] a) { return asList(a); } public static ArrayList toList(Set s) { return asList(s); } public static ArrayList toList(Iterable s) { return asList(s); } public static IterableIterator concurrentlyIterateList(final List l) { return iteratorFromFunction_withEndMarker_f0(new F0() { public int i; public A get() { int _i = i++; synchronized (l) { if (_i < l(l)) { A a = l.get(_i); if (a == iteratorFromFunction_endMarker) throw fail("no"); return a; } return (A) iteratorFromFunction_endMarker; } } }); } public static List javaTokWithAngleBrackets(String s) { return tok_combineAngleBrackets_keep(javaTok(s)); } public static Map>> mechMultiMapCI_cache = synchroMap(); public static MultiMap mechMultiMapCI(String listName) { Pair> p = mechMultiMapCI_cache.get(listName); String text = mechList_opt_raw(listName); if (p != null && eq(p.a, text)) return p.b; MultiMap map = parseDoubleArrowMultiMapCI(tlft(text)); p = pair(text, map); mechMultiMapCI_cache.put(listName, p); return p.b; } public static String ai_language() { return or2(ai_setLanguage_value.get(), "English"); } public static List ai_splitSplittables_cnc(List tok) { MultiMap splits = mechMultiMapCI(ai_language() + " splittables => split"); for (int i = 1; i < l(tok); i += 2) { List l = splits.get(tok.get(i)); if (nempty(l)) replaceSubList(tok, i, i + 1, ll(first(l))); } return tok; } public static TimedCache>>> ai_wordCategoriesWithElements_cache = new TimedCache("ciMap", 10.0); public static Map> ai_wordCategoriesWithElements() { Map>> languages = ai_wordCategoriesWithElements_cache.get(); Map> map = languages.get(ai_language()); if (map == null) languages.put(ai_language(), map = ai_wordCategoriesWithElements_make()); return map; } public static Map> ai_wordCategoriesWithElements_make() { final Map> wordCategories = ciMap(); for (String cat : mL("All " + ai_language() + " word categories")) putIfValueNempty(wordCategories, cat, combinedMechSetsCI(ai_mechListNamesForAngleBracketCategory(cat))); return wordCategories; } public static void printNumberedLines(Map map) { printNumberedLines(mapToLines(map)); } public static A printNumberedLines(A l) { int i = 0; if (l != null) for (Object a : l) print((++i) + ". " + str(a)); return l; } public static A printNumberedLines(String prefix, A l) { int i = 0; if (l != null) for (Object a : l) print(prefix + (++i) + ". " + str(a)); return l; } public static void printNumberedLines(Object[] l) { printNumberedLines(asList(l)); } public static void printNumberedLines(Object o) { printNumberedLines(lines(str(o))); } public static List collectInstances(Iterable i, Class c) { List l = new ArrayList(); if (i == null) return l; c = primitiveToBoxedTypeOpt(c); for (Object o : i) if (isInstance(c, o)) l.add(o); return l; } public static List collectInstances(Class c, Iterable i) { return collectInstances(i, c); } public static boolean checkFields(Object x, Object... data) { for (int i = 0; i < l(data); i += 2) if (neq(getOpt(x, (String) data[i]), data[i + 1])) return false; return true; } public static List tlftj(String text) { return toLinesFullTrim_java(text); } public static List trimAll(Collection l) { List l2 = new ArrayList(); if (l != null) for (String s : l) l2.add(trim(s)); return l2; } public static String trailingAngleBracketStuff(String s) { if (!endsWith(s, ']')) return null; return substring(s, smartLastIndexOf(s, '[') + 1, l(s) - 1); } public static String dropTrailingAngleBracketStuff(String s) { if (!endsWith(s, ']')) return s; return trimSubstring(s, 0, lastIndexOf(s, '[')); } public static List javaTokWithAngleBracketsC(String s) { return codeTokens(javaTokWithAngleBrackets(s)); } public static boolean startsWithDigit(String s) { return nempty(s) && isDigit(s.charAt(0)); } public static String curlyBraceIfMoreThanOneToken(String s) { return numberOfJavaTokens(s) > 1 ? curlyBrace(s) : s; } public static AccessControlContext vm_globalACC() { return (AccessControlContext) vm_generalMap_get("Global ACC"); } public static void replaceACCInClassLoader(Object o, AccessControlContext newACC) { ClassLoader cl = getClassLoaderOrSame(o); if (cl instanceof URLClassLoader) { try { setOpt(cl, "acc", newACC); try { setOpt((Object) getOpt(cl, "ucp"), "acc", newACC); } catch (Throwable e) { printShortException("replaceACCInClassLoader: ", e); if (java10OrHigher()) if (addDefaultVMOption("--add-opens java.base/jdk.internal.loader=ALL-UNNAMED")) print("Please restart the OS"); } } catch (Throwable __e) { _handleException(__e); } } } public static String intToHex(int i) { return bytesToHex(intToBytes(i)); } public static String appendSquareBracketed(String b) { return empty(b) ? "" : "" + " [" + b + "]"; } public static String appendSquareBracketed(String a, String b) { return empty(b) ? a : a + appendSquareBracketed(b); } public static Set allFields(Object o) { TreeSet fields = new TreeSet(); Class _c = _getClass(o); do { for (Field f : _c.getDeclaredFields()) fields.add(f.getName()); _c = _c.getSuperclass(); } while (_c != null); return fields; } public static int stdHash(Object a, String... fields) { if (a == null) return 0; int hash = getClassName(a).hashCode(); for (String field : fields) hash = boostHashCombine(hash, hashCode(getOpt(a, field))); return hash; } public static String[] toStringArray(Collection c) { String[] a = new String[l(c)]; Iterator it = c.iterator(); for (int i = 0; i < l(a); i++) a[i] = it.next(); return a; } public static String[] toStringArray(Object o) { if (o instanceof String[]) return (String[]) o; else if (o instanceof Collection) return toStringArray((Collection) o); else throw fail("Not a collection or array: " + getClassName(o)); } public static ThreadLocal ai_setLanguage_value = new ThreadLocal(); public static void ai_setLanguage(String language) { ai_setLanguage_value.set(language); } public static void _onLoad_ai_setLanguage() { _threadInfo_addMakerAndRetriever(new VF1() { public void get(Map map) { try { mapPut(map, "ai_setLanguage_value", ai_setLanguage_value.get()); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "mapPut(map, 'ai_setLanguage_value, ai_setLanguage_value!)"; } }, new VF1() { public void get(Map map) { try { ai_setLanguage((String) map.get("ai_setLanguage_value")); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ai_setLanguage((S) map.get('ai_setLanguage_value))"; } }); } public static String curlyBrace(String s) { return "{" + s + "}"; } public static String loadTextFilePossiblyGZipped(String fileName) { return loadTextFilePossiblyGZipped(fileName, null); } public static String loadTextFilePossiblyGZipped(String fileName, String defaultContents) { File gz = new File(fileName + ".gz"); return gz.exists() ? loadGZTextFile(gz) : loadTextFile(fileName, defaultContents); } public static String loadTextFilePossiblyGZipped(File fileName) { return loadTextFilePossiblyGZipped(fileName, null); } public static String loadTextFilePossiblyGZipped(File fileName, String defaultContents) { return loadTextFilePossiblyGZipped(fileName.getPath(), defaultContents); } public static File getCachedTranspilationFile(String id) { return newFile(getCodeProgramDir(id), "Transpilation"); } public static Object vmGeneralMap_get(Object key) { return vm_generalMap_get(key); } public static String loadProgramTextFile(String name) { return loadTextFile(getProgramFile(name)); } public static String loadProgramTextFile(String progID, String name) { return loadTextFile(getProgramFile(progID, name)); } public static String loadProgramTextFile(String progID, String name, String defaultText) { return loadTextFile(getProgramFile(progID, name), defaultText); } public static boolean isLocalSnippetID(String snippetID) { return isSnippetID(snippetID) && isLocalSnippetID(psI(snippetID)); } public static boolean isLocalSnippetID(long snippetID) { return snippetID >= 1000 && snippetID <= 9999; } public static String standardCredentials() { String user = standardCredentialsUser(); String pass = standardCredentialsPass(); if (nempty(user) && nempty(pass)) return "&_user=" + urlencode(user) + "&_pass=" + urlencode(pass); return ""; } public static void saveTranspiledCode(String progID, String code) { File dir = getCodeProgramDir(progID); new File(dir, "Transpilation").delete(); saveGZTextFile(new File(dir, "Transpilation.gz"), code); } public static void mapPut(Map map, A key, B value) { if (map != null && key != null && value != null) map.put(key, value); } public static int findEndOfBlock(List cnc, int i) { int j = i + 2, level = 1, n = cnc.size(); while (j < n) { String t = cnc.get(j); if ("{".equals(t)) ++level; else if ("}".equals(t)) --level; if (level == 0) return j + 1; j += 2; } return n; } public static boolean tok_isJavaxMetaCommandLeftOf(List tok, int i) { int j = i - 2 * 4 - 1; if (j >= 0 && jfind(subList(tok, j, i), "set flag .") == 1) return true; return false; } public static int leftScanModifiers(List tok, int i) { List mod = getJavaModifiers(); while (i > 1 && mod.contains(tok.get(i - 2))) i -= 2; return i; } public static boolean eqGet(List l, int i, Object o) { return eq(get(l, i), o); } public static boolean eqGet(Map map, A key, Object o) { return eq(mapGet(map, key), o); } public static A assertEquals(Object x, A y) { return assertEquals(null, x, y); } public 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; } public static File makeTempDir() { return (File) call(getJavaX(), "TempDirMaker_make"); } public static File linux_fileInRamDisk(String name) { if (!isLinux()) return null; File dir = newFile("/dev/shm"); if (dir.isDirectory()) return newFile(dir, name); return null; } public static String aGlobalID() { return randomID(globalIDLength()); } public static boolean isMarkedAsSrcLib(String snippetID) { if (snippetID == null) return false; IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return isJavaxCompilableSnippetType(rl.getSnippetType(snippetID)); return fileExists(javaxCodeDir("srclibs/" + psI(snippetID))); } public static A pairA(Pair p) { return p == null ? null : p.a; } public static Object hotwire_onCompile; public static boolean hotwire_serially = false; public static Lock hotwire_overInternalBot_lock = lock(); public static boolean hotwire_compileOnServer = false; public static Class hotwire_overInternalBot(String progID) { return hotwire_overInternalBot(progID, "main"); } public static Class hotwire_overInternalBot(String progID, String mainClass) { try { Pair p; try { p = hotwire_compile(progID); } catch (Throwable e) { throw rethrow("Error hotwiring " + progID, e); } File jar = p.a; assertTrue(jar.getAbsolutePath(), jar.isFile()); List files = hotwire_collectJars(jar); JavaXClassLoader classLoader = hotwire_makeClassLoader(files); classLoader.progID = progID; return hotwire_finish(classLoader, progID, p.b, mainClass); } catch (Exception __e) { throw rethrow(__e); } } public static Pair hotwire_compile(String progID) { Pair p = hotwire_compileOnServer && !isLocalSnippetID(progID) ? compileSnippetThroughServer(progID) : CompilerBot.compileSnippet2(progID); Lock __1691 = hotwire_serially ? hotwire_overInternalBot_lock : null; lock(__1691); try { callF(hotwire_onCompile, p); return p; } finally { unlock(__1691); } } public static void markAsSrcLib(String snippetID) { saveTextFile(javaxCodeDir("srclibs/" + psI(snippetID)), ""); } public static int latestInstalledJavaX() { File[] files = new File(userHome(), ".javax").listFiles(); int v = 0; if (files != null) for (File f : files) { Matcher m = Pattern.compile("x(\\d\\d\\d?)\\.jar").matcher(f.getName()); if (m.matches()) v = Math.max(v, Integer.parseInt(m.group(1))); } return v; } public static String x30JarServerURL() { return "http://tinybrain.de/x30.jar"; } public static String dropPrefixTrim(String prefix, String s) { return trim(dropPrefix(prefix, s)); } public static boolean startsWithIgnoreCase(String a, String b) { return regionMatchesIC(a, 0, b, 0, b.length()); } public static String addSuffix(String s, String suffix) { return s == null || s.endsWith(suffix) ? s : s + suffix; } public static List filesWithExtension(String ext, List files) { return filesEndingWith(files, addPrefixIfNotEmpty2(".", ext)); } public static List findAllFiles_noDirs(List dirs) { return findAllFiles_noDirs(asObjectArray(dirs)); } public static List findAllFiles_noDirs(Object... dirs) { List l = new ArrayList(); for (Object dir : dirs) { if (dir instanceof String && ((String) dir).endsWith("/.")) { for (File f : listFiles(dropSuffix("/.", (String) dir))) if (f.isFile()) l.add(f); } else findAllFiles_noDirs_impl(toFile(dir), l); } return l; } public static void findAllFiles_noDirs_impl(File dir, List l) { for (File f : listFiles(dir)) { if (f.isDirectory()) findAllFiles_noDirs_impl(f, l); else l.add(f); } } public static File[] listFiles(File dir) { File[] files = dir.listFiles(); return files == null ? new File[0] : files; } public static File[] listFiles(String dir) { return listFiles(new File(dir)); } public static List sortFilesByName(List l) { sort(l, new Comparator() { public int compare(File a, File b) { return stdcompare(a.getName(), b.getName()); } }); return l; } public static int deleteAllFilesInDirectory_minPathLength = 10; public static void deleteAllFilesInDirectory(File dir) { deleteAllFilesInDirectory(dir, false, false); } public static void deleteAllFilesInDirectory(File dir, boolean verbose, boolean testRun) { dir = getCanonicalFile(dir); assertTrue(f2s(dir), l(f2s(dir)) >= deleteAllFilesInDirectory_minPathLength); File[] files = dir.listFiles(); if (files == null) return; for (File f : files) { if (!isSymLink(f) && f.isDirectory()) deleteDirectory(f, verbose, testRun); else { if (verbose) print((testRun ? "Would delete " : "Deleting ") + f.getAbsolutePath()); if (!testRun) f.delete(); } } } public static Object callJavaX(String method, Object... args) { return callOpt(getJavaX(), method, args); } public static IterableIterator iteratorFromFunction_withEndMarker_f0(final F0 f) { class IFF2 extends IterableIterator { public A a; public boolean have, done; public boolean hasNext() { getNext(); return !done; } public A next() { getNext(); if (done) throw fail(); A _a = a; a = null; have = false; return _a; } public void getNext() { if (done || have) return; a = f.get(); have = true; if (a == iteratorFromFunction_endMarker) done = true; } } ; return new IFF2(); } public static List tok_combineAngleBrackets_keep(List tok) { List l = new ArrayList(); for (int i = 0; i < l(tok); i++) { String t = tok.get(i); if (odd(i) && eq(t, "<")) { int j = findEndOfAngleBracketPart(tok, i); l.add(joinSubList(tok, i, j)); i = j - 1; } else l.add(t); } return l; } public static String mechList_opt_raw(String name) { try { Lock __1722 = mechList_raw_lock; lock(__1722); try { if (mechList_goToCreator() || eq(mechMode().readMode, "creator")) return (String) call(creator(), "mechList_opt_raw", name); String src = mechList_raw_cache.get(name); if (src != null) return src; src = mechList_opt_raw_fresh(name); mechList_raw_cache.put(name, unnull(src)); return src; } finally { unlock(__1722); } } catch (Exception __e) { throw rethrow(__e); } } public static void _onLoad_mechList_opt_raw() { _registerAutoCloseable(vmBus_onMessage("mechList_clearCache", new VF1() { public void get(String listName) { try { if (!isTrue(mechList_clearCache_inFunction.get())) mechList_clearCache_noSend(listName); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!isTrue(mechList_clearCache_inFunction!))\r\n mechList_clearCache_noSe..."; } })); } public static MultiMap parseDoubleArrowMultiMapCI(List l) { return parseDoubleArrowMultiMap((MultiMap) ciMultiMap(), l); } public static List mL(String name) { return mechList_opt(name); } public static void putIfValueNempty(Map map, A key, B value) { if (map != null && nempty(value)) map.put(key, value); } public static Set combinedMechSetsCI(String... listNames) { return combinedMechSetsCI(asList(listNames)); } public static Set combinedMechSetsCI(Collection listNames) { if (empty(listNames)) return ciSet(); if (l(listNames) == 1) return mechSetCI(first(listNames)); Set set = ciSet(); for (String listName : listNames) set.addAll(mL(listName)); return set; } public static TimedCache>> ai_mechListNamesForAngleBracketCategory_cache = new TimedCache(5.0); public static Set ai_mechListNamesForAngleBracketCategory(String s) { if (!isAngleBracketed(s)) return null; Map> map = ai_mechListNamesForAngleBracketCategory_cache.get("newHashMap"); s = dropTrailingNumberFromAngleBracketCategory(s); if (map.containsKey(s)) return map.get(s); Set set = litorderedset(mechLanguagePrefix() + firstToUpper(plural_mech(replace(deAngleBracket(s), "_", " ")))); set.addAll(mechMultiMapCI(mechLanguagePrefix() + "Word category => List").get(s)); map.put(s, set); return set; } public static List mapToLines(Map map) { List l = new ArrayList(); for (Object key : keys(map)) l.add(str(key) + " = " + str(map.get(key))); return l; } public static String mapToLines(Map map, Object f) { return lines(map(map, f)); } public static String mapToLines(Object f, Map map) { return lines(map(map, f)); } public static String mapToLines(Object f, Iterable l) { return lines(map(f, l)); } public static String mapToLines(Iterable l, IF1 f) { return mapToLines(f, l); } public static Class primitiveToBoxedTypeOpt(Class type) { return or(primitiveToBoxedType(type), type); } public static List toLinesFullTrim_java(String text) { return tlft(joinLines(map("javaDropComments", tlft(text)))); } public static int smartLastIndexOf(String s, char c) { if (s == null) return 0; int i = s.lastIndexOf(c); return i >= 0 ? i : l(s); } public static String trimSubstring(String s, int x) { return trim(substring(s, x)); } public static String trimSubstring(String s, int x, int y) { return trim(substring(s, x, y)); } public static String trimSubstring(String s, IntRange r) { return trim(substring(s, r)); } public static int lastIndexOf(String a, String b) { return a == null || b == null ? -1 : a.lastIndexOf(b); } public static int lastIndexOf(String a, char b) { return a == null ? -1 : a.lastIndexOf(b); } public static boolean isDigit(char c) { return Character.isDigit(c); } public static int numberOfJavaTokens(String s) { return l(javaTokC(s)); } public static ClassLoader getClassLoaderOrSame(Object o) { return o instanceof ClassLoader ? (ClassLoader) o : getClassLoader(o); } public static void printShortException(Throwable e) { print(exceptionToStringShort(e)); } public static void printShortException(String s, Throwable e) { print(s, exceptionToStringShort(e)); } public static boolean java10OrHigher() { return parseFirstInt(javaVersion()) >= 10; } public static boolean addDefaultVMOption(String option) { String s = defaultVMArgs(), old = s; if (!s.contains(option)) s = trim(s + " " + option); if (eq(old, s)) return false; { setDefaultVMArgs(s); return true; } } public static byte[] intToBytes(int i) { return new byte[] { (byte) (i >>> 24), (byte) (i >>> 16), (byte) (i >>> 8), (byte) i }; } public static int hashCode(Object a) { return a == null ? 0 : a.hashCode(); } public static Object iteratorFromFunction_endMarker = new Object(); public static IterableIterator iteratorFromFunction_withEndMarker(final Object f) { class IFF extends IterableIterator { public A a; public boolean done = false; public boolean hasNext() { getNext(); return !done; } public A next() { getNext(); if (done) throw fail(); A _a = a; a = null; return _a; } public void getNext() { if (done || a != null) return; a = (A) callF(f); if (a == iteratorFromFunction_endMarker) done = true; } } ; return new IFF(); } public static IterableIterator iteratorFromFunction_withEndMarker(final F0 f) { return iteratorFromFunction_withEndMarker_f0(f); } public static ThreadLocal mechList_clearCache_inFunction = new ThreadLocal(); public static void mechList_clearCache(String listName) { { AutoCloseable __1768 = tempSetTL(mechList_clearCache_inFunction, true); try { print("Sending mech clear: " + listName); vmBus_send("mechList_clearCache", listName); } finally { _close(__1768); } } mechList_clearCache_noSend(listName); } public static void mechList_clearCache_noSend(String listName) { for (String field : mcFields()) if (startsWithEndsWith(field, "mech", "_cache")) { Object o = getOptMC(field); if (o instanceof Map) removeFromMapCI_sync((Map) o, listName); } } public static HashMap newHashMap() { return new HashMap(); } public static String loadGZTextFile(File file) { try { if (!file.isFile()) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream fis = new FileInputStream(file); GZIPInputStream gis = newGZIPInputStream(fis); try { byte[] buffer = new byte[1024]; int len; while ((len = gis.read(buffer)) != -1) { baos.write(buffer, 0, len); } } finally { fis.close(); } baos.close(); return fromUtf8(baos.toByteArray()); } catch (Exception __e) { throw rethrow(__e); } } public static File getCodeProgramDir() { return getCodeProgramDir(getProgramID()); } public static File getCodeProgramDir(String snippetID) { return new File(javaxCodeDir(), formatSnippetID(snippetID)); } public static File getCodeProgramDir(long snippetID) { return getCodeProgramDir(formatSnippetID(snippetID)); } public static String standardCredentialsUser() { return trim(loadTextFile(oneOfTheFiles(javaxSecretDir("tinybrain-username"), userDir(".tinybrain/username")))); } public static String standardCredentialsPass() { return trim(loadTextFile(oneOfTheFiles(javaxSecretDir("tinybrain-userpass"), userDir(".tinybrain/userpass")))); } public static void saveGZTextFile(File file, String contents) { try { File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = file.getPath() + "_temp"; File tempFile = new File(tempFileName); if (contents != null) { if (tempFile.exists()) try { String saveName = tempFileName + ".saved." + now(); copyFile(tempFile, new File(saveName)); } catch (Throwable e) { printStackTrace(e); } FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath()); GZIPOutputStream gos = new GZIPOutputStream(fileOutputStream); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(gos, "UTF-8"); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); gos.close(); fileOutputStream.close(); } if (file.exists() && !file.delete()) throw new IOException("Can't delete " + file.getPath()); if (contents != null) if (!tempFile.renameTo(file)) throw new IOException("Can't rename " + tempFile + " to " + file); } catch (Exception __e) { throw rethrow(__e); } } public static List getJavaModifiers_list = litlist("static", "abstract", "public", "private", "protected", "final", "native", "volatile", "synchronized", "transient"); public static List getJavaModifiers() { return getJavaModifiers_list; } public static ThreadLocal assertVerbose_value = new ThreadLocal(); public static void assertVerbose(boolean b) { assertVerbose_value.set(b); } public static boolean assertVerbose() { return isTrue(assertVerbose_value.get()); } public static A assertEqualsVerbose(Object x, A y) { assertEqualsVerbose((String) null, x, y); return y; } public static A assertEqualsVerbose(String msg, Object x, A y) { if (!eq(x, y)) { throw fail((msg != null ? msg + ": " : "") + (y) + " != " + (x)); } else print("OK: " + (x)); return y; } public static Cache isLinux_cache = new Cache("isLinux_load"); public static boolean isLinux() { return isLinux_cache.get(); } public static Boolean isLinux_load() { return !isWindows() && !isMac() && !isAndroid(); } public static int randomID_defaultLength = 12; public static String randomID(int length) { return makeRandomID(length); } public static String randomID(Random r, int length) { return makeRandomID(r, length); } public static String randomID() { return randomID(randomID_defaultLength); } public static String randomID(Random r) { return randomID(r, randomID_defaultLength); } public static int globalIDLength() { return 16; } public static boolean isJavaxCompilableSnippetType(int type) { return isJavaxCompilableSnippetTypeExceptInclude(type) || type == javaxIncludeSnippetType(); } public static List hotwire_collectJars(File jar) { List libIDs = hotwire_libraryIDsFromJar_deleteJarOnFail(jar); List files = ll(jar); for (String libID : libIDs) files.add(loadLibraryOrSrcLib(libID)); return files; } public static Pair compileSnippetThroughServer(String progID) { String transpiledSrc = getServerTranspiled2(progID); String md5 = md5(transpiledSrc + "\n" + progID); File jar = CompilerBot.getJarFile(md5); if (jar == null || jar.length() <= 22) { byte[] jarData = null; boolean dontLoad = false; IResourceLoader rl = vm_getResourceLoader(); if (rl != null) { dontLoad = true; File jar2 = rl.getSnippetJar(progID, transpiledSrc); if (jar2 != null) return pair(jar2, transpiledSrc); } if (!dontLoad) { try { jarData = loadBinaryPage("http://www.botcompany.de/jar/" + psI(progID) + "?md5=" + md5(transpiledSrc)); } catch (Throwable __e) { _handleException(__e); } } if (!isJAR(jarData)) { if (jarData != null) { print(bytesToHex(takeFirstOfByteArray(8, jarData))); print("fallback to CompilerBot: " + fromUtf8(takeFirstOfByteArray(80, jarData))); } return CompilerBot.compileSnippet2(progID); } saveBinaryFile(jar, jarData); } return pair(jar, transpiledSrc); } public static String dropPrefix(String prefix, String s) { return s == null ? null : s.startsWith(prefix) ? s.substring(l(prefix)) : s; } public static List filesEndingWith(File dir, String suffix) { return listFilesWithSuffix(dir, suffix); } public static List filesEndingWith(List l, String suffix) { List out = new ArrayList(); for (File f : unnull(l)) if (!f.isDirectory() && (empty(suffix) || endsWithIgnoreCase(f.getName(), suffix))) out.add(f); return out; } public static String addPrefixIfNotEmpty2(String prefix, String s) { return empty(s) ? "" : addPrefix(prefix, s); } public static Object[] asObjectArray(Collection l) { return toObjectArray(l); } public static File toFile(Object o) { if (o instanceof File) return (File) o; if (o instanceof String) return new File((String) o); throw fail("Not a file: " + o); } public static void sort(T[] a, Comparator c) { Arrays.sort(a, c); } public static void sort(T[] a) { Arrays.sort(a); } public static void sort(int[] a) { if (a != null) Arrays.sort(a); } public static void sort(List a, Comparator c) { Collections.sort(a, c); } public static void sort(List a) { Collections.sort(a); } public static int stdcompare(Number a, Number b) { return cmp(a, b); } public static int stdcompare(String a, String b) { return cmp(a, b); } public static int stdcompare(long a, long b) { return a < b ? -1 : a > b ? 1 : 0; } public static int stdcompare(Object a, Object b) { return cmp(a, b); } public static File getCanonicalFile(File f) { try { return f == null ? null : f.getCanonicalFile(); } catch (Exception __e) { throw rethrow(__e); } } public static boolean isSymLink(File f) { return f != null && Files.isSymbolicLink(toPath(f)); } public static int findEndOfAngleBracketPart(List cnc, int i) { int j = i + 2, level = 1; while (j < cnc.size()) { if (eq(cnc.get(j), "<")) ++level; else if (eq(cnc.get(j), ">")) --level; if (level == 0) return j + 1; ++j; } return cnc.size(); } public static boolean mechList_goToCreator_dont = false; public static boolean mechList_goToCreator() { return eqOneOf(mechMode().readMode, null, "creator") && !mechList_goToCreator_dont && hasMethodNamed(creator(), "mechList_opt_raw"); } public static MultiMap parseDoubleArrowMultiMap(MultiMap map, List l) { for (String s : l) { List x = splitAtDoubleArrow_bothDirections(s); if (l(x) == 2) map.put(first(x), second(x)); } return map; } public static TreeSet mechSetCI(String name) { return mechCISet(name); } public static String dropTrailingNumberFromAngleBracketCategory(String s) { if (!isAngleBracketed(s)) return s; int i = l(s) - 1; while (i > 1 && isDigit(s.charAt(i - 1))) --i; if (i == l(s) - 1) return s; return substring(s, 0, i) + ">"; } public static String mechLanguagePrefix() { return ai_mechLanguagePrefix(); } public static String firstToUpper(String s) { if (empty(s)) return s; return Character.toUpperCase(s.charAt(0)) + s.substring(1); } public static String plural_mech(String s) { String __1833 = mechMapCI("Singular => Plural").get(s); if (!empty(__1833)) return __1833; return plural(s); } public static List replace(List l, A a, A b) { for (int i = 0; i < l(l); i++) if (eq(l.get(i), a)) l.set(i, b); return l; } public static String replace(String s, String a, String b) { return s == null ? null : a == null || b == null ? s : s.replace(a, b); } public static String replace(String s, char a, char b) { return s == null ? null : s.replace(a, b); } public static String deAngleBracket(String s) { return isAngleBracketed(s) ? dropFirstAndLast(s) : s; } public static Class primitiveToBoxedType(Class type) { if (type == boolean.class) return Boolean.class; if (type == int.class) return Integer.class; if (type == long.class) return Long.class; if (type == float.class) return Float.class; if (type == short.class) return Short.class; if (type == char.class) return Character.class; if (type == byte.class) return Byte.class; if (type == double.class) return Double.class; return null; } public static String joinLines(List lines) { return fromLines(lines); } public static String joinLines(String glue, String text) { return join(glue, toLines(text)); } public static String javaDropComments(String s) { return javaDropAllComments(s); } public static ClassLoader getClassLoader(Object o) { return o == null ? null : _getClass(o).getClassLoader(); } public static int parseFirstInt(String s) { return parseInt(jextract("", s)); } public static String javaVersion() { return System.getProperty("java.version"); } public static String defaultVMArgs() { return javaxDefaultVMArgs(); } public static void setDefaultVMArgs(String args) { String oldArgs = javaxDefaultVMArgs(); args = trim(args); if (neq(unnull(oldArgs), unnull(args))) { print(); print("Changing default VM arguments from"); print(" " + oldArgs); print("to"); print(" " + args); print(); saveTextFile(javaxDataDir("default-vm-args"), nullIfEmpty(args)); } } public static AutoCloseable tempSetTL(ThreadLocal tl, A a) { return tempSetThreadLocal(tl, a); } public static Set mcFields() { return staticFieldNames(mc()); } public static boolean startsWithEndsWith(String s, String prefix, String suffix) { return startsWith(s, prefix) && endsWith(s, suffix); } public static boolean startsWithEndsWith(String s, String prefixSuffix) { return startsWithEndsWith(s, prefixSuffix, prefixSuffix); } public static void removeFromMapCI_sync(Map map, String key) { if (map == null) return; synchronized (getMutex(map)) { for (String k : cloneList(keys(map))) if (eqic(key, k)) map.remove(k); } } public static File oneOfTheFiles(String... paths) { if (paths != null) for (String path : paths) if (fileExists(path)) return newFile(path); return null; } public static File oneOfTheFiles(File... files) { if (files != null) for (File f : files) if (fileExists(f)) return f; return null; } public static boolean isWindows() { return System.getProperty("os.name").contains("Windows"); } public static boolean isMac() { return System.getProperty("os.name").toLowerCase().contains("mac"); } public static boolean isJavaxCompilableSnippetTypeExceptInclude(int type) { return isJavaxApplicationSnippetType(type) || isJavaxModuleSnippetType(type) || type == snippetType_dynModule(); } public static int javaxIncludeSnippetType() { return 42; } public static List hotwire_libraryIDsFromJar_deleteJarOnFail(File jar) { try { return hotwire_libraryIDsFromJar(jar); } catch (Throwable _e) { jar.delete(); throw rethrow(_e); } } public static byte[] isJAR_magic = bytesFromHex("504B0304"); public static boolean isJAR(byte[] data) { return byteArrayStartsWith(data, isJAR_magic); } public static boolean isJAR(File f) { return isJAR(loadBeginningOfBinaryFile(f, l(isJAR_magic))); } public static byte[] takeFirstOfByteArray(byte[] b, int n) { return subByteArray(b, 0, n); } public static byte[] takeFirstOfByteArray(int n, byte[] b) { return takeFirstOfByteArray(b, n); } public static List listFilesWithSuffix(File dir, String suffix) { List l = new ArrayList(); for (File f : listFiles(dir)) if (!f.isDirectory() && (empty(suffix) || endsWithIgnoreCase(f.getName(), suffix))) l.add(f); return l; } public static String addPrefix(String prefix, String s) { return s.startsWith(prefix) ? s : prefix + s; } public static Path toPath(File f) { return f == null ? null : f.toPath(); } public static boolean hasMethodNamed(Object obj, String method) { if (obj == null) return false; if (obj instanceof Class) return hasMethodNamed((Class) obj, method); return hasMethodNamed(obj.getClass(), method); } public static boolean hasMethodNamed(Class c, String method) { while (c != null) { for (Method m : c.getDeclaredMethods()) if (m.getName().equals(method)) return true; c = c.getSuperclass(); } return false; } public static String ai_mechLanguagePrefix() { String l = ai_language(); return eqic(l, "English") ? "" : l + " | "; } public static String plural(String s) { return getPlural(s); } public static String jextract(String pat, String s) { return jextract(pat, javaTok(s)); } public static String jextract(String pat, List tok) { List tokpat = javaTok(pat); jfind_preprocess(tokpat); int i = jfind(tok, tokpat); if (i < 0) return null; int j = i + l(tokpat) - 2; return joinSubList(tok, i, j); } public static String javaxDefaultVMArgs() { File fileNew, fileOld; String text = loadTextFile(fileNew = javaxDataDir("default-vm-args")); if (text == null) { text = loadTextFile(fileOld = programFile("#1005850", "default-vm-args")); if (text != null) moveFile(fileOld, fileNew); } return trim(unnull(text)); } public static String nullIfEmpty(String s) { return isEmpty(s) ? null : s; } public static Map nullIfEmpty(Map map) { return isEmpty(map) ? null : map; } public static List nullIfEmpty(List l) { return isEmpty(l) ? null : l; } public static Set staticFieldNames(Object o) { TreeSet fields = new TreeSet(); Class _c = _getClass(o); do { for (Field f : _c.getDeclaredFields()) if ((f.getModifiers() & Modifier.STATIC) != 0) fields.add(f.getName()); _c = _c.getSuperclass(); } while (_c != null); return fields; } public static Object getMutex(Object o) { return collectionMutex(o); } public static boolean isJavaxApplicationSnippetType(int type) { return type == snippetType_javaxSource() || type == snippetType_JavaXDesktop(); } public static boolean isJavaxModuleSnippetType(int type) { return type == snippetType_javaxModule() || type == snippetType_javaxDesktopModule(); } public static int snippetType_dynModule() { return 57; } public static List hotwire_libraryIDsFromJar(File jar) { String dehlibs = unnull(loadTextFileFromZip(jar, "libraries")); return regexpExtractAll("\\d+", dehlibs); } public static byte[] subByteArray(byte[] b, int start) { return subByteArray(b, start, l(b)); } public static byte[] subByteArray(byte[] b, int start, int end) { start = max(start, 0); end = min(end, l(b)); if (start == 0 && end == l(b)) return b; if (start >= end) return new byte[0]; byte[] x = new byte[end - start]; System.arraycopy(b, start, x, 0, end - start); return x; } public static List getPlural_specials = ll("sheep", "fish"); public static String getPlural(String s) { if (containsIgnoreCase(getPlural_specials, s)) return s; if (ewic(s, "y")) return dropSuffixIgnoreCase("y", s) + "ies"; if (ewic(s, "ss")) return s + "es"; if (ewic(s, "s")) return s; return s + "s"; } public static File programFile(String name) { return prepareProgramFile(name); } public static File programFile(String progID, String name) { return prepareProgramFile(progID, name); } public static void moveFile(File a, File b) { if (!renameFile(a, b)) throw fail("File move failed: " + a + " to " + b); } public static boolean isEmpty(Collection c) { return c == null || c.isEmpty(); } public static boolean isEmpty(CharSequence s) { return s == null || s.length() == 0; } public static boolean isEmpty(Object[] a) { return a == null || a.length == 0; } public static boolean isEmpty(byte[] a) { return a == null || a.length == 0; } public static boolean isEmpty(Map map) { return map == null || map.isEmpty(); } public static int snippetType_javaxSource() { return 34; } public static int snippetType_JavaXDesktop() { return 55; } public static int snippetType_javaxModule() { return 54; } public static int snippetType_javaxDesktopModule() { return 58; } public static List regexpExtractAll(String pat, String s) { if (s == null) return null; Matcher m = regexpMatcher(pat, s); List out = new ArrayList(); while (m.find()) out.add(m.group()); return out; } public static boolean containsIgnoreCase(Collection l, String s) { if (l != null) for (String x : l) if (eqic(x, s)) return true; return false; } public static boolean containsIgnoreCase(String[] l, String s) { if (l != null) for (String x : l) if (eqic(x, s)) return true; return false; } public static boolean containsIgnoreCase(String s, char c) { return indexOfIgnoreCase(s, String.valueOf(c)) >= 0; } public static boolean containsIgnoreCase(String a, String b) { return indexOfIgnoreCase(a, b) >= 0; } public static String dropSuffixIgnoreCase(String suffix, String s) { return ewic(s, suffix) ? s.substring(0, l(s) - l(suffix)) : s; } public static File prepareProgramFile(String name) { return mkdirsForFile(getProgramFile(name)); } public static File prepareProgramFile(String progID, String name) { return mkdirsForFile(getProgramFile(progID, name)); } public static boolean renameFile(File a, File b) { mkdirsForFile(b); return a.renameTo(b); } public static boolean renameFile(File a, String newName) { return renameFile(a, fileInSameDir(a, newName)); } public static int indexOfIgnoreCase(List a, String b) { return indexOfIgnoreCase(a, b, 0); } public static int indexOfIgnoreCase(List a, String b, int i) { int n = a == null ? 0 : a.size(); for (; i < n; i++) if (eqic(a.get(i), b)) return i; return -1; } public static int indexOfIgnoreCase(String a, String b) { return indexOfIgnoreCase_manual(a, b); } public static File fileInSameDir(File f, String newName) { return newFile(parentFile(f), newName); } public static int indexOfIgnoreCase_manual(String a, String b) { int la = strL(a), lb = strL(b); if (la < lb) return -1; int n = la - lb; loop: for (int i = 0; i <= n; i++) { for (int j = 0; j < lb; j++) { char c1 = a.charAt(i + j), c2 = b.charAt(j); if (!eqic(c1, c2)) continue loop; } return i; } return -1; } public static File parentFile(File f) { return dirOfFile(f); } public static File dirOfFile(File f) { return f == null ? null : f.getParentFile(); } public static class TimedCache { public long timeout; public volatile A value; public Object function; public long set; public Lock lock = lock(); public boolean keepValueWhileCalculating = false; public int stores, fails, hits; public TimedCache(double timeoutSeconds, Object function) { this(function, timeoutSeconds); } public TimedCache(Object function, double timeoutSeconds) { this(timeoutSeconds); this.function = function; } public TimedCache(double timeoutSeconds) { timeout = toMS(timeoutSeconds); } public A set(A a) { Lock __1947 = lock; lock(__1947); try { ++stores; value = a; set = now(); return a; } finally { unlock(__1947); } } public boolean has() { Lock __1948 = lock; lock(__1948); try { clean(); if (set != 0) { ++hits; return true; } ++fails; return false; } finally { unlock(__1948); } } public A get() { Lock __1949 = lock; lock(__1949); try { if (function != null) return get(function); clean(); if (set != 0) ++hits; else ++fails; return value; } finally { unlock(__1949); } } public A get(Object makerFunction) { Lock __1950 = lock; lock(__1950); try { if (keepValueWhileCalculating) { if (value == null || shouldClean()) set((A) callF(makerFunction)); return value; } else { return this.has() ? getNoClean() : set((A) callF(makerFunction)); } } finally { unlock(__1950); } } public A getNoClean() { Lock __1951 = lock; lock(__1951); try { return value; } finally { unlock(__1951); } } public void clean() { Lock __1952 = lock; lock(__1952); try { if (shouldClean()) clear(); } finally { unlock(__1952); } } public boolean shouldClean() { return timeout > 0 && now() > set + timeout; } public void clear() { Lock __1953 = lock; lock(__1953); try { set = 0; value = null; } finally { unlock(__1953); } } public String stats() { return "Stores: " + stores + ", hits: " + hits + ", fails: " + fails; } public A peek() { return value; } } public static class Scored extends Var { public float score; public Scored() { } public Scored(A a, float score) { super(a); this.score = score; } public Scored(A a, double score) { super(a); this.score = (float) score; } public float score() { return score; } public String toString() { return toIntPercent(score) + "%: " + str(get()); } } public static long toMS(double seconds) { return (long) (seconds * 1000); } public static int toIntPercent(double ratio) { return roundToInt(ratio * 100); } public static int toIntPercent(float ratio) { return toIntPercent((double) ratio); } }