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;
}
static boolean startsWithAndEndsWith(String s, String prefix, String suffix) {
return startsWith(s, prefix) && endsWith(s, suffix);
}
static boolean swic(String a, String b) {
return startsWithIgnoreCase(a, b);
}
static boolean ewic(String a, String b) {
return endsWithIgnoreCase(a, b);
}
static boolean containsNewLines(String s) {
return containsNewLine(s);
}
static String jlabel_textAsHTML_center(String text) {
return ""
+ replace(htmlencode(text), "\n", "
")
+ "
";
}
static List subList(List l, int startIndex) {
return subList(l, startIndex, l(l));
}
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);
}
static String shortenClassName(String name) {
if (name == null) return null;
int i = lastIndexOf(name, "$");
if (i < 0) i = lastIndexOf(name, ".");
return i < 0 ? name : substring(name, i+1);
}
static Set syncIdentityHashSet() {
return (Set) synchronizedSet(identityHashSet());
}
static Map syncHashMap() {
return synchroHashMap();
}
static A popLast(List l) {
return liftLast(l);
}
// PersistableThrowable doesn't hold GC-disturbing class references in backtrace
static volatile PersistableThrowable lastException_lastException;
static PersistableThrowable lastException() {
return lastException_lastException;
}
static void lastException(Throwable e) {
lastException_lastException = persistableThrowable(e);
}
static boolean isStaticMethod(Method m) {
return methodIsStatic(m);
}
static Method findMethod(Object o, String method, Object... args) {
return findMethod_cached(o, method, args);
}
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;
}
static List dropFirstAndLast(int n, List l) {
return new ArrayList(subList(l, n, l(l)-n));
}
static List dropFirstAndLast(List l) {
return dropFirstAndLast(1, l);
}
static String dropFirstAndLast(String s) {
return substring(s, 1, l(s)-1);
}
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);
}
static String javaTok_substringC(String s, int i, int j) {
return s.substring(i, j);
}
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;
// scan for whitespace
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);
// scan for non-whitespace
// Special JavaX syntax: 'identifier
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 /*|| s.charAt(j) == '\n'*/) { // allow multi-line strings
++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)); // for stuff like "don't"
else if (Character.isDigit(c)) {
do ++j; while (j < l && Character.isDigit(s.charAt(j)));
if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
} 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;
}
static boolean javaTokWithExisting_isCopyable(String t, String s, int i, int j) {
return t.length() == j-i
&& s.regionMatches(i, t, 0, j-i); // << could be left out, but that's brave
}
static boolean odd(int i) {
return (i & 1) != 0;
}
static boolean odd(long i) {
return (i & 1) != 0;
}
static boolean odd(BigInteger i) { return odd(toInt(i)); }
// i must point at the (possibly imaginary) opening bracket
// index returned is index of closing bracket + 1
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();
}
static String joinSubList(List l, int i, int j) {
return join(subList(l, i, j));
}
static String joinSubList(List l, int i) {
return join(subList(l, i));
}
static boolean startsWithIgnoreCase(String a, String b) {
return regionMatchesIC(a, 0, b, 0, b.length());
}
static boolean endsWithIgnoreCase(String a, String b) {
int la = l(a), lb = l(b);
return la >= lb && regionMatchesIC(a, la-lb, b, 0, lb);
}
static boolean containsNewLine(String s) {
return contains(s, '\n'); // screw \r, nobody needs it
}
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;
}
static String replace(String s, String a, String b) {
return s == null ? null : a == null || b == null ? s : s.replace(a, b);
}
static String replace(String s, char a, char b) {
return s == null ? null : s.replace(a, b);
}
static String htmlencode(Object o) {
return htmlencode(str(o));
}
static String htmlencode(String s) {
if (s == null) return "";
StringBuilder out = new StringBuilder(Math.max(16, s.length()));
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
/*if (c >= 0x100)
out.append("").append(charToHex(c)).append(';');
else*/ if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&')
out.append("").append((int) c).append(';');
else
out.append(c);
}
return out.toString();
}
static int lastIndexOf(String a, String b) {
return a == null || b == null ? -1 : a.lastIndexOf(b);
}
static int lastIndexOf(String a, char b) {
return a == null ? -1 : a.lastIndexOf(b);
}
static Set synchronizedSet() {
return synchroHashSet();
}
static Set synchronizedSet(Set set) {
return Collections.synchronizedSet(set);
}
static Set identityHashSet() {
return Collections.newSetFromMap(new IdentityHashMap());
}
static A liftLast(List l) {
if (l.isEmpty()) return null;
int i = l(l)-1;
A a = l.get(i);
l.remove(i);
return a;
}
static boolean methodIsStatic(Method m) {
return (m.getModifiers() & Modifier.STATIC) != 0;
}
static boolean regionMatchesIC(String a, int offsetA, String b, int offsetB, int len) {
return a != null && a.regionMatches(true, offsetA, b, offsetB, len);
}
static boolean contains(Collection c, Object o) {
return c != null && c.contains(o);
}
static boolean contains(Object[] x, Object o) {
if (x != null)
for (Object a : x)
if (eq(a, o))
return true;
return false;
}
static boolean contains(String s, char c) {
return s != null && s.indexOf(c) >= 0;
}
static boolean contains(String s, String b) {
return s != null && s.indexOf(b) >= 0;
}
static boolean contains(BitSet bs, int i) {
return bs != null && bs.get(i);
}
static Set synchroHashSet() {
return Collections.synchronizedSet(new HashSet());
}
// immutable, has strong refs
final static class _MethodCache {
final Class c;
final HashMap> cache = new HashMap();
_MethodCache(Class c) {
this.c = c;
_init();
}
void _init() {
Class _c = c;
while (_c != null) {
for (Method m : _c.getDeclaredMethods()) {
m.setAccessible(true);
multiMapPut(cache, m.getName(), m);
}
_c = _c.getSuperclass();
}
}
// Returns only matching methods
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); } }
}static abstract class VF1 {
abstract void get(A a);
}abstract static class DynBigNumber extends DynModule {
String description;
Object value;
int fontSize = 40;
String toolTip;
transient JLabel label;
transient SimpleLiveValue < String > lvText = new SimpleLiveValue(String.class);
transient SimpleLiveValue < String > lvDesc = new SimpleLiveValue(String.class);
// PUBLIC API
// any Number, or anything persistable with toString
boolean setValue(Object value) {
boolean changed = setField("value", value);
makeTexts();
return changed;
}
void setDescription(String desc) {
setField("description", desc);
makeTexts();
}
Object getValue() { return value; }
String getDescription() { return description; }
void setToolTip(String toolTip) { setField("toolTip", toolTip); }
// END PUBLIC API
void makeTexts() {
lvText.set(value == null ? "-" : str(value));
lvDesc.set(description);
}
void start() { super.start();
_persistenceInfo = mapPlus(_persistenceInfo, "value" , false);
makeTexts();
}
JComponent visualize() {
return bindToolTipToLiveValue(dm_fieldLiveValue("toolTip"),
jLiveValueSection(lvDesc,
centerLabel(makeBold(setFontSize(fontSize, label = jLiveValueLabel(lvText))))));
}
}static class Var implements IVar {
A v; // you can access this directly if you use one thread
Var() {}
Var(A v) {
this.v = 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()); }
}static class PersistableThrowable {
String className;
String msg;
String stacktrace;
PersistableThrowable() {}
PersistableThrowable(Throwable e) {
if (e == null)
className = "Crazy Null Error";
else {
className = getClassName(e).replace('/', '.');
msg = e.getMessage();
stacktrace = getStackTrace_noRecord(e);
}
}
public String toString() {
return nempty(msg) ? className + ": " + msg : className;
}
}static abstract class F0 {
abstract A get();
}static abstract class LiveValue {
abstract Class getType();
abstract A get();
abstract void onChange(Runnable l);
abstract void removeOnChangeListener(Runnable l);
void onChangeAndNow(Runnable l) {
onChange(l);
callF(l);
}
}static class FixedRateTimer extends java.util.Timer {
FixedRateTimer() { this(false); }
FixedRateTimer(boolean daemon) { this(defaultTimerName(), daemon); }
FixedRateTimer(String name) { this(name, false); }
FixedRateTimer(String name, boolean daemon) {
super(name, daemon);
_registerTimer(this);
}
List entries = synchroList();
static class Entry {
static String _fieldOrder = "task firstTime period";
TimerTask task;
long firstTime;
long period;
Entry() {}
Entry(TimerTask task, long firstTime, long period) {
this.period = period;
this.firstTime = firstTime;
this.task = task;}
public String toString() { return "Entry(" + task + ", " + firstTime + ", " + period + ")"; }}
// Note: not all methods overridden; only use these ones
public void scheduleAtFixedRate(TimerTask task, long delay, long period) {
entries.add(new Entry(task, now()+delay, period));
super.scheduleAtFixedRate(task, delay, period);
}
public void cancel() {
entries.clear();
super.cancel();
}
public int purge() {
entries.clear();
return super.purge();
}
FixedRateTimer changeRate(int newPeriod) {
Object r = ((SmartTimerTask) first(entries).task).r;
cancel();
return doEvery(newPeriod, r);
}
}static class Pair implements Comparable> {
A a;
B b;
Pair() {}
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);
}
}
static class SimpleLiveValue extends LiveValue {
Class type;
volatile A value;
transient List onChange = synchroList();
SimpleLiveValue(Class type) {
this.type = type;}
SimpleLiveValue(Class type, A value) {
this.value = value;
this.type = type;}
public Class getType() { return type; }
public A get() { return value; }
public void onChange(Runnable l) { onChange.add(l); }
public void onChangeAndNow(Runnable l) { onChange(l); callF(l); }
public void removeOnChangeListener(Runnable l) { onChange.remove(l); }
void fireChanged() {
pcallFAll(onChange);
}
void set(A a) { if (neq(value, a)) { value = a; fireChanged(); } }
}static abstract class DynModule {
String name, toolTip;
PersistableThrowable _error;
Map> mechLists;
Map _persistenceInfo;
transient Object _host;
transient Map timers = newWeakHashMap(); // all kinds of resources actually; value is closer helper
transient Set _resources = synchroHashSet();
transient Lock lock; // set by stem
transient boolean persistOnChangedField = true;
transient int changeCount;
transient Object changeCountSync = new Object();
transient List onChange;
transient boolean verboseTimers;
transient ReliableSingleThread rstUpdate;
transient Set componentFieldsToKeep;
transient Map transientGeneralMap = synchroHashMap();
transient Q q; // module command queue
DynModule() {
dm_initErrorHandling();
setMainDesktopPane((JDesktopPane) getCreatorOpt("desktop"));
}
boolean isVisible() { return isTrue(getOpt(_host, "visible")); }
String moduleName() { return name; }
void setModuleName(String name) {
String oldName = this.name;
if (!eq(name, oldName)) {
this.name = name;
possiblyInternalFrameTitle(vis(), name);
vmBus_send("moduleNameChange", this, oldName, name);
}
}
void setModuleToolTip(String toolTip) {
this.toolTip = toolTip;
}
JComponent vis() {
return (JComponent) getOpt(_host, "vis");
}
A ownResource(A a) {
if (a != null)
_resources.add(a);
return a;
}
A ownTimer(A timer) {
if (timer instanceof AutoCloseable) ownResource((AutoCloseable) timer);
ownTimer(timer, "cancelTimerOrInterruptThread");
return timer;
}
void ownTimer(Object timer, Object closerHelper) {
timers.put(timer, closerHelper);
}
void singleTimer(java.util.Timer timer) {
stopAllTimers();
ownTimer(timer);
}
void stopAllTimers() {
for (AutoCloseable resource : getAndClearList(_resources)) {
if (verboseTimers)
print("Releasing resource: " + resource);
try { resource.close(); } catch (Throwable __e) { _handleException(__e); }
}
{ final Map