Warning : session_start(): open(/var/lib/php/sessions/sess_r75a6glhbshji3gq1rouhc0mgj, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning : session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import java.util.function.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.imageio.*;
import java.math.*;
import java.time.Duration;
import java.lang.invoke.VarHandle;
import java.lang.invoke.MethodHandles;
// A database system for only slightly modified Java objects.
// Recent changes:
// -minimal crash recovery disabled for now (could reenable)
// -idCounter.structure file no longer used
// -allDynamic/safeLoad disabled, it doesn't really make any sense
// -conceptsFile can be in any directory now
// Functions that should always be there for child processes:
import java.text.NumberFormat;
import java.text.*;
import java.awt.geom.*;
import java.nio.charset.Charset;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
import static x30_pkg.x30_util.DynamicObject;
import java.nio.file.Path;
class main {
static int concepts_internStringsLongerThan = 10;
static ThreadLocal concepts_unlisted = new ThreadLocal();
// BREAKING CHANGE 2021/6/7 set to true
static boolean concepts_unlistedByDefault = true; // true = we can create instances of concepts with "new" without registering them automatically
interface IConceptIndex {
void update(Concept c); // also for adding
void remove(Concept c);
}
interface IFieldIndex {
Collection getAll(Val val);
List allValues(); // returns a cloned list
MultiSet allValues_multiSet();
IterableIterator objectIterator();
}
// Approach to persisting the Concepts object itself (in normal
// DB operation, this is not done): For simplification, speed and
// compactness, we make almost all the fields transient and store only // the concepts and the idCounter. To unstructure the Concepts object,
// use unstructureConcepts() or postUnstructureConcepts(), then
// re-set up any indices, listeners etc.
// base class indicating nothing
static class ConceptsChange {}
// change of a single concept
static class ConceptCreate extends ConceptsChange implements IFieldsToList{
Concept c;
ConceptCreate() {}
ConceptCreate(Concept c) {
this.c = c;}
public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + c + ")"; }
public boolean equals(Object o) {
if (!(o instanceof ConceptCreate)) return false;
ConceptCreate __5 = (ConceptCreate) o;
return eq(c, __5.c);
}
public int hashCode() {
int h = -1751266972;
h = boostHashCombine(h, _hashCode(c));
return h;
}
public Object[] _fieldsToList() { return new Object[] {c}; }
}
// change of a single concept
static class ConceptChange extends ConceptsChange implements IFieldsToList{
Concept c;
ConceptChange() {}
ConceptChange(Concept c) {
this.c = c;}
public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + c + ")"; }
public boolean equals(Object o) {
if (!(o instanceof ConceptChange)) return false;
ConceptChange __6 = (ConceptChange) o;
return eq(c, __6.c);
}
public int hashCode() {
int h = -1760609256;
h = boostHashCombine(h, _hashCode(c));
return h;
}
public Object[] _fieldsToList() { return new Object[] {c}; }
}
// removal of a single concept
// c.id is going to become 0 at some point, so we pass the
// id separately
static class ConceptDelete extends ConceptsChange implements IFieldsToList{
static final String _fieldOrder = "id c";
long id;
Concept c;
ConceptDelete() {}
ConceptDelete(long id, Concept c) {
this.c = c;
this.id = id;}
public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + id + ", " + c + ")"; }
public boolean equals(Object o) {
if (!(o instanceof ConceptDelete)) return false;
ConceptDelete __7 = (ConceptDelete) o;
return id == __7.id && eq(c, __7.c);
}
public int hashCode() {
int h = -1734431213;
h = boostHashCombine(h, _hashCode(id));
h = boostHashCombine(h, _hashCode(c));
return h;
}
public Object[] _fieldsToList() { return new Object[] {id, c}; }
}
// unknown change anywhere in concepts; consider it all dirty
// (this one should not be used except for batch jobs)
static class FullChange extends ConceptsChange implements IFieldsToList{
FullChange() {}
public String toString() { return shortClassName_dropNumberPrefix(this); }
public boolean equals(Object o) {
return o instanceof FullChange;
}
public int hashCode() {
int h = 733452095;
return h;
}
public Object[] _fieldsToList() { return null; }
}
static class Concepts implements AutoCloseable {
SortedMap concepts = synchroTreeMap();
// ID of last created concept
final public long getIdCounter(){ return idCounter(); }
public long idCounter() { return idCounter; }
long idCounter;
transient HashMap perClassData;
transient Map miscMap; // don't use directly, call miscMap... methods to access
// set to "-" for non-persistent (possibly not implemented)
// also, can include a case ID ("#123/1")
// TODO: phase out (we have conceptsFile field now)
transient String programID;
transient File conceptsFile;
transient Concepts parent; // new mechanism
transient volatile long changes, changesWritten, lastChange;
transient volatile java.util.Timer autoSaver;
transient volatile boolean dontSave = false;
transient volatile boolean savingConcepts, noXFullGrab;
transient boolean vmBusSend = true;
transient boolean initialSave = false; // set to false to avoid initial useless saving
transient int autoSaveInterval = -1000; // 1 second + wait logic
transient boolean useGZIP = true, quietSave;
transient ReentrantLock lock = new ReentrantLock(true);
transient ReentrantLock saverLock = new ReentrantLock(true);
transient long lastSaveTook = -1, lastSaveWas, loadTook, uncompressedSize;
transient float maxAutoSavePercentage = 10;
transient List conceptIndices;
transient Map, Map> fieldIndices;
transient Map, Map> ciFieldIndices;
//transient L saveActions = synchroList();
transient List preSave;
transient Object classFinder = _defaultClassFinder();
transient List onAllChanged = synchroList(); // list of runnables
transient Set onChange = new HashSet();
transient Object saveWrapper; // VF1, to profile saving
final public Concepts setModifyOnCreate(boolean modifyOnCreate){ return modifyOnCreate(modifyOnCreate); }
public Concepts modifyOnCreate(boolean modifyOnCreate) { this.modifyOnCreate = modifyOnCreate; return this; } final public boolean getModifyOnCreate(){ return modifyOnCreate(); }
public boolean modifyOnCreate() { return modifyOnCreate; }
transient boolean modifyOnCreate = false; // set _modified == created initially
transient boolean modifyOnBackRef = false; // set modified if back refs change
transient boolean useFileLock = true; // instead of locking by bot
final public Concepts setGrabThroughSocket(boolean grabThroughSocket){ return grabThroughSocket(grabThroughSocket); }
public Concepts grabThroughSocket(boolean grabThroughSocket) { this.grabThroughSocket = grabThroughSocket; return this; } final public boolean getGrabThroughSocket(){ return grabThroughSocket(); }
public boolean grabThroughSocket() { return grabThroughSocket; }
transient boolean grabThroughSocket = true; // probably never want to do this again actually
// OLD - not done anymore. transient bool collectForwardRefs
transient FileBasedLock fileLock;
transient boolean storeBaseClassesInStructure = false; // helps with schema evolution when concept subclasses disappear
transient boolean useBackRefsForSearches = false; // assume backRefs are sane in order to speed up searches
transient boolean defunct = false;
transient int newBackupEveryXMinutes = 60;
final public Concepts setCloseConceptsOnExit(boolean closeConceptsOnExit){ return closeConceptsOnExit(closeConceptsOnExit); }
public Concepts closeConceptsOnExit(boolean closeConceptsOnExit) { this.closeConceptsOnExit = closeConceptsOnExit; return this; } final public boolean getCloseConceptsOnExit(){ return closeConceptsOnExit(); }
public boolean closeConceptsOnExit() { return closeConceptsOnExit; }
transient boolean closeConceptsOnExit = false;
final public Concepts setSaveError(Throwable saveError){ return saveError(saveError); }
public Concepts saveError(Throwable saveError) { this.saveError = saveError; return this; } final public Throwable getSaveError(){ return saveError(); }
public Throwable saveError() { return saveError; }
transient Throwable saveError;
transient Set> onSaveError;
public Concepts onSaveError(IVF1 f) { onSaveError = createOrAddToSyncLinkedHashSet(onSaveError, f); return this; }
public Concepts removeSaveErrorListener(IVF1 f) { main.remove(onSaveError, f); return this; }
public void fireSaveError(Throwable error) { if (onSaveError != null) for (var listener : onSaveError) pcallF_typed(listener, error); }
// add more fields for Concepts here
Concepts() {}
Concepts(String programID) {
this.programID = programID;}
Concepts(File conceptsFile) {
this.conceptsFile = conceptsFile;}
synchronized long internalID() {
do {
++idCounter;
} while (hasConcept(idCounter));
return idCounter;
}
synchronized HashMap perClassData() {
if (perClassData == null) perClassData = new HashMap();
return perClassData;
}
void initProgramID() {
if (programID == null)
programID = getDBProgramID();
}
// load from structure
Concepts load(String structure) { return load(structure, false); }
Concepts load(String structure, boolean allDynamic) {
clearConcepts();
Map map = unstructureMap(structure, allDynamic, classFinder);
concepts.putAll(map);
assignConceptsToUs();
calcIdCounter();
return this;
}
Concepts load() {
initProgramID();
// try custom grabber
Object dbGrabber = miscMapGet("dbGrabber");
if (dbGrabber != null && !isFalse(callF(dbGrabber)))
return this;
try {
if (grabThroughSocket && tryToGrab()) return this;
} catch (Throwable e) {
if (!exceptionMessageContains(e, "no xfullgrab"))
printShortException(e);
print("xfullgrab failed - loading DB of " + programID + " from disk");
}
return loadFromDisk();
}
Concepts loadFromDisk() {
if (nempty(concepts)) clearConcepts();
// minimal crash recovery (disabled for now)
//restoreLatestBackupIfConceptsFileEmpty(programID, doIt := true);
long time = now();
Map _concepts = (Map) (unstructureGZFile(conceptsFile(), toIF1(classFinder)));
putAll(concepts, _concepts);
assignConceptsToUs();
loadTook = now()-time;
done("Loaded " + n2(l(concepts), "concept"), time);
calcIdCounter();
return this;
}
Concepts loadConcepts() { return load(); }
boolean tryToGrab() {
if (sameSnippetID(programID, getDBProgramID())) return false;
RemoteDB db = connectToDBOpt(programID); try {
if (db != null) {
loadGrab(db.fullgrab());
return true;
}
return false;
} finally { _close(db); }}
Concepts loadGrab(String grab) {
clearConcepts();
DynamicObject_loading.set(true);
try {
Map map = (Map) unstructure(grab, false, classFinder);
concepts.putAll(map);
assignConceptsToUs();
for (long l : map.keySet())
idCounter = max(idCounter, l);
} finally {
DynamicObject_loading.set(null);
}
//XXX allChanged(); // Nobody is listening at this point anyway
return this;
}
void assignConceptsToUs() {
// fix unstructure bugs
for (Pair p: mapToPairs((Map) (Map) concepts))
if (!(p.b instanceof Concept)) {
print("DROPPING non-existant concept " + p.a + ": " + dynShortName(p.b));
concepts.remove(p.a);
}
for (Concept c : values(concepts)) c._concepts = this;
for (Concept c : values(concepts))
c._doneLoading2(); // doneLoading2 is called on all concepts after all concepts are loaded
}
String progID() {
return programID == null ? getDBProgramID() : programID;
}
Concept getConcept(String id) {
return empty(id) ? null : getConcept(parseLong(id));
}
Concept getConcept(long id) {
return (Concept) concepts.get((long) id);
}
Concept getConcept(RC ref) {
return ref == null ? null : getConcept(ref.longID());
}
boolean hasConcept(long id) {
return concepts.containsKey((long) id);
}
void deleteConcept(long id) {
Concept c = getConcept(id);
if (c == null)
print("Concept " + id + " not found");
else
c.delete();
}
void calcIdCounter() {
Long lastID = lastKey(concepts);
idCounter = lastID == null ? 1 : lastID+1;
}
File conceptsDir() { return dirOfFile(conceptsFile()); }
Concepts conceptsFile(File conceptsFile) { this.conceptsFile = conceptsFile; return this; }
File conceptsFile() {
if (conceptsFile != null) return conceptsFile;
return getProgramFile(programID, useGZIP ? "concepts.structure.gz" : "concepts.structure");
}
// used for locking when useFileLock is activated
File lockFile() {
return newFile(conceptsDir(), "concepts.lock");
}
FileBasedLock fileLock() {
if (fileLock == null)
fileLock = new FileBasedLock(lockFile());
return fileLock;
}
void saveConceptsIfDirty() { saveConcepts(); }
void save() { saveConcepts(); }
void saveConcepts() {
vmBus_send("saveConceptsCalled", Concepts.this);
if (dontSave) return;
initProgramID();
saverLock.lock();
savingConcepts = true;
long start = now(), time;
try {
String s = null;
//synchronized(main.class) {
long _changes = changes;
if (_changes == changesWritten) return;
File f = conceptsFile();
lock.lock();
long fullTime = now();
try {
if (useGZIP) {
vmBus_send("callingSaveWrapper", Concepts.this, saveWrapper);
callRunnableWithWrapper(saveWrapper, new Runnable() { public void run() { try {
vmBus_send("callingPreSave", Concepts.this, preSave);
callFAll(preSave);
vmBus_send("writingFile", Concepts.this, f);
uncompressedSize = saveGZStructureToFile(f, cloneMap(concepts), makeStructureData());
vmBus_send("gzFileSaved", Concepts.this, f, uncompressedSize);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "vmBus_send callingPreSave(Concepts.this, preSave);\r\n callFAll(preS..."; }});
newFile(conceptsDir(), "concepts.structure").delete();
} else
s = fullStructure();
} finally {
lock.unlock();
}
/*while (nempty(saveActions))
pcallF(popFirst(saveActions));*/
changesWritten = _changes; // only update when structure didn't fail
if (!useGZIP) {
time = now()-start;
if (!quietSave)
print("Saving " + toM(l(s)) + "M chars (" /*+ changesWritten + ", "*/ + time + " ms)");
start = now();
saveTextFile(f, javaTokWordWrap(s));
newFile(conceptsDir(), "concepts.structure.gz").delete();
}
File conceptsFile = conceptsFile();
File backupFile = newFile(conceptsDir(), "backups/" + fileName(conceptsFile) + ".backup" + ymd() + "-" + formatInt(hours(), 2)
+ (newBackupEveryXMinutes >= 60 ? "" : formatInt(roundDownTo_rev(minutes(), newBackupEveryXMinutes), 2)));
// TODO: get rid of this
copyFile(f, backupFile);
time = now()-start;
if (!quietSave)
print("Saved " + toK(f.length()) + " K, " + n(concepts, "concepts") + " (" + time + " ms)");
lastSaveWas = fullTime;
lastSaveTook = now()-fullTime;
saveError(null);
} finally {
savingConcepts = false;
saverLock.unlock();
}
}
void _autoSaveConcepts() {
if (autoSaveInterval < 0 && maxAutoSavePercentage != 0) {
long pivotTime = Math.round(lastSaveWas+lastSaveTook*100.0/maxAutoSavePercentage);
if (now() < pivotTime) {
//print("Skipping auto-save (last save took " + lastSaveTook + ")");
return;
}
}
try {
saveConcepts();
} catch (Throwable e) {
saveError(e);
print("Concept save failed, will try again");
printStackTrace(e);
fireSaveError(e);
}
}
String fullStructure() {
return structure(cloneMap(concepts), makeStructureData());
}
transient IF0 makeStructureData;
structure_Data makeStructureData() { return makeStructureData != null ? makeStructureData.get() : makeStructureData_base(); }
final structure_Data makeStructureData_fallback(IF0 _f) { return _f != null ? _f.get() : makeStructureData_base(); }
structure_Data makeStructureData_base() {
return finishStructureData(new structure_Data());
}
structure_Data finishStructureData(structure_Data data) {
if (storeBaseClassesInStructure)
data.storeBaseClasses = true;
return data;
}
void clearConcepts() {
for (Concept c : allConcepts()) c.delete();
//concepts.clear();
//allChanged();
}
void fireLegacyChangeEvent() {
synchronized(this) { ++changes; lastChange = sysNow(); }
if (vmBusSend) vmBus_send("conceptsChanged", this);
pcallFAll(onAllChanged);
}
// auto-save every second if dirty
synchronized void autoSaveConcepts() {
if (autoSaver == null) {
if (isTransient()) throw fail("Can't persist transient database");
autoSaver = doEvery_daemon("Concepts Saver for " + conceptsDir(),
abs(autoSaveInterval), new Runnable() { public void run() { try { _autoSaveConcepts() ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "_autoSaveConcepts()"; }});
// print("Installed auto-saver (" + autoSaveInterval + " ms, " + progID() + ")");
}
}
public void close() { cleanMeUp(); }
void cleanMeUp() {
try {
if (closeConceptsOnExit)
closeAllOpt(allConcepts());
defunct = true;
boolean shouldSave = autoSaver != null;
if (autoSaver != null) {
autoSaver.cancel();
autoSaver = null;
}
while (savingConcepts) sleepInCleanUp(10);
if (shouldSave)
saveConceptsIfDirty();
} catch (Throwable __e) { pcallFail(__e); }
{ cleanUp(fileLock); fileLock = null; }
}
Map getIDsAndNames() {
Map map = new HashMap();
Map cloned = cloneMap(concepts);
for (long id : keys(cloned))
map.put(id, cloned.get(id).className);
return map;
}
void deleteConcepts(List l) {
ping();
if (l != null) for (Object o : cloneList(l))
if (o instanceof Long) {
Concept c = concepts.get(o);
if (c != null) c.delete();
} else if (o instanceof Concept)
((Concept) o).delete();
else
warn("Can't delete " + getClassName(o));
}
A conceptOfType(Class type) {
IConceptCounter counter = conceptCounterForClass(type);
if (counter != null) return (A) first(counter.allConcepts());
return firstOfType(allConcepts(), type);
}
List conceptsOfType(Class type) {
List l = conceptsOfType_noParent(type);
if (parent == null) return l;
return concatLists_conservative(l, parent.conceptsOfType(type));
}
List conceptsOfType_noParent(Class type) {
ping();
IConceptCounter counter = conceptCounterForClass(type);
if (counter != null) return (List ) cloneList(counter.allConcepts());
return filterByType(allConcepts(), type);
}
List listConcepts(Class type) {
return conceptsOfType(type);
}
List list(Class type) {
return conceptsOfType(type);
}
List list_noParent(Class type) {
return conceptsOfType_noParent(type);
}
// TODO: would be better to make this Cl (indices may return sets)
List list(String type) {
return conceptsOfType(type);
}
List conceptsOfType(String type) {
return filterByDynamicType(allConcepts(), "main$" + type);
}
boolean hasConceptOfType(Class extends Concept> type) {
return hasType(allConcepts(), type);
}
void persistConcepts() {
loadConcepts();
autoSaveConcepts();
}
// We love synonyms
void conceptPersistence() { persistConcepts(); }
Concepts persist() { persistConcepts(); return this; }
void persist(Integer interval) {
if (interval != null) autoSaveInterval = interval;
persist();
}
// Runs r if there is no concept of that type
A ensureHas(Class c, Runnable r) {
A a = conceptOfType(c);
if (a == null) {
r.run();
a = conceptOfType(c);
if (a == null)
throw fail("Concept not made by " + r + ": " + shortClassName(c));
}
return a;
}
// Ensures that every concept of type c1 is ref'd by a concept of
// type c2.
// Type of func: voidfunc(concept)
void ensureHas(Class extends Concept> c1, Class extends Concept> c2, Object func) {
for (Concept a : conceptsOfType(c1)) {
Concept b = findBackRef(a, c2);
if (b == null) {
callF(func, a);
b = findBackRef(a, c2);
if (b == null)
throw fail("Concept not made by " + func + ": " + shortClassName(c2));
}
}
}
// Type of func: voidfunc(concept)
void forEvery(Class extends Concept> type, Object func) {
for (Concept c : conceptsOfType(type))
callF(func, c);
}
int deleteAll(Class extends Concept> type) {
List l = (List) conceptsOfType(type);
for (Concept c : l) c.delete();
return l(l);
}
// always returns a new list (callers depend on this)
Collection allConcepts() {
synchronized(concepts) {
return new ArrayList(values(concepts));
}
}
IConceptCounter conceptCounterForClass(Class extends Concept> c) {
for (IFieldIndex idx : values(mapGet(fieldIndices, c)))
if (idx instanceof IConceptCounter) return ((IConceptCounter) idx);
for (IFieldIndex idx : values(mapGet(ciFieldIndices, c)))
if (idx instanceof IConceptCounter) return ((IConceptCounter) idx);
return null;
}
int countConcepts(Class c, Object... params) {
int n = countConcepts_noParent(c, params);
if (parent == null) return n;
return n+parent.countConcepts(c, params);
}
int countConcepts_noParent(Class c, Object... params) {
ping();
if (empty(params)) {
IConceptCounter counter = conceptCounterForClass(c);
if (counter != null) return counter.countConcepts();
return l(list_noParent(c));
}
int n = 0;
for (A x : list_noParent(c)) if (checkConceptFields(x, params)) ++n;
return n;
}
int countConcepts(String c, Object... params) {
ping();
if (empty(params)) return l(list(c));
int n = 0;
for (Concept x : list(c)) if (checkConceptFields(x, params)) ++n;
return n;
}
int countConcepts() {
return l(concepts);
}
synchronized List clonedConceptIndices() {
return cloneList(conceptIndices);
}
synchronized void addConceptIndex(IConceptIndex index) {
if (conceptIndices == null)
conceptIndices = new ArrayList();
conceptIndices.add(index);
}
synchronized void removeConceptIndex(IConceptIndex index) {
if (conceptIndices == null) return;
conceptIndices.remove(index);
if (empty(conceptIndices)) conceptIndices = null;
}
synchronized void addFieldIndex(Class extends Concept> c, String field, IFieldIndex index) {
if (fieldIndices == null)
fieldIndices = new HashMap();
Map map = fieldIndices.get(c);
if (map == null)
fieldIndices.put(c, map = new HashMap());
map.put(field, index);
}
synchronized void removeFieldIndex(Class extends Concept> c, String field, IFieldIndex index) {
Map map = mapGet(fieldIndices, c);
mapRemove(map, field);
}
synchronized IFieldIndex getFieldIndex(Class extends Concept> c, String field) {
if (fieldIndices == null) return null;
Map map = fieldIndices.get(c);
return map == null ? null : map.get(field);
}
synchronized IFieldIndex getAnyIndexForClass(Class extends Concept> c) {
return first(allIndicesForClass(c));
}
synchronized Collection allIndicesForClass(Class extends Concept> c) {
return concatLists(
values(fieldIndices == null ? null : fieldIndices.get(c)),
values(ciFieldIndices == null ? null : ciFieldIndices.get(c)));
}
synchronized void addCIFieldIndex(Class extends Concept> c, String field, IFieldIndex index) {
if (ciFieldIndices == null)
ciFieldIndices = new HashMap();
Map map = ciFieldIndices.get(c);
if (map == null)
ciFieldIndices.put(c, map = new HashMap());
map.put(field, index);
}
synchronized void removeCIFieldIndex(Class extends Concept> c, String field) {
Map map = mapGet(ciFieldIndices, c);
mapRemove(map, field);
}
synchronized IFieldIndex getCIFieldIndex(Class extends Concept> c, String field) {
if (ciFieldIndices == null) return null;
Map map = ciFieldIndices.get(c);
return map == null ? null : map.get(field);
}
// inter-process methods
RC xnew(String name, Object... values) {
return new RC(cnew(name, values));
}
void xset(long id, String field, Object value) {
xset(new RC(id), field, value);
}
void xset(RC c, String field, Object value) {
if (value instanceof RC)
value = getConcept((RC) value);
cset(getConcept(c), field, value);
}
Object xget(long id, String field) {
return xget(new RC(id), field);
}
Object xget(RC c, String field) {
return xgetPost(cget(getConcept(c), field));
}
Object xgetPost(Object o) {
o = deref(o);
if (o instanceof Concept)
return new RC((Concept) o);
return o;
}
void xdelete(long id) {
xdelete(new RC(id));
}
void xdelete(RC c) {
getConcept(c).delete();
}
void xdelete(List l) {
for (RC c : l)
xdelete(c);
}
List xlist() {
return map("toPassRef", allConcepts());
}
List xlist(String className) {
return map("toPassRef", conceptsOfType(className));
}
boolean isTransient() { return eq(programID, "-"); }
String xfullgrab() {
if (noXFullGrab) throw fail("no xfullgrab (DB too large)");
Lock __3 = lock(); lock(__3); try {
if (changes == changesWritten && !isTransient())
return loadConceptsStructure(programID);
return fullStructure();
} finally { unlock(__3); } }
/* dev.
Either xfullgrabGZipped() {
lock lock();
if (changes == changesWritten && !isTransient())
ret loadConceptsStructure(programID);
ret fullStructure();
}*/
void xshutdown() {
// Killing whole VM if someone wants this DB to shut down
cleanKillVM();
}
long xchangeCount() { return changes; }
int xcount() { return countConcepts(); }
void register(Concept c) {
ping();
if (c._concepts == this) return;
if (c._concepts != null) throw fail("Can't re-register");
c.id = internalID();
c.created = now();
if (modifyOnCreate) c._setModified(c.created);
register_phase2(c);
vmBus_send("conceptCreated", c);
fireChange(new ConceptCreate(c));
}
// also called by replaceConceptAndUpdateRefs
void register_phase2(Concept c) {
c._concepts = this;
concepts.put((long) c.id, c);
for (Concept.Ref r : c._refs())
r.index();
c.change();
c._onRegistered();
}
void registerKeepingID(Concept c) {
if (c._concepts == this) return;
if (c._concepts != null) throw fail("Can't re-register");
c._concepts = this;
concepts.put((long) c.id, c);
c.change();
}
void conceptChanged(Concept c) {
fireChange(new ConceptChange(c));
if (conceptIndices != null)
for (IConceptIndex index : clonedConceptIndices())
index.update(c);
}
boolean hasUnsavedData() {
return changes != changesWritten || savingConcepts;
}
synchronized Object miscMapGet(Object key) {
return mapGet(miscMap, key);
}
synchronized Object miscMapPut(Object key, Object value) {
if (miscMap == null) miscMap = new HashMap();
return miscMap.put(key, value);
}
synchronized void miscMapRemove(Object key) {
mapRemove(miscMap, key);
}
// Note: auto-typing can fool you, make sure create returns
// a wide enough type
synchronized A miscMapGetOrCreate(Object key, IF0 create) {
if (containsKey(miscMap, key)) return (A) miscMap.get(key);
A value = create.get();
miscMapPut(key, value);
return value;
}
void setParent(Concepts parent) {
this.parent = parent;
}
void fireChange(ConceptsChange change) {
if (change == null) return;
pcallFAll(onChange, change);
fireLegacyChangeEvent();
}
final void onChange(IVF1 l){ addChangeListener(l); }
void addChangeListener(IVF1 l) {
syncAdd(onChange, l);
}
void removeChangeListener(IVF1 l) {
syncRemove(onChange, l);
}
void addPreSave(Runnable r) {
preSave = syncAddOrCreate(preSave, r);
}
public String toString() {
return nConcepts(concepts) + " (" + conceptsDir() + ", hash: " + identityHashCode(this) + ")";
}
} // end of Concepts
// TODO: Move everyone over to this so we can have concepts that
// don't subclass Concept. It's hard though because of Concept.Ref
interface IConcept {
public long _conceptID();
public Concepts concepts();
}
static class Concept extends DynamicObject implements IConcept, ChangeTriggerable {
transient Concepts _concepts; // Where we belong
long id;
long created, _modified;
List[ backRefs;
// used only internally (cnew)
Concept(String className) {
super(className);
_created();
}
Concept() {
if (!_loading()) {
//className = shortClassName(this); // XXX - necessary?
//print("New concept of type " + className);
_created();
}
}
Concept(boolean unlisted) {
if (!unlisted) _created();
}
boolean includeZeroIDInToString() { return false; }
public String toString() {
String s = shortDynamicClassName(this);
long id = this.id;
if (id != 0 || includeZeroIDInToString())
s += " " + id;
return s;
}
static boolean loading() { return _loading(); }
static boolean _loading() { return dynamicObjectIsLoading(); }
void _created() {
if (!concepts_unlistedByDefault && !eq(concepts_unlisted.get(), true))
db_mainConcepts().register(this);
}
// base class + required interface. experimental
class TypedRef] extends Ref {
TypedRef() {}
//Class aType;
Class bType;
TypedRef(Class bType) {
this.bType = bType;}
TypedRef(Class bType, B value) {
this.bType = bType; set((A) value); }
TypedRef(B value) { set((A) value); }
public boolean set(A a) {
return super.set(checkValue(a));
}
void check() { checkValue(get()); }
C checkValue(C a) {
if (bType != null && a != null)
assertIsInstance(a, bType);
return a;
}
B b() { return (B) value; }
}
class Ref implements IRef {
A value;
Ref() {
if (!dynamicObjectIsLoading())
registerRef();
}
void registerRef() {
vmBus_send("registeringConceptRef", this);
}
Ref(A value) {
this.value = value;
registerRef();
index();
}
// get owning concept (source)
Concept concept() {
return Concept.this;
}
// get target
public A get() { return value; }
public boolean has() { return value != null; }
boolean set(A a) {
if (a == value) return false;
unindex();
value = a;
index();
change();
return true;
}
void setIfEmpty(A a) {
if (!has()) set(a);
}
public void set(Ref ref) { set(ref.get()); }
public void clear() { set((A) null); }
boolean validRef() {
return value != null && _concepts != null && _concepts == value._concepts;
}
// TODO: sync all the indexing and unindexing!?
void index() {
if (validRef()) {
value._addBackRef(this);
change();
}
}
Ref unindex() {
if (validRef()) {
value._removeBackRef(this);
change();
}
return this;
}
void unindexAndDrop() {
unindex();
_removeRef(this);
}
void change() {
Concept.this.change();
}
public String toString() { return
str(value); }
}
class RefL extends AbstractList {
List[> l = new ArrayList();
RefL() {}
RefL(List] l) { replaceWithList(l); }
public void clear() {
while (!isEmpty()) removeLast(this);
}
public void replaceWithList(List l) {
clear();
for (A a : unnullForIteration(l)) add(a);
}
public A set(int i, A o) {
Ref ref = syncGet(l, i);
A prev = ref.get();
ref.set(o);
return prev;
}
public void add(int i, A o) {
syncAdd(l, i, new Ref(o));
}
public A get(int i) {
return syncGet(l, i).get();
}
public A remove(int i) {
return syncRemove(l, i).get();
}
public int size() {
return syncL(l);
}
public boolean contains(Object o) {
if (o instanceof Concept)
for (Ref r : l) if (eq(r.get(), o)) return true;
return super.contains(o);
}
}
void delete() {
//name = "[defunct " + name + "]";
//defunct = true;
//energy = 0;
// clean refs
for (Ref r : unnullForIteration(_refs()))
r.unindex();
// set back refs to null
for (Ref r : cloneList(backRefs))
r.set((Concept) null);
backRefs = null;
var _concepts = this._concepts;
if (_concepts != null) {
_concepts.concepts.remove(id);
_concepts.fireChange(new ConceptDelete(id, this));
if (_concepts.conceptIndices != null)
for (IConceptIndex index : _concepts.conceptIndices)
index.remove(this);
this._concepts = null;
}
id = 0;
}
BaseXRef export() {
return new BaseXRef(_concepts.progID(), id);
}
// notice system of a change in this object
final public void _change(){ change(); }
public void change() {
_setModified(now());
_change_withoutUpdatingModifiedField();
}
void _setModified(long modified) {
_modified = modified;
}
final void _change_withoutUpdatingModifiedField() {
_onChange();
if (_concepts != null) _concepts.conceptChanged(this);
}
// overridable
void _onChange() {}
String _programID() {
return _concepts == null ? getDBProgramID() : _concepts.progID();
}
// overridable
void _addBackRef(Concept.Ref ref) {
backRefs = addDyn_quickSync(backRefs, ref);
_backRefsModified();
}
void _backRefsModified() {
if (_concepts != null && _concepts.modifyOnBackRef) change();
}
void _removeBackRef(Concept.Ref ref) {
backRefs = removeDyn_quickSync(backRefs, ref);
_backRefsModified();
}
void _removeRef(Concept.Ref ref) {
}
int _backRefCount() { return syncL(backRefs); }
// convenience methods
final void setField(String field, Object value){ _setField(field, value); }
void _setField(String field, Object value) {
cset(this, field, value);
}
boolean setField_trueIfChanged(String field, Object value) {
return cset(this, field, value) != 0;
}
A setFieldAndReturn(String field, A value) {
setField(field, value);
return value;
}
final void setFields(Object... values){ _setFields(values); }
void _setFields(Object... values) {
cset(this, values);
}
public Concepts concepts() { return _concepts; }
boolean isDeleted() { return id == 0; }
// Called directly after concept is unstructured
void _doneLoading() {}
// Called after all concepts are unstructured and added to Concepts
void _doneLoading2() {
Map map = _fieldMigrations();
if (map != null) for (Map.Entry extends String, ? extends FieldMigration> __2 : _entrySet( map))
{ String oldField = __2.getKey(); FieldMigration m = __2.getValue(); crenameField_noOverwrite(this, oldField, m.newField); }
}
static class FieldMigration implements IFieldsToList{
String newField;
FieldMigration() {}
FieldMigration(String newField) {
this.newField = newField;}
public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + newField + ")"; }
public boolean equals(Object o) {
if (!(o instanceof FieldMigration)) return false;
FieldMigration __8 = (FieldMigration) o;
return eq(newField, __8.newField);
}
public int hashCode() {
int h = 558692372;
h = boostHashCombine(h, _hashCode(newField));
return h;
}
public Object[] _fieldsToList() { return new Object[] {newField}; }
}
// value is
Map _fieldMigrations() { return null; }
// new wrapper to get a copy of the refs list
// so we can eventually drop the refs field
Collection[ _refs() {
return scanConceptForRefs(this);
}
Concepts _concepts() { return _concepts; }
boolean _conceptsDefunct() { return _concepts != null && _concepts.defunct; }
boolean _conceptsDefunctOrUnregistered() { return _concepts == null || _concepts.defunct; }
// allow refs to do magic stuff?
void _onRegistered() {
/*for (Ref ref : _refs())
refs._onRegistered();*/
}
] boolean addAndChange(Collection cl, A a) {
if (cl == null || !cl.add(a)) return false;
change();
return true;
}
void clearAndChange(Collection cl) {
if (cl == null) return;
cl.clear();
change();
}
File conceptsDir() {
var concepts = concepts();
return concepts == null ? null : concepts.conceptsDir();
}
File fileInConceptsDir(String name) {
var dir = conceptsDir();
return dir == null ? null : newFile(dir, name);
}
public long _conceptID() { return id; }
} // end of Concept
// remote reference (for inter-process communication or
// external databases). Formerly "PassRef".
// prepared for string ids if we do them later
static class RC {
transient Object owner;
String id;
RC() {} // make serialisation happy
RC(long id) { this.id = str(id); }
RC(Object owner, long id) { this.id = str(id); this.owner = owner; }
RC(Concept c) { this(c.id); }
long longID() { return parseLong(id); }
public String toString() {
return id;
}
transient RemoteDB db;
String getString(String field) { return db.xS(this, field); }
Object get(String field) { return db.xget(this, field); }
void set(String field, Object value) { db.xset(this, field, value); }
} // end of RC
// Reference to a concept in another program
static class BaseXRef {
String programID;
long id;
BaseXRef() {}
BaseXRef(String programID, long id) {
this.id = id;
this.programID = programID;}
public boolean equals(Object o) {
if (!(o instanceof BaseXRef)) return false;
BaseXRef r = (BaseXRef) o;
return eq(programID, r.programID) && eq(id, r.id);
}
public int hashCode() {
return programID.hashCode() + (int) id;
}
}
// BaseXRef as a concept
static class XRef extends Concept {
BaseXRef ref;
XRef() {}
XRef(BaseXRef ref) {
this.ref = ref; _doneLoading2(); }
// after we have been added to concepts
void _doneLoading2() {
getIndex().put(ref, this);
}
HashMap getIndex() {
return getXRefIndex(_concepts);
}
}
static synchronized HashMap getXRefIndex(Concepts concepts) {
HashMap cache = (HashMap) concepts.perClassData().get(XRef.class);
if (cache == null)
concepts.perClassData.put(XRef.class, cache = new HashMap());
return cache;
}
// uses mainConcepts
static XRef lookupOrCreateXRef(BaseXRef ref) {
XRef xref = getXRefIndex(db_mainConcepts()).get(ref);
if (xref == null)
xref = new XRef(ref);
return xref;
}
// define standard concept functions to use main concepts
// Now in db_mainConcepts()
/*static void cleanMeUp_concepts() {
if (db_mainConcepts() != null) db_mainConcepts().cleanMeUp();
// mainConcepts = null; // TODO
}*/
static void loadAndAutoSaveConcepts() {
db_mainConcepts().persist();
}
static void loadAndAutoSaveConcepts(int interval) {
db_mainConcepts().persist(interval);
}
static RC toPassRef(Concept c) {
return new RC(c);
}
// so we can instantiate the program to run as a bare DB bot
static void concepts_setUnlistedByDefault(boolean b) {
concepts_unlistedByDefault = b;
}
static Lock dbLock() {
return db_mainConcepts().lock;
}
static Lock dbLock(Concepts cc) {
return cc == null ? null : cc.lock;
}
static Lock dbLock(Concept c) {
return dbLock(c == null ? null : c._concepts);
}
static boolean bareDBMode_on = false;
static void bareDBMode() {
bareDBMode(null); // default autoSaveInterval
}
static void bareDBMode(Integer autoSaveInterval) {
bareDBMode_on = true;
conceptsAndBot(autoSaveInterval);
}
static ThreadLocal DynamicObject_loading = or((ThreadLocal) get(getClass("x30_pkg.x30_util"), "DynamicObject_loading"), new ThreadLocal());
static ThreadLocal dynamicObjectIsLoading_threadLocal() {
return DynamicObject_loading;
}
static List getAll(Map map, Collection l) {
return lookupAllOpt(map, l);
}
static List getAll(Collection l, Map map) {
return lookupAllOpt(map, l);
}
static > List getAll(Iterable l) {
return getVars(l);
}
static > List allValues(Map map) {
List out = new ArrayList();
for (var l : values(map))
addAll(out, l);
return out;
}
static String shortClassName_dropNumberPrefix(Object o) {
return dropNumberPrefix(shortClassName(o));
}
static boolean eq(Object a, Object b) {
return a == b || a != null && b != null && a.equals(b);
}
// a little kludge for stuff like eq(symbol, "$X")
static boolean eq(Symbol a, String b) {
return eq(str(a), b);
}
static int boostHashCombine(int a, int b) {
return a ^ (b + 0x9e3779b9 + (a << 6) + (a >>> 2));
// OLD (changed) 2022/3/10: ret a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2));
}
static int _hashCode(Object a) {
return a == null ? 0 : a.hashCode();
}
static NavigableMap synchroTreeMap() {
return synchroNavigableMap(new TreeMap());
}
static Object _defaultClassFinder_value = defaultDefaultClassFinder();
static Object _defaultClassFinder() {
return _defaultClassFinder_value;
}
static List synchroList() {
return synchroList(new ArrayList ());
}
static List synchroList(List l) {
return Collections.synchronizedList(l);
}
static Set createOrAddToSyncLinkedHashSet(Set set, A a) {
if (set == null) set = syncLinkedHashSet();
set.add(a);
return set;
}
static void remove(List l, int i) {
if (l != null && i >= 0 && i < l(l))
l.remove(i);
}
static void remove(Collection l, A a) {
if (l != null) l.remove(a);
}
static B remove(Map map, Object a) {
return map == null ? null : map.remove(a);
}
static void remove(BitSet bs, int i) {
bs.clear(i);
}
static A pcallF_typed(F0 f) {
try { return f == null ? null : f.get(); } catch (Throwable __e) { pcallFail(__e); } return null;
}
static B pcallF_typed(F1 f, A a) {
try { return f == null ? null : f.get(a); } catch (Throwable __e) { pcallFail(__e); } return null;
}
static void pcallF_typed(VF1 f, A a) {
try {
{ if (f != null) f.get(a); }
} catch (Throwable __e) { pcallFail(__e); }
}
static void pcallF_typed(IVF1 f, A a) {
try {
{ if (f != null) f.get(a); }
} catch (Throwable __e) { pcallFail(__e); }
}
static void pcallF_typed(IVF2 f, A a, B b) {
try {
{ if (f != null) f.get(a, b); }
} catch (Throwable __e) { pcallFail(__e); }
}
static Object pcallF_typed(Runnable r) {
try { { if (r != null) r.run(); } } catch (Throwable __e) { pcallFail(__e); } return null;
}
static A pcallF_typed(IF0 f) {
try { return f == null ? null : f.get(); } catch (Throwable __e) { pcallFail(__e); } return null;
}
static B pcallF_typed(IF1 f, A a) {
try { return f == null ? null : f.get(a); } catch (Throwable __e) { pcallFail(__e); } return null;
}
static boolean hasConcept(Class extends Concept> c, Object... params) { return hasConcept(db_mainConcepts(), c, params); }
static boolean hasConcept(Concepts cc, Class extends Concept> c, Object... params) {
return findConceptWhere(cc, c, params) != null;
}
static String getDBProgramID_id;
static String getDBProgramID() {
return nempty(getDBProgramID_id) ? getDBProgramID_id : programIDWithCase();
}
static Object load(String varName) {
readLocally(varName);
return get(mc(), varName);
}
static Object load(String progID, String varName) {
readLocally(progID, varName);
return get(mc(), varName);
}
static void clearConcepts() {
db_mainConcepts().clearConcepts();
}
static void clearConcepts(Concepts concepts) {
concepts.clearConcepts();
}
static Map unstructureMap(String s) {
return (Map) unstructure(s);
}
static Map unstructureMap(String text, boolean allDynamic, Object classFinder) {
return (Map) unstructure(text, allDynamic, classFinder);
}
static boolean isFalse(Object o) {
return eq(false, o);
}
static Map> callF_cache = newDangerousWeakHashMap();
static A callF(F0 f) {
return f == null ? null : f.get();
}
static B callF(F1 f, A a) {
return f == null ? null : f.get(a);
}
static A callF(IF0 f) {
return f == null ? null : f.get();
}
static B callF(IF1 f, A a) {
return f == null ? null : f.get(a);
}
static B callF(A a, IF1 f) {
return f == null ? null : f.get(a);
}
static C callF(IF2 f, A a, B b) {
return f == null ? null : f.get(a, b);
}
static void callF(VF1 f, A a) {
if (f != null) f.get(a);
}
static void callF(A a, IVF1 f) {
if (f != null) f.get(a);
}
static void callF(IVF1 f, A a) {
if (f != null) f.get(a);
}
static Object callF(Runnable r) { { if (r != null) r.run(); } return null; }
static Object callF(Object f, Object... args) {
return safeCallF(f, args);
}
static Object safeCallF(Object f, Object... args) {
if (f instanceof Runnable) {
((Runnable) f).run();
return null;
}
if (f == null) return null;
Class c = f.getClass();
ArrayList methods;
synchronized(callF_cache) {
methods = callF_cache.get(c);
if (methods == null)
methods = callF_makeCache(c);
}
int n = l(methods);
if (n == 0) {
if (f instanceof String)
throw fail("Legacy call: " + f);
throw fail("No get method in " + getClassName(c));
}
if (n == 1) return invokeMethod(methods.get(0), f, args);
for (int i = 0; i < n; i++) {
Method m = methods.get(i);
if (call_checkArgs(m, args, false))
return invokeMethod(m, f, args);
}
throw fail("No matching get method in " + getClassName(c));
}
// used internally
static ArrayList callF_makeCache(Class c) {
ArrayList l = new ArrayList();
Class _c = c;
do {
for (Method m : _c.getDeclaredMethods())
if (m.getName().equals("get")) {
makeAccessible(m);
l.add(m);
}
if (!l.isEmpty()) break;
_c = _c.getSuperclass();
} while (_c != null);
callF_cache.put(c, l);
return l;
}
static boolean exceptionMessageContains(Throwable e, String s) {
return cic(getInnerMessage(e), s);
}
static void printShortException(Throwable e) {
print(exceptionToStringShort(e));
}
static void printShortException(String s, Throwable e) {
print(s, exceptionToStringShort(e));
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static boolean printAlsoToSystemOut = true;
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal print_byThread; // special handling by thread - prefers F1
static volatile Object print_allThreads;
static volatile Object print_preprocess;
static void print() {
print("");
}
static A print(String s, A o) {
print(combinePrintParameters(s, o));
return o;
}
// slightly overblown signature to return original object...
static A print(A o) {
ping_okInCleanUp();
if (print_silent) return o;
String s = o + "\n";
print_noNewLine(s);
return o;
}
static void print_noNewLine(String s) {
try {
Object f = getThreadLocal(print_byThread_dontCreate());
if (f == null) f = print_allThreads;
if (f != null)
// We do need the general callF machinery here as print_byThread is sometimes shared between modules
if (isFalse(
f instanceof F1 ? ((F1) f).get(s) :
callF(f, s))) return;
} catch (Throwable e) {
System.out.println(getStackTrace(e));
}
print_raw(s);
}
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);
if (printAlsoToSystemOut)
System.out.print(s);
vmBus_send("printed", mc(), s);
}
static void print_autoRotate() {
}
static boolean nempty(Collection c) {
return !empty(c);
}
static boolean nempty(CharSequence s) {
return !empty(s);
}
static boolean nempty(Object[] o) { return !empty(o); }
static boolean nempty(byte[] o) { return !empty(o); }
static boolean nempty(int[] o) { return !empty(o); }
static boolean nempty(BitSet bs) { return !empty(bs); }
static boolean nempty(Map m) {
return !empty(m);
}
static boolean nempty(Iterator i) {
return i != null && i.hasNext();
}
static boolean nempty(IMultiMap mm) { return mm != null && mm.size() != 0; }
static boolean nempty(Object o) { return !empty(o); }
static boolean nempty(Rect r) { return r != null && r.w != 0 && r.h != 0; }
static boolean nempty(MultiSet ms) { return ms != null && !ms.isEmpty(); }
static boolean nempty(IntSize l) { return l != null && l.size() != 0; }
static long now_virtualTime;
static long now() {
return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis();
}
static Object unstructureGZFile(File f) { return unstructureGZFile(f, null); }
static Object unstructureGZFile(File f, IF1 classFinder) { try {
if (!fileExists(f)) return null;
BufferedReader reader = utf8BufferedReader(gzInputStream(f)); try {
return unstructure_tok(javaTokC_noMLS_onReader(reader), false, classFinder);
} finally { _close(reader); }} catch (Exception __e) { throw rethrow(__e); } }
static File conceptsFile(String progID) {
return getProgramFile(progID, conceptsFileName());
}
static File conceptsFile() {
return conceptsFile(dbProgramID());
}
static File conceptsFile(Concepts concepts) {
return concepts.conceptsFile();
}
static IF1 toIF1(final Object f) {
if (f == null) return null;
if (f instanceof IF1) return (IF1) f;
if (isString(f)) {
throw fail("callF_legacy");
}
return new IF1() {
public Object get(Object a) { return callF(f, a); }
};
}
static Map putAll(Map a, Map extends A,? extends B> b) {
if (a != null && b != null) a.putAll(b);
return a;
}
static MultiMap putAll(MultiMap a, Map extends A,? extends B> b) {
if (a != null) a.putAll((Map) b);
return a;
}
static Map putAll(Map a, Object... b) {
if (a != null)
litmap_impl(a, b);
return a;
}
static int done_minPrint = 10;
static long done(long startTime, String desc) {
long time = now()-startTime;
if (time >= done_minPrint)
print(desc + " [" + time + " ms]");
return time;
}
static long done(String desc, long startTime) {
return done(startTime, desc);
}
static long done(long startTime) {
return done(startTime, "");
}
static String n2(long l) { return formatWithThousands(l); }
static String n2(AtomicLong l) { return n2(l.get()); }
static String n2(Collection l) { return n2(l(l)); }
static String n2(Map map) { return n2(l(map)); }
static String n2_getPlural(String singular) {
//ret singular + "s";
return plural(singular);
}
static String n2(double l, String singular) {
return empty(singular) ? str(l) : n2(l, singular, n2_getPlural(singular));
}
static String n2(double l, String singular, String plural) {
if (fraction(l) == 0)
return n2((long) l, singular, plural);
else
return l + " " + plural;
}
static String n2(long l, String singular, String plural) {
return n_fancy2(l, singular, plural);
}
static String n2(long l, String singular) {
return empty(singular) ? n2(l) : n_fancy2(l, singular, n2_getPlural(singular));
}
static String n2(Collection l, String singular) {
return n2(l(l), singular);
}
static String n2(Collection l, String singular, String plural) {
return n_fancy2(l, singular, plural);
}
static String n2(Map m, String singular, String plural) {
return n_fancy2(m, singular, plural);
}
static String n2(Map m, String singular) {
return n2(l(m), singular);
}
static String n2(long[] a, String singular) { return n2(l(a), singular); }
static String n2(Object[] a, String singular) { return n2(l(a), singular); }
static String n2(Object[] a, String singular, String plural) { return n_fancy2(a, singular, plural); }
static String n2(MultiSet ms, String singular) { return n2(ms, singular, n2_getPlural(singular)); }
static String n2(MultiSet ms, String singular, String plural) {
return n_fancy2(ms, singular, plural);
}
static String n2(IMultiMap mm, String singular) { return n2(mm, singular, n2_getPlural(singular)); }
static String n2(IMultiMap mm, String singular, String plural) {
return n_fancy2(l(mm), singular, plural);
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Iterator i) { return iteratorCount_int_close(i); } // consumes the iterator && closes it if possible
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); }
static long l(File f) { return f == null ? 0 : f.length(); }
static int l(IMultiSet ms) { return ms == null ? 0 : ms.size(); }
static int l(IMultiMap mm) { return mm == null ? 0 : mm.size(); }
static int l(IntSize o) { return o == null ? 0 : o.size(); }
static boolean sameSnippetID(String a, String b) {
if (!isSnippetID(a) || !isSnippetID(b)) return false;
return parseSnippetID(a) == parseSnippetID(b);
}
static RemoteDB connectToDBOpt(String dbNameOrID) { try {
return new RemoteDB(dbNameOrID);
} catch (Throwable __e) { return null; } }
static void _close(AutoCloseable c) {
if (c != null) try {
c.close();
} catch (Throwable e) {
// Some classes stupidly throw an exception on double-closing
if (c instanceof javax.imageio.stream.ImageOutputStream)
return;
else throw rethrow(e);
}
}
// TODO: cyclic structures involving certain lists & sets
static Object unstructure(String text) {
return unstructure(text, false);
}
static Object unstructure(String text, boolean allDynamic) {
return unstructure(text, allDynamic, null);
}
static Object unstructure(String text, IF1 classFinder) {
return unstructure(text, false, classFinder);
}
static int structure_internStringsLongerThan = 50;
static int unstructure_unquoteBufSize = 100;
static int unstructure_tokrefs; // stats
abstract static class unstructure_Receiver {
abstract void set(Object o);
}
// classFinder: func(name) -> class (optional)
static Object unstructure(String text, boolean allDynamic,
Object classFinder) {
if (text == null) return null;
return unstructure_tok(javaTokC_noMLS_iterator(text), allDynamic, classFinder);
}
static Object unstructure_reader(BufferedReader reader) {
return unstructure_tok(javaTokC_noMLS_onReader(reader), false, null);
}
interface unstructure_Handler {
void parse(int refID, int tokIndex, unstructure_Receiver out);
}
static class Unstructurer {
final public Unstructurer setTok(Producer tok){ return tok(tok); }
public Unstructurer tok(Producer tok) { this.tok = tok; return this; } final public Producer getTok(){ return tok(); }
public Producer tok() { return tok; }
Producer tok;
final public Unstructurer setAllDynamic(boolean allDynamic){ return allDynamic(allDynamic); }
public Unstructurer allDynamic(boolean allDynamic) { this.allDynamic = allDynamic; return this; } final public boolean getAllDynamic(){ return allDynamic(); }
public boolean allDynamic() { return allDynamic; }
boolean allDynamic = false;
int i = -1;
Object classFinder;
String mcDollar = actualMCDollar();
Unstructurer classFinder(Object _classFinder) {
classFinder = _classFinder != null ? _classFinder : _defaultClassFinder();
return this;
}
// use Eclipse primitive collection if possible (smaller & hopefully faster?)
HashMap refs = new HashMap();
HashMap tokrefs = new HashMap();
HashSet concepts = new HashSet();
List stack = new ArrayList();
Map baseClassMap = new HashMap();
HashMap innerClassConstructors = new HashMap();
String curT;
char[] unquoteBuf = new char[unstructure_unquoteBufSize];
// value is a class or a Handler
final HashMap handlers = new HashMap();
Unstructurer() {
try {
Class mc = (Class) (callF(classFinder, ""));
if (mc != null) mcDollar = mc.getName() + "$";
} catch (Throwable __e) { pcallFail(__e); }
makeHandlers();
}
void makeHandlers() {
unstructure_Handler h;
handlers.put("bigint", (unstructure_Handler) (refID, tokIndex, out)
-> out.set(parseBigInt()));
handlers.put("d", (unstructure_Handler) (refID, tokIndex, out)
-> out.set(parseDouble()));
handlers.put("fl", (unstructure_Handler) (refID, tokIndex, out)
-> out.set(parseFloat()));
handlers.put("sh", (unstructure_Handler) (refID, tokIndex, out) -> {
consume();
String t = tpp();
if (t.equals("-")) {
t = tpp();
out.set((short) (-parseInt(t))); return;
}
out.set((short) parseInt(t));
});
handlers.put("enum", (unstructure_Handler) (refID, tokIndex, out) -> {
consume();
String t = tpp();
assertTrue(isJavaIdentifier(t));
String fullClassName = mcDollar + t;
Class _c = findAClass(fullClassName);
if (_c == null) throw fail("Enum class not found: " + fullClassName);
int ordinal = parseInt(tpp());
out.set(_c.getEnumConstants()[ordinal]);
});
handlers.put("false", h = (unstructure_Handler) (refID, tokIndex, out) -> {
consume(); out.set(false);
});
handlers.put("f", h);
handlers.put("true", h = (unstructure_Handler) (refID, tokIndex, out) -> {
consume(); out.set(true);
});
handlers.put("t", h);
handlers.put("{", (unstructure_Handler) (refID, tokIndex, out) -> parseMap(out));
handlers.put("[", (unstructure_Handler) (refID, tokIndex, out) -> {
ArrayList l = new ArrayList();
if (refID >= 0) refs.put(refID, l);
this.parseList(l, out);
});
handlers.put("bitset", (unstructure_Handler) (refID, tokIndex, out) -> parseBitSet(out));
handlers.put("array", h = (unstructure_Handler) (refID, tokIndex, out) -> parseArray(out));
handlers.put("intarray", h);
handlers.put("dblarray", h);
handlers.put("floatarray", h);
handlers.put("shortarray", (unstructure_Handler) (refID, tokIndex, out) -> {
consume();
String hex = trivialUnquote(tpp());
out.set(shortArrayFromBytes(hexToBytes(hex)));
});
handlers.put("longarray", (unstructure_Handler) (refID, tokIndex, out) -> {
consume();
String hex = trivialUnquote(tpp());
out.set(longArrayFromBytes(hexToBytes(hex)));
});
} // end of makeHandlers - add more handlers here
Class findAClass(String fullClassName) { try {
return classFinder != null ? (Class) callF(classFinder, fullClassName) : findClass_fullName(fullClassName);
} catch (Throwable __e) { return null; } }
String unquote(String s) {
return unquoteUsingCharArray(s, unquoteBuf);
}
// look at current token
String t() {
return curT;
}
// get current token, move to next
String tpp() {
String t = curT;
consume();
return t;
}
void parse(final unstructure_Receiver out) {
String t = t();
int refID;
if (structure_isMarker(t, 0, l(t))) {
refID = parseInt(t.substring(1));
consume();
} else refID = -1;
// if (debug) print("parse: " + quote(t));
final int tokIndex = i;
parse_inner(refID, tokIndex, new unstructure_Receiver() {
void set(Object o) {
if (refID >= 0)
refs.put(refID, o);
if (o != null)
tokrefs.put(tokIndex, o);
out.set(o);
}
});
}
void parse_inner(int refID, int tokIndex, unstructure_Receiver out) {
String t = t();
// if (debug) print("parse_inner: " + quote(t));
Object handler = handlers.get(t);
if (handler instanceof unstructure_Handler)
{ ((unstructure_Handler) handler).parse(refID, tokIndex, out); return; }
Class c = (Class) handler;
if (c == null) {
if (t.startsWith("\"")) {
String s = internIfLongerThan(unquote(tpp()), structure_internStringsLongerThan);
out.set(s); return;
}
if (t.startsWith("'")) {
out.set(unquoteCharacter(tpp())); return;
}
if (t.equals("-")) {
consume();
t = tpp();
out.set(isLongConstant(t) ? (Object) (-parseLong(t)) : (Object) (-parseInt(t))); return;
}
if (isInteger(t) || isLongConstant(t)) {
consume();
//if (debug) print("isLongConstant " + quote(t) + " => " + isLongConstant(t));
if (isLongConstant(t)) {
out.set(parseLong(t)); return;
}
long l = parseLong(t);
boolean isInt = l == (int) l;
out.set(isInt ? (Object) Integer.valueOf((int) l) : (Object) Long.valueOf(l)); return;
}
if (t.equals("-")) {
consume();
t = tpp();
out.set(isLongConstant(t) ? (Object) (-parseLong(t)) : (Object) (-parseInt(t))); return;
}
if (isInteger(t) || isLongConstant(t)) {
consume();
//if (debug) print("isLongConstant " + quote(t) + " => " + isLongConstant(t));
if (isLongConstant(t)) {
out.set(parseLong(t)); return;
}
long l = parseLong(t);
boolean isInt = l == (int) l;
out.set(isInt ? (Object) Integer.valueOf((int) l) : (Object) Long.valueOf(l)); return;
}
if (t.equals("File")) {
consume();
File f = new File(unquote(tpp()));
out.set(f); return;
}
if (t.startsWith("r") && isInteger(t.substring(1))) {
consume();
int ref = Integer.parseInt(t.substring(1));
Object o = refs.get(ref);
if (o == null)
warn("unsatisfied back reference " + ref);
out.set(o); return;
}
if (t.startsWith("t") && isInteger(t.substring(1))) {
consume();
int ref = Integer.parseInt(t.substring(1));
Object o = tokrefs.get(ref);
if (o == null)
warn("unsatisfied token reference " + ref + " at " + tokIndex);
out.set(o); return;
}
if (t.equals("hashset")) { parseHashSet(out); return; }
if (t.equals("lhs")) { parseLinkedHashSet(out); return; }
if (t.equals("treeset")) { parseTreeSet(out); return; }
if (t.equals("ciset")) { parseCISet(out); return; }
if (eqOneOf(t, "hashmap", "hm")) {
consume();
parseMap(new HashMap(), out);
return;
}
if (t.equals("lhm")) {
consume();
parseMap(new LinkedHashMap(), out);
return;
}
if (t.equals("tm")) {
consume();
parseMap(new TreeMap(), out);
return;
}
if (t.equals("cimap")) {
consume();
parseMap(ciMap(), out);
return;
}
if (t.equals("ll")) {
consume();
LinkedList l = new LinkedList();
if (refID >= 0) refs.put(refID, l);
{ parseList(l, out); return; }
}
if (t.equals("syncLL")) { // legacy
consume();
{ parseList(synchroLinkedList(), out); return; }
}
if (t.equals("sync")) {
consume();
{ parse(new unstructure_Receiver() {
void set(Object value) {
if (value instanceof Map) {
// Java 7
if (value instanceof NavigableMap)
{ out.set(synchroNavigableMap((NavigableMap) value)); return; }
if (value instanceof SortedMap)
{ out.set(synchroSortedMap((SortedMap) value)); return; }
{ out.set(synchroMap((Map) value)); return; }
} else
{ out.set(synchroList((List) value)); return; }
}
}); return; }
}
if (t.equals("ba")) {
consume();
String hex = trivialUnquote(tpp());
out.set(hexToBytes(hex)); return;
}
if (t.equals("boolarray")) {
consume();
int n = parseInt(tpp());
String hex = trivialUnquote(tpp());
out.set(boolArrayFromBytes(hexToBytes(hex), n)); return;
}
if (t.equals("class")) {
out.set(parseClass()); return;
}
if (t.equals("l")) {
parseLisp(out); return;
}
if (t.equals("null")) {
consume(); out.set(null); return;
}
if (eq(t, "c")) {
consume();
t = t();
assertTrue(isJavaIdentifier(t));
concepts.add(t);
}
// custom deserialization (new static method method)
if (eq(t, "cu")) {
consume();
t = tpp();
assertTrue(isJavaIdentifier(t));
String fullClassName = mcDollar + t;
Class _c = findAClass(fullClassName);
if (_c == null) throw fail("Class not found: " + fullClassName);
parse(new unstructure_Receiver() {
void set(Object value) {
out.set(call(_c, "_deserialize", value));
}
});
return;
}
}
if (eq(t, "j")) {
consume();
out.set(parseJava()); return;
}
if (eq(t, "bc")) {
consume();
String c1 = tpp();
String c2 = tpp();
baseClassMap.put(c1, c2);
{ parse_inner(refID, i, out); return; }
}
// add more tokens here
// Now we want to find our target class c
// Have we failed to look up the class before?
//bool seenBefore = handlers.containsKey(cname);
// If we have seen the class before, we skip all of this
// and simply leave c as null
// TODO - how do we fill className?
//if (!seenBefore) {
if (c == null && !isJavaIdentifier(t))
throw new RuntimeException("Unknown token " + (i+1) + ": " + quote(t));
// any other class name (or package name)
consume();
String className, fullClassName;
// Is it a package name?
if (eq(t(), ".")) {
className = t;
do {
consume();
className += "." + assertIdentifier(tpp());
} while (eq(t(), "."));
fullClassName = className;
} else {
className = t;
fullClassName = mcDollar + t;
}
if (c == null && !allDynamic) {
// First, find class
c = findAClass(fullClassName);
handlers.put(className, c);
}
// check for existing base class
if (c == null && !allDynamic) {
Set seen = new HashSet();
String parent = className;
while (true) {
String baseName = baseClassMap.get(parent);
if (baseName == null)
break;
if (!seen.add(baseName))
throw fail("Cyclic superclass info: " + baseName);
c = findAClass(mcDollar + baseName);
if (c == null)
print("Base class " + baseName + " of " + parent + " doesn't exist either");
else if (isAbstract(c))
print("Can't instantiate abstract base class: " + c);
else {
printVars_str("Reverting to base class", "className", className, "baseName", baseName, "c", c);
handlers.put(className, c);
break;
}
parent = baseName;
}
}
//}
// Check if it has an outer reference
boolean hasBracket = eq(t(), "(");
if (hasBracket) consume();
boolean hasOuter = hasBracket && startsWith(t(), "this$");
DynamicObject dO = null;
Object o = null;
final String thingName = t;
try {
if (c != null) {
if (hasOuter) try {
Constructor ctor = innerClassConstructors.get(c);
if (ctor == null)
innerClassConstructors.put(c, ctor = nuStubInnerObject_findConstructor(c, classFinder));
o = ctor.newInstance(new Object[] {null});
} catch (Exception e) {
print("Error deserializing " + c + ": " + e);
o = nuEmptyObject(c);
} else
o = nuEmptyObject(c);
if (o instanceof DynamicObject) dO = (DynamicObject) o;
} else {
if (concepts.contains(t) && (c = findAClass(mcDollar + "Concept")) != null)
o = dO = (DynamicObject) nuEmptyObject(c);
else
dO = new DynamicObject();
dO.className = className;
}
} catch (Throwable __e) { pcallFail(__e); } // end of pcall
// Creating instance failed? Use DynamicObject
if (o == null && dO == null)
dO = new DynamicObject();
// Save in references list early because contents of object
// might link back to main object
if (refID >= 0)
refs.put(refID, o != null ? o : dO);
tokrefs.put(tokIndex, o != null ? o : dO);
// NOW parse the fields!
HashMap fields = new HashMap(); // no longer preserving order (why did we do this?)
Object _o = o;
DynamicObject _dO = dO;
if (hasBracket) {
stack.add(new Runnable() { public void run() { try {
if (eq(t(), ",")) consume();
if (eq(t(), ")")) {
consume(")");
objRead(_o, _dO, fields, hasOuter);
out.set(_o != null ? _o : _dO);
} else {
final String key = unquote(tpp());
String t = tpp();
if (!eq(t, "="))
throw fail("= expected, got " + t + " after " + quote(key) + " in object " + thingName /*+ " " + sfu(fields)*/);
stack.add(this);
parse(new unstructure_Receiver() {
void set(Object value) {
fields.put(key, value);
/*ifdef unstructure_debug
print("Got field value " + value + ", next token: " + t());
endifdef*/
//if (eq(t(), ",")) consume();
}
});
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ifdef unstructure_debug\r\n print(\"in object values, token: \" + t());\r..."; }});
} else {
objRead(o, dO, fields, hasOuter);
out.set(o != null ? o : dO);
}
}
void objRead(Object o, DynamicObject dO, Map fields, boolean hasOuter) {
// translate between diferent compilers (this$0 vs this$1)
Object outer = fields.get("this$0");
if (outer != null) fields.put("this$1", outer);
else {
outer = fields.get("this$1");
if (outer != null) fields.put("this$0", outer);
}
if (o != null) {
if (dO != null) {
setOptAllDyn_pcall(dO, fields);
} else {
setOptAll_pcall(o, fields);
}
if (hasOuter)
fixOuterRefs(o);
} else for (Map.Entry e : fields.entrySet())
setDynObjectValue(dO, intern(e.getKey()), e.getValue());
if (o != null)
pcallOpt_noArgs(o, "_doneLoading");
}
void parseSet(final Set set, final unstructure_Receiver out) {
this.parseList(new ArrayList(), new unstructure_Receiver() {
void set(Object o) {
set.addAll((List) o);
out.set(set);
}
});
}
void parseLisp(final unstructure_Receiver out) {
throw fail("class Lisp not included");
}
void parseBitSet(final unstructure_Receiver out) {
consume("bitset");
consume("{");
final BitSet bs = new BitSet();
stack.add(new Runnable() { public void run() { try {
if (eq(t(), "}")) {
consume("}");
out.set(bs);
} else {
stack.add(this);
parse(new unstructure_Receiver() {
void set(Object o) {
bs.set((Integer) o);
if (eq(t(), ",")) consume();
}
});
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \"}\")) {\r\n consume(\"}\");\r\n out.set(bs);\r\n } els..."; }});
}
void parseList(final List list, final unstructure_Receiver out) {
tokrefs.put(i, list);
consume("[");
stack.add(new Runnable() { public void run() { try {
if (eq(t(), "]")) {
consume();
out.set(list);
} else {
stack.add(this);
parse(new unstructure_Receiver() {
void set(Object o) {
//if (debug) print("List element type: " + getClassName(o));
list.add(o);
if (eq(t(), ",")) consume();
}
});
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \"]\")) {\r\n consume();\r\n ifdef unstructure_debug\r\n ..."; }});
}
void parseArray(unstructure_Receiver out) {
String _type = tpp();
int dims;
if (eq(t(), "S")) { // string array
_type = "S";
consume();
}
if (eq(t(), "/")) { // multi-dimensional array
consume();
dims = parseInt(tpp());
} else
dims = 1;
consume("{");
List list = new ArrayList();
String type = _type;
stack.add(new Runnable() { public void run() { try {
if (eq(t(), "}")) {
consume("}");
if (dims > 1) {
Class atype;
if (type.equals("intarray")) atype = int.class;
else if (type.equals("S")) atype = String.class;
else throw todo("multi-dimensional arrays of other types");
out.set(list.toArray((Object[]) newMultiDimensionalOuterArray(atype, dims, l(list))));
} else
out.set(
type.equals("intarray") ? toIntArray(list)
: type.equals("dblarray") ? toDoubleArray(list)
: type.equals("floatarray") ? toFloatArray(list)
: type.equals("S") ? toStringArray(list)
: list.toArray());
} else {
stack.add(this);
parse(new unstructure_Receiver() {
void set(Object o) {
list.add(o);
if (eq(t(), ",")) consume();
}
});
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (eq(t(), \"}\")) {\r\n consume(\"}\");\r\n if (dims > 1) {\r\n ..."; }});
}
Object parseClass() {
consume("class");
consume("(");
String name = unquote(tpp());
consume(")");
Class c = allDynamic ? null : findAClass(name);
if (c != null) return c;
DynamicObject dO = new DynamicObject();
dO.className = "java.lang.Class";
name = dropPrefix(mcDollar, name);
dynamicObject_setRawFieldValue(dO, "name", name);
return dO;
}
Object parseBigInt() {
consume("bigint");
consume("(");
String val = tpp();
if (eq(val, "-"))
val = "-" + tpp();
consume(")");
return new BigInteger(val);
}
Object parseDouble() {
consume("d");
consume("(");
String val = unquote(tpp());
consume(")");
return Double.parseDouble(val);
}
Object parseFloat() {
consume("fl");
String val;
if (eq(t(), "(")) {
consume("(");
val = unquote(tpp());
consume(")");
} else {
val = unquote(tpp());
}
return Float.parseFloat(val);
}
void parseHashSet(unstructure_Receiver out) {
consume("hashset");
parseSet(new HashSet(), out);
}
void parseLinkedHashSet(unstructure_Receiver out) {
consume("lhs");
parseSet(new LinkedHashSet(), out);
}
void parseTreeSet(unstructure_Receiver out) {
consume("treeset");
parseSet(new TreeSet(), out);
}
void parseCISet(unstructure_Receiver out) {
consume("ciset");
parseSet(ciSet(), out);
}
void parseMap(unstructure_Receiver out) {
parseMap(new TreeMap(), out);
}
Object parseJava() {
String j = unquote(tpp());
Matches m = new Matches();
if (jmatch("java.awt.Color[r=*,g=*,b=*]", j, m))
return nuObject("java.awt.Color", parseInt(m.unq(0)), parseInt(m.unq(1)), parseInt(m.unq(2)));
else {
warn("Unknown Java object: " + j);
return null;
}
}
void parseMap(final Map map, final unstructure_Receiver out) {
consume("{");
stack.add(new Runnable() {
boolean v = false;
Object key;
public void run() {
if (v) {
v = false;
stack.add(this);
if (!eq(tpp(), "="))
throw fail("= expected, got " + t() + " in map of size " + l(map));
parse(new unstructure_Receiver() {
void set(Object value) {
map.put(key, value);
if (eq(t(), ",")) consume();
}
});
} else {
if (eq(t(), "}")) {
consume("}");
out.set(map);
} else {
v = true;
stack.add(this);
parse(new unstructure_Receiver() {
void set(Object o) {
key = o;
}
});
}
} // if v else
} // run()
});
}
/*void parseSub(unstructure_Receiver out) {
int n = l(stack);
parse(out);
while (l(stack) > n)
stack
}*/
void consume() { curT = tok.next(); ++i; }
void consume(String s) {
if (!eq(t(), s)) {
/*S prevToken = i-1 >= 0 ? tok.get(i-1) : "";
S nextTokens = join(tok.subList(i, Math.min(i+2, tok.size())));
fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")");*/
throw fail(quote(s) + " expected, got " + quote(t()));
}
consume();
}
// outer wrapper function getting first token and unwinding the stack
void parse_initial(unstructure_Receiver out) {
consume(); // get first token
parse(out);
while (nempty(stack))
popLast(stack).run();
}
}
static Object unstructure_tok(Producer tok, boolean allDynamic, Object classFinder) {
boolean debug = unstructure_debug;
AutoCloseable __1 = tempSetTL(dynamicObjectIsLoading_threadLocal(), true); try {
Var v = new Var();
var unstructurer = new Unstructurer()
.tok(tok)
.allDynamic(allDynamic)
.classFinder(classFinder);
unstructurer.parse_initial(new unstructure_Receiver() {
void set(Object o) { v.set(o); }
});
unstructure_tokrefs = unstructurer.tokrefs.size();
return v.get();
} finally { _close(__1); }}
static boolean unstructure_debug = false;
static int max(int a, int b) { return Math.max(a, b); }
static int max(int a, int b, int c) { return max(max(a, b), c); }
static long max(int a, long b) { return Math.max((long) a, b); }
static long max(long a, long b) { return Math.max(a, b); }
static double max(int a, double b) { return Math.max((double) a, b); }
static float max(float a, float b) { return Math.max(a, b); }
static double max(double a, double b) { return Math.max(a, b); }
static > A max (Iterable l) {
A max = null;
var it = iterator(l);
if (it.hasNext()) {
max = it.next();
while (it.hasNext()) {
A a = it.next();
if (cmp(a, max) > 0)
max = a;
}
}
return max;
}
/*Nah.
static int max(Collection c) {
int x = Integer.MIN_VALUE;
for (int i : c) x = max(x, i);
ret x;
}*/
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;
}
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;
}
static byte max(byte[] c) {
byte x = -128;
for (byte d : c) if (d > x) x = d;
return x;
}
static short max(short[] c) {
short x = -0x8000;
for (short d : c) if (d > x) x = d;
return x;
}
static int max(int[] c) {
int x = Integer.MIN_VALUE;
for (int d : c) if (d > x) x = d;
return x;
}
static > A max(A a, A b) {
return cmp(a, b) >= 0 ? a : b;
}
static List> mapToPairs(Map map) {
List> l = emptyList(l(map));
if (map != null) for (Map.Entry e : map.entrySet())
l.add(pair(e.getKey(), e.getValue()));
return l;
}
static String dynShortName(Object o) {
return shortDynamicClassName(o);
}
static Collection values(Map map) {
return map == null ? emptyList() : map.values();
}
// convenience shortcut for values_gen
static Collection values(Object map) {
return values((Map) map);
}
static Collection values(MultiMap mm) {
return mm == null ? emptyList() : concatLists(values(mm.data));
}
static boolean empty(Collection c) { return c == null || c.isEmpty(); }
static boolean empty(Iterable c) { return c == null || !c.iterator().hasNext(); }
static boolean empty(CharSequence s) { return s == null || s.length() == 0; }
static boolean empty(Map map) { return map == null || map.isEmpty(); }
static boolean empty(Object[] o) { return o == null || o.length == 0; }
static boolean empty(BitSet bs) { return bs == null || bs.isEmpty(); }
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));
}
static boolean empty(Iterator i) { return i == null || !i.hasNext(); }
static boolean empty(double[] a) { return a == null || a.length == 0; }
static boolean empty(float[] a) { return a == null || a.length == 0; }
static boolean empty(int[] a) { return a == null || a.length == 0; }
static boolean empty(long[] a) { return a == null || a.length == 0; }
static boolean empty(byte[] a) { return a == null || a.length == 0; }
static boolean empty(short[] a) { return a == null || a.length == 0; }
static boolean empty(MultiSet ms) { return ms == null || ms.isEmpty(); }
static boolean empty(IMultiMap mm) { return mm == null || mm.size() == 0; }
static boolean empty(File f) { return getFileSize(f) == 0; }
static boolean empty(Rect r) { return !(r != null && r.w != 0 && r.h != 0); }
static boolean empty(Chain c) { return c == null; }
static boolean empty(AppendableChain c) { return c == null; }
static boolean empty(IntSize l) { return l == null || l.size() == 0; }
static Concept getConcept(long id) {
return db_mainConcepts().getConcept(id);
}
static Concept getConcept(Concepts concepts, long id) {
return concepts.getConcept(id);
}
static A getConcept(Class cc, long id) {
return getConcept(db_mainConcepts(), cc, id);
}
static A getConcept(Concepts concepts, Class cc, long id) {
Concept c = concepts.getConcept(id);
if (c == null) return null;
if (!isInstance(cc, c))
throw fail("Can't convert concept: " + getClassName(c) + " -> " + getClassName(cc) + " (" + id + ")");
return (A) c;
}
static long parseLong(String s) {
if (empty(s)) return 0;
return Long.parseLong(dropSuffix("L", s));
}
static long parseLong(Object s) {
return Long.parseLong((String) s);
}
static A lastKey(SortedMap map) {
return empty(map) ? null : map.lastKey();
}
static File conceptsDir() { return conceptsDir(db_mainConcepts()); }
static File conceptsDir(Concepts cc) {
return cc.conceptsDir();
}
static File conceptsDir(String subName) { return conceptsDir(db_mainConcepts(), subName); }
static File conceptsDir(Concepts cc, String subName) {
return newFile(conceptsDir(cc), subName);
}
static File dirOfFile(File f) {
return f == null ? null : f.getParentFile();
}
static File getProgramFile(String progID, String fileName) {
if (new File(fileName).isAbsolute())
return new File(fileName);
return new File(getProgramDir(progID), fileName);
}
static File getProgramFile(String fileName) {
return getProgramFile(getProgramID(), fileName);
}
static File newFile(File base, String... names) {
for (String name : names) base = new File(base, name);
return base;
}
static File newFile(String name) {
return name == null ? null : new File(name);
}
static File newFile(String base, String... names) {
return newFile(newFile(base), names);
}
static void vmBus_send(String msg, Object... args) {
Object arg = vmBus_wrapArgs(args);
pcallFAll_minimalExceptionHandling(vm_busListeners_live(), msg, arg);
pcallFAll_minimalExceptionHandling(vm_busListenersByMessage_live().get(msg), msg, arg);
}
static void vmBus_send(String msg) {
vmBus_send(msg, (Object) null);
}
// wrapper: VF1 or null
static void callRunnableWithWrapper(Object wrapper, Runnable r) {
if (wrapper == null) callF(r);
else callF(wrapper, r);
}
static List callFAll(Collection l, Object... args) {
return callF_all(l, args);
}
static long saveGZStructureToFile(String file, Object o) {
return saveGZStructureToFile(getProgramFile(file), o);
}
// returns number of uncompressed bytes written
static long saveGZStructureToFile(File file, Object o) { return saveGZStructureToFile(file, o, new structure_Data()); }
static long saveGZStructureToFile(File file, Object o, structure_Data data) { try {
File parentFile = file.getParentFile();
if (parentFile != null)
parentFile.mkdirs();
File tempFile = tempFileFor(file);
if (tempFile.exists()) try {
String saveName = tempFile.getPath() + ".saved." + now();
copyFile(tempFile, new File(saveName));
} catch (Throwable e) { printStackTrace(e); }
FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
CountingOutputStream cos;
try {
GZIPOutputStream gos = new GZIPOutputStream(fileOutputStream);
cos = new CountingOutputStream(gos);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(cos, "UTF-8");
PrintWriter printWriter = new PrintWriter(outputStreamWriter);
structureToPrintWriter(o, printWriter, data);
printWriter.close();
gos.close();
fileOutputStream.close();
} catch (Throwable e) {
fileOutputStream.close();
tempFile.delete();
throw rethrow(e);
}
if (file.exists() && !file.delete())
throw new IOException("Can't delete " + file.getPath());
if (!tempFile.renameTo(file))
throw new IOException("Can't rename " + tempFile + " to " + file);
return cos.getFilePointer();
} catch (Exception __e) { throw rethrow(__e); } }
static Map cloneMap(Map map) {
if (map == null) return new HashMap();
// assume mutex is equal to map
synchronized(map) {
return map instanceof TreeMap ? new TreeMap((TreeMap) map) // copies comparator
: map instanceof LinkedHashMap ? new LinkedHashMap(map)
: new HashMap(map);
}
}
static List cloneMap(Iterable l, IF1 f) {
List x = emptyList(l);
if (l != null) for (A o : cloneList(l))
x.add(f.get(o));
return x;
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static long toM(long l) {
return (l+1024*1024-1)/(1024*1024);
}
static String toM(long l, int digits) {
return formatDouble(toM_double(l), digits);
}
/** writes safely (to temp file, then rename) */
static File saveTextFile(String fileName, String contents) throws IOException {
/*ifdef CriticalActions
temp beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)");
endifdef*/
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()); try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
PrintWriter printWriter = new PrintWriter(outputStreamWriter);
printWriter.print(contents);
printWriter.close();
} finally { _close(fileOutputStream); }}
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;
}
static File saveTextFile(File fileName, String contents) { try {
saveTextFile(fileName.getPath(), contents);
return fileName;
} catch (Exception __e) { throw rethrow(__e); } }
static String javaTokWordWrap(String s) {
return javaTokWordWrap(120, s);
}
// TODO: complete trimming
static String javaTokWordWrap(int cols, String s) {
int col = 0;
List tok = javaTok(s);
for (int i = 0; i < l(tok); i++) {
String t = tok.get(i);
if (odd(i) && col >= cols && !containsNewLine(t))
tok.set(i, t = rtrimSpaces(t) + "\n");
int idx = t.lastIndexOf('\n');
if (idx >= 0) col = l(t)-(idx+1);
else col += l(t);
}
return join(tok);
}
static String fileName(File f) {
return f == null ? null : f.getName();
}
static String ymd() {
return ymd(now());
}
static String ymd(long now) {
return year(now) + formatInt(month(now), 2) + formatInt(dayOfMonth(now), 2);
}
static String ymd(long now, TimeZone tz) {
return year(now, tz) + formatInt(month(now, tz), 2) + formatInt(dayOfMonth(now, tz), 2);
}
static String formatInt(int i, int digits) {
return padLeft(str(i), '0', digits);
}
static String formatInt(long l, int digits) {
return padLeft(str(l), '0', digits);
}
static int hours() {
return hours(java.util.Calendar.getInstance());
}
static int hours(java.util.Calendar c) {
return c.get(java.util.Calendar.HOUR_OF_DAY);
}
static int hours(long time) {
return hours(calendarFromTime(time));
}
static int hours(long time, TimeZone tz) {
return hours(calendarFromTime(time, tz));
}
static int roundDownTo_rev(int x, int n) {
return roundDownTo(n, x);
}
static long roundDownTo_rev(long x, long n) {
return roundDownTo(n, x);
}
static int minutes() {
return minutes(java.util.Calendar.getInstance());
}
static int minutes(java.util.Calendar c) {
return c.get(java.util.Calendar.MINUTE);
}
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); } }
static long toK(long l) {
return (l+1023)/1024;
}
static String n(long l, String name) {
return l + " " + trim(l == 1 ? singular(name) : getPlural(name));
}
static String n(Collection l, String name) {
return n(l(l), name);
}
static String n(Map m, String name) {
return n(l(m), name);
}
static String n(Object[] a, String name) {
return n(l(a), name);
}
static String n(MultiSet ms, String name) {
return n(l(ms), name);
}
static A printStackTrace(A e) {
// we go to system.out now - system.err is nonsense
if (e != null) print(getStackTrace(e));
return e;
}
static void printStackTrace() {
printStackTrace(new Throwable());
}
static void printStackTrace(String msg) {
printStackTrace(new Throwable(msg));
}
static void printStackTrace(String msg, Throwable e) {
printStackTrace(new Throwable(msg, e));
}
static boolean structure_showTiming, structure_checkTokenCount;
static String structure(Object o) {
return structure(o, new structure_Data());
}
static String structure(Object o, structure_Data d) {
StringWriter sw = new StringWriter();
d.out = new PrintWriter(sw);
structure_go(o, d);
String s = str(sw);
if (structure_checkTokenCount) {
print("token count=" + d.n);
assertEquals("token count", l(javaTokC(s)), d.n);
}
return s;
}
static void structure_go(Object o, structure_Data d) {
structure_1(o, d);
while (nempty(d.stack))
popLast(d.stack).run();
}
static void structureToPrintWriter(Object o, PrintWriter out) { structureToPrintWriter(o, out, new structure_Data()); }
static void structureToPrintWriter(Object o, PrintWriter out, structure_Data d) {
d.out = out;
structure_go(o, d);
}
// leave to false, unless unstructure() breaks
static boolean structure_allowShortening = false;
// info on how to serialize objects of a certain class
static class structure_ClassInfo {
Class c;
String shortName;
List fields;
Method customSerializer;
IVF1 serializeObject; // can be set by caller of structure function
boolean special = false; // various special classes
boolean nullInstances = false; // serialize all instances as null (e.g. lambdas/anonymous classes)
boolean javafy = false; // always convert to "j ..."
Object emptyInstance; // to grab default field values from
public String toString() {
return commaCombine(
"Class " + className(c),
stringIf("special", special),
stringIf("customSerializer", customSerializer != null),
stringIf("javafy", javafy),
stringIf("nullInstances", nullInstances));
}
void nullInstances(boolean b) {
this.nullInstances = b;
if (b) special = true;
}
void javafy(boolean b) {
this.javafy = b;
if (b) special = true;
}
// overridable - return true if you wrote the object
boolean handle(A o) { return false; }
}
static class structure_Data {
PrintWriter out;
int stringSizeLimit;
int shareStringsLongerThan = 20;
boolean noStringSharing = false;
boolean storeBaseClasses = false;
boolean honorFieldOrder = true;
String mcDollar = actualMCDollar();
final public structure_Data setWarnIfUnpersistable(boolean warnIfUnpersistable){ return warnIfUnpersistable(warnIfUnpersistable); }
public structure_Data warnIfUnpersistable(boolean warnIfUnpersistable) { this.warnIfUnpersistable = warnIfUnpersistable; return this; } final public boolean getWarnIfUnpersistable(){ return warnIfUnpersistable(); }
public boolean warnIfUnpersistable() { return warnIfUnpersistable; }
boolean warnIfUnpersistable = true;
final public structure_Data setStackTraceIfUnpersistable(boolean stackTraceIfUnpersistable){ return stackTraceIfUnpersistable(stackTraceIfUnpersistable); }
public structure_Data stackTraceIfUnpersistable(boolean stackTraceIfUnpersistable) { this.stackTraceIfUnpersistable = stackTraceIfUnpersistable; return this; } final public boolean getStackTraceIfUnpersistable(){ return stackTraceIfUnpersistable(); }
public boolean stackTraceIfUnpersistable() { return stackTraceIfUnpersistable; }
boolean stackTraceIfUnpersistable = true;
// skip a field if it has the default value defined in the class
// -slower, and may cause issues with schema evolution
// -OTOH, it can serialize null values for a field with default non-null value
final public structure_Data setSkipDefaultValues(boolean skipDefaultValues){ return skipDefaultValues(skipDefaultValues); }
public structure_Data skipDefaultValues(boolean skipDefaultValues) { this.skipDefaultValues = skipDefaultValues; return this; } final public boolean getSkipDefaultValues(){ return skipDefaultValues(); }
public boolean skipDefaultValues() { return skipDefaultValues; }
boolean skipDefaultValues = false;
structure_Data d() { return this; }
transient IF1 shouldIncludeField;
boolean shouldIncludeField(Field f) { return shouldIncludeField != null ? shouldIncludeField.get(f) : shouldIncludeField_base(f); }
final boolean shouldIncludeField_fallback(IF1 _f, Field f) { return _f != null ? _f.get(f) : shouldIncludeField_base(f); }
boolean shouldIncludeField_base(Field f) { return true; }
IdentityHashMap seen = new IdentityHashMap();
//new BitSet refd;
HashMap strings = new HashMap();
HashSet concepts = new HashSet();
HashMap infoByClass = new HashMap();
// wrapper for _persistenceInfo field or _persistenceInfo method
// by class (taking the object instance)
HashMap> persistenceInfo = new HashMap();
int n; // token count
List stack = new ArrayList();
// append single token
structure_Data append(String token) { out.print(token); ++n; return this; }
structure_Data append(int i) { out.print(i); ++n; return this; }
// append multiple tokens
structure_Data append(String token, int tokCount) { out.print(token); n += tokCount; return this; }
// extend last token
structure_Data app(String token) { out.print(token); return this; }
structure_Data app(int i) { out.print(i); return this; }
structure_Data app(char c) { out.print(c); return this; }
structure_ClassInfo infoForClass(Class c) {
structure_ClassInfo info = infoByClass.get(c);
if (info == null) info = newClass(c);
return info;
}
transient IF1 realShortName;
String realShortName(String name) { return realShortName != null ? realShortName.get(name) : realShortName_base(name); }
final String realShortName_fallback(IF1 _f, String name) { return _f != null ? _f.get(name) : realShortName_base(name); }
String realShortName_base(String name) {
return dropPrefix("main$",
dropPrefix("loadableUtils.utils$",
dropPrefix(mcDollar, name)));
}
// called when a new class is detected
// can be overridden by clients
structure_ClassInfo newClass(Class c) {
// special classes here!
var d = d();
boolean isJavaXClass = isJavaXClassName(c.getName(), mcDollar);
if (c == String.class)
return new structure_ClassInfo() {
@Override boolean handle(String o) {
String s = d.stringSizeLimit != 0 ? shorten((String) o, d.stringSizeLimit) : (String) o;
if (!d.noStringSharing) {
if (d.shareStringsLongerThan == Integer.MAX_VALUE)
d.seen.put(o, d.n);
if (l(s) >= d.shareStringsLongerThan)
d.strings.put(s, d.n);
}
quoteToPrintWriter(s, d.out); d.n++;
return true;
}
};
if (c == File.class)
return new structure_ClassInfo() {
@Override boolean handle(File o) {
append("File ").append(quote(o.getPath()));
return true;
}
};
if (!isJavaXClass) {
if (isSubClassOf(c, Set.class))
return new structure_ClassInfo() {
@Override boolean handle(Set o) {
writeSet(o);
return true;
}
};
if (isSubClassOf(c, Collection.class))
return new structure_ClassInfo() {
@Override boolean handle(Collection o) {
writeCollection(o);
return true;
}
};
if (isSubClassOf(c, Map.class))
return new structure_ClassInfo() {
@Override boolean handle(Map o) {
writeMap(o);
return true;
}
};
}
structure_ClassInfo info = new structure_ClassInfo();
info.c = c;
infoByClass.put(c, info);
String name = c.getName();
String shortName = realShortName(name);
if (startsWithDigit(shortName)) shortName = name; // for anonymous classes
info.shortName = shortName;
try {
if (isSyntheticOrAnonymous(c)) {
info.nullInstances(true);
return info;
}
if (c.isEnum()) {
info.special = true;
return info;
}
if (c.isArray()) {
// info.special?
return info;
}
if ((info.customSerializer = findMethodNamed(c, "_serialize"))
!= null) info.special = true;
if (storeBaseClasses) {
Class sup = c.getSuperclass();
if (sup != Object.class) {
append("bc ");
append(shortDynClassNameForStructure(c));
out.print(" ");
append(shortDynClassNameForStructure(sup));
out.print(" ");
infoForClass(sup); // transitively write out superclass relations
}
}
if (eqOneOf(name, "java.awt.Color", "java.lang.ThreadLocal"))
info.javafy(true);
else if (name.startsWith("sun") || !isPersistableClass(c)) {
info.javafy(true);
if (warnIfUnpersistable) {
String msg = "Class not persistable: " + c + " (anonymous or no default constructor), referenced from " + last(stack);
if (stackTraceIfUnpersistable)
printStackTrace(new Throwable(msg));
else
print(msg);
}
} else if (skipDefaultValues) {
var ctor = getDefaultConstructor(c);
if (ctor != null)
info.emptyInstance = invokeConstructor(ctor);
}
} catch (Throwable e) { printStackTrace(e);
info.nullInstances(true);
}
return info;
}
void setFields(structure_ClassInfo info, List fields) {
info.fields = fields;
}
void writeObject(Object o, String shortName, Map fv) {
String singleField = fv.size() == 1 ? first(fv.keySet()) : null;
append(shortName);
n += countDots(shortName)*2; // correct token count
int l = n;
Iterator it = fv.entrySet().iterator();
class WritingObject implements Runnable {
String lastFieldWritten;
public void run() { try {
if (!it.hasNext()) {
if (n != l)
append(")");
} else {
Map.Entry e = (Map.Entry) (it.next());
append(n == l ? "(" : ", ");
append(lastFieldWritten = (String) e.getKey()).append("=");
stack.add(this);
structure_1(e.getValue(), structure_Data.this);
}
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return shortName + "." + lastFieldWritten; }
}
stack.add(new WritingObject());
}
void writeMap(Map o) {
var d = this;
String name = o.getClass().getName();
if (o instanceof LinkedHashMap) d.append("lhm");
else if (o instanceof HashMap) d.append("hm");
else if (o instanceof TreeMap)
d.append(isCIMap_gen((TreeMap) o) ? "cimap" : "tm");
else if (name.equals("java.util.Collections$SynchronizedMap")
|| name.equals("java.util.Collections$SynchronizedSortedMap")
|| name.equals("java.util.Collections$SynchronizedNavigableMap")) {
d.append("sync ");
{ structure_1(unwrapSynchronizedMap(((Map) o)), d); return; }
}
d.append("{");
final int l = d.n;
final Iterator it = cloneMap((Map) o).entrySet().iterator();
class WritingMap implements Runnable {
boolean v = false;
Map.Entry e;
public String toString() {
return renderVars("WritingMap", "e" , mapEntryToPair(e), "v" , !v);
}
public void run() { try {
if (v) {
d.append("=");
v = false;
d.stack.add(this);
structure_1(e.getValue(), d);
} else {
if (!it.hasNext())
d.append("}");
else {
e = (Map.Entry) it.next();
v = true;
d.stack.add(this);
if (d.n != l) d.append(", ");
structure_1(e.getKey(), d);
}
}
} catch (Exception __e) { throw rethrow(__e); } }
}
d.stack.add(new WritingMap());
}
void writeSet(Set o) {
var d = this;
String name = o.getClass().getName();
/*O set2 = unwrapSynchronizedSet(o);
if (set2 != o) {
d.append("sync");
o = set2;
} TODO */
if (o instanceof TreeSet) {
d.append(isCISet_gen(o) ? "ciset" : "treeset");
structure_1(new ArrayList(o), d);
return;
}
// assume it's a HashSet or LinkedHashSet
d.append(o instanceof LinkedHashSet ? "lhs" : "hashset");
structure_1(new ArrayList(o), d);
}
void writeCollection(Collection o) {
var d = this;
String name = o.getClass().getName();
if (name.equals("java.util.Collections$SynchronizedList")
|| name.equals("java.util.Collections$SynchronizedRandomAccessList")) {
d.append("sync ");
{ structure_1(unwrapSynchronizedList(((List) o)), d); return; }
}
else if (name.equals("java.util.LinkedList"))
d.append("ll");
d.append("[");
int l = d.n;
Iterator it = cloneList(o).iterator();
d.stack.add(new Runnable() { public void run() { try {
if (!it.hasNext())
d.append("]");
else {
d.stack.add(this);
if (d.n != l) d.append(", ");
structure_1(it.next(), d);
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!it.hasNext())\r\n d.append(\"]\");\r\n else {\r\n d.stack.add..."; }});
}
} // end of class structure_Data
static void structure_1(Object o, structure_Data d) { try {
if (o == null) { d.append("null"); return; }
Class c = o.getClass();
boolean concept = false;
concept = o instanceof Concept;
structure_ClassInfo info = d.infoForClass(c);
boolean isJavaXName = isJavaXClassName(c.getName(), d.mcDollar);
boolean referencable = isJavaXName &&
!(o instanceof Number || o instanceof Character || o instanceof Boolean)
|| o instanceof Collection || o instanceof Map;
if (referencable) {
Integer ref = d.seen.get(o);
if (ref != null) {
//d.refd.set(ref);
d.append("t").app(ref);
return;
}
d.seen.put(o, d.n); // record token number
}
if (info.handle(o)) {
return;
}
if (info.special) {
if (info.javafy) {
d.append("j ").append(quote(str(o))); return; // This is not unstructure-able except for java.awt.Color
}
if (c.isEnum()) {
d.append("enum ");
d.append(info.shortName);
d.out.append(' ');
d.append(((Enum) o).ordinal());
return;
}
if (info.customSerializer != null) {
// custom serialization (_serialize method)
Object o2 = invokeMethod(info.customSerializer, o);
if (o2 == o) {} // bail out to standard serialization
else {
d.append("cu ");
String name = c.getName();
String shortName = d.realShortName(name);
d.append(shortName);
d.out.append(' ');
structure_1(o2, d);
return;
}
} else if (info.nullInstances) { d.append("null"); return; }
else if (info.serializeObject != null)
{ info.serializeObject.get(o); return; }
else throw fail("unknown special type");
}
List lFields = info.fields;
if (lFields == null) {
// these are never back-referenced (for readability)
if (o instanceof Number) {
PrintWriter out = d.out;
if (o instanceof Integer) { int i = ((Integer) o).intValue(); out.print(i); d.n += i < 0 ? 2 : 1; return; }
if (o instanceof Long) { long l = ((Long) o).longValue(); out.print(l); out.print("L"); d.n += l < 0 ? 2 : 1; return; }
if (o instanceof Short) { short s = ((Short) o).shortValue(); d.append("sh "); out.print(s); d.n += s < 0 ? 2 : 1; return; }
if (o instanceof Float) { d.append("fl ", 2); quoteToPrintWriter(str(o), out); return; }
if (o instanceof Double) { d.append("d(", 3); quoteToPrintWriter(str(o), out); d.append(")"); return; }
if (o instanceof BigInteger) { out.print("bigint("); out.print(o); out.print(")"); d.n += ((BigInteger) o).signum() < 0 ? 5 : 4; return; }
}
if (o instanceof Boolean) {
d.append(((Boolean) o).booleanValue() ? "t" : "f"); return;
}
if (o instanceof Character) {
d.append(quoteCharacter((Character) o)); return;
}
String name = c.getName();
if (!isJavaXName) {
if (o instanceof Set)
{ d.writeSet((Set) o); return; }
if (o instanceof Collection
/* && neq(name, "main$Concept$RefL") */) {
{ d.writeCollection((Collection) o); return; }
}
if (o instanceof Map)
{ d.writeMap((Map) o); return; }
}
if (c.isArray()) {
if (o instanceof byte[]) {
d.append("ba ").append(quote(bytesToHex((byte[]) o))); return;
}
final int n = Array.getLength(o);
if (o instanceof boolean[]) {
String hex = boolArrayToHex((boolean[]) o);
int i = l(hex);
while (i > 0 && hex.charAt(i-1) == '0' && hex.charAt(i-2) == '0') i -= 2;
d.append("boolarray ").append(n).app(" ").append(quote(substring(hex, 0, i))); return;
}
if (o instanceof short[]) {
String hex = shortArrayToHex_bigEndian((short[]) o);
d.append("shortarray \"").append(hex).app('\"');
return;
}
if (o instanceof long[]) {
String hex = longArrayToHex_bigEndian((long[]) o);
d.append("longarray \"").append(hex).app('\"');
return;
}
String atype = "array"/*, sep = ", "*/; // sep is not used yet
if (o instanceof int[]) {
//ret "intarray " + quote(intArrayToHex((int[]) o));
atype = "intarray";
//sep = " ";
} else if (o instanceof double[]) {
atype = "dblarray";
//sep = " ";
} else if (o instanceof float[]) {
atype = "floatarray";
} else {
// 2-dimensional and deeper arrays
Pair p = arrayTypeAndDimensions(c);
if (p.a == int.class) atype = "intarray";
else if (p.a == byte.class) atype = "bytearray";
else if (p.a == boolean.class) atype = "boolarray";
else if (p.a == double.class) atype = "dblarray";
else if (p.a == float.class) atype = "floatarray";
else if (p.a == String.class) { atype = "array S"; d.n++; }
else atype = "array"; // fail("Unsupported array type: " + p.a);
if (p.b > 1) {
atype += "/" + p.b; // add number of dimensions
d.n += 2; // 2 additional tokens will be written
}
}
d.append(atype).append("{");
d.stack.add(new Runnable() {
int i;
public void run() {
if (i >= n)
d.append("}");
else {
d.stack.add(this);
if (i > 0) d.append(", ");
structure_1(Array.get(o, i++), d);
}
}
});
return;
}
if (o instanceof Class) {
d.append("class(", 2).append(quote(((Class) o).getName())).append(")"); return;
}
if (o instanceof Throwable) {
d.append("exception(", 2).append(quote(((Throwable) o).getMessage())).append(")"); return;
}
if (o instanceof BitSet) {
BitSet bs = (BitSet) o;
d.append("bitset{", 2);
int l = d.n;
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
if (d.n != l) d.append(", ");
d.append(i);
}
d.append("}"); return;
}
/*if (name.equals("main$Lisp")) {
fail("lisp not supported right now");
}*/
String dynName = shortDynClassNameForStructure(o);
if (concept && !d.concepts.contains(dynName)) {
d.concepts.add(dynName);
d.append("c ");
}
// serialize an object with fields.
// first, collect all fields and values in fv.
TreeSet fields = new TreeSet(new Comparator() {
public int compare(Field a, Field b) {
return stdcompare(a.getName(), b.getName());
}
});
Class cc = c;
while (cc != Object.class) {
for (Field field : getDeclaredFields_cached(cc)) {
if (!d.shouldIncludeField(field)) continue;
String fieldName = field.getName();
if (fieldName.equals("_persistenceInfo"))
d.persistenceInfo.put(c, obj -> (Map) fieldGet(field, obj));
if ((field.getModifiers() & (java.lang.reflect.Modifier.STATIC | java.lang.reflect.Modifier.TRANSIENT)) != 0)
continue;
fields.add(field);
// put special cases here...?
}
cc = cc.getSuperclass();
}
Method persistenceInfoMethod = findInstanceMethod(c, "_persistenceInfo");
if (persistenceInfoMethod != null)
d.persistenceInfo.put(c, obj -> (Map) invokeMethod(persistenceInfoMethod, obj));
lFields = asList(d.honorFieldOrder ? fieldObjectsInFieldOrder(c, fields) : fields);
// Render this$0/this$1 first because unstructure needs it for constructor call.
int n = l(lFields);
for (int i = 0; i < n; i++) {
Field f = lFields.get(i);
if (f.getName().startsWith("this$")) {
lFields.remove(i);
lFields.add(0, f);
break;
}
}
d.setFields(info, lFields);
} // << if (lFields == null)
// get _persistenceInfo from field and/or dynamic field
IF1 piGetter = d.persistenceInfo.get(c);
Map persistenceInfo = piGetter == null ? null : piGetter.get(o);
if (piGetter == null && o instanceof DynamicObject)
persistenceInfo = (Map) getOptDynOnly(((DynamicObject) o), "_persistenceInfo");
LinkedHashMap fv = new LinkedHashMap();
Object defaultInstance = info.emptyInstance;
if (!referencable)
throw fail("Not referencable: " + className(o));
for (Field f : lFields) {
Object value, defaultValue = null;
try {
value = f.get(o);
defaultValue = defaultInstance == null ? null : f.get(defaultInstance);
} catch (Exception e) {
value = "?";
}
if (!eq(defaultValue, value) && (persistenceInfo == null
|| !Boolean.FALSE.equals(persistenceInfo.get(f.getName()))))
fv.put(f.getName(), value);
}
String shortName = info.shortName;
// Now we have fields & values. Process fieldValues if it's a DynamicObject.
Object classNameFromFV = fv.get("className");
// omit field "className" if equal to class's name
if (concept && eq(classNameFromFV, shortName))
fv.remove("className");
if (o instanceof DynamicObject) {
putAll(fv, (Map) fv.get("fieldValues"));
fv.remove("fieldValues");
if (((DynamicObject) o).className != null) {
// TODO: this probably doesn't work with inner classes
shortName = shortDynClassNameForStructure((DynamicObject) o);
fv.remove("className");
// special handling for missing Class objects encoded as DynamicObject
if (eq(shortName, "java.lang.Class")) {
d.append("class(");
d.append(quoted(fv.get("name")));
d.append(")");
return;
}
}
}
d.writeObject(o, shortName, fv);
} catch (Exception __e) { throw rethrow(__e); } }
static Collection allConcepts() {
return db_mainConcepts().allConcepts();
}
static Collection allConcepts(Concepts concepts) {
return concepts.allConcepts();
}
static long sysNow() {
ping();
return System.nanoTime()/1000000;
}
static void pcallFAll(Collection l, Object... args) {
if (l != null) for (Object f : cloneList(l)) pcallF(f, args);
}
static void pcallFAll(Iterator it, Object... args) {
while (it.hasNext()) pcallF(it.next(), args);
}
static boolean isTransient(Field f) {
return (f.getModifiers() & java.lang.reflect.Modifier.TRANSIENT) != 0;
}
static RuntimeException fail() { throw new RuntimeException("fail"); }
static RuntimeException fail(Throwable e) { throw asRuntimeException(e); }
static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); }
static RuntimeException fail(Object... objects) { throw new Fail(objects); }
static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); }
static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); }
// firstDelay = delay
static FixedRateTimer doEvery_daemon(long delay, final Object r) { return doEvery_daemon(defaultTimerName(), delay, r); }
static FixedRateTimer doEvery_daemon(String timerName, long delay, final Object r) {
return doEvery_daemon(timerName, delay, delay, r);
}
static FixedRateTimer doEvery_daemon(long delay, long firstDelay, final Object r) { return doEvery_daemon(defaultTimerName(), delay, firstDelay, r); }
static FixedRateTimer doEvery_daemon(String timerName, long delay, long firstDelay, final Object r) {
FixedRateTimer timer = new FixedRateTimer(true);
timer.scheduleAtFixedRate(smartTimerTask(r, timer, delay), firstDelay, delay);
return timer;
}
static FixedRateTimer doEvery_daemon(double delaySeconds, final Object r) {
return doEvery_daemon(toMS(delaySeconds), r);
}
static int abs(int i) { return Math.abs(i); }
static long abs(long i) { return Math.abs(i); }
static float abs(float i) { return Math.abs(i); }
static double abs(double i) { return Math.abs(i); }
static double abs(Complex c) { return c.abs(); }
static void closeAllOpt(Iterable l) {
if (l == null) return;
for (Object c : l) { try { if (c instanceof AutoCloseable) close((AutoCloseable) c); } catch (Throwable __e) { pcallFail(__e); }}
}
static void closeAllOpt(Object... l) {
if (l == null) return;
for (Object c : l) { try { if (c instanceof AutoCloseable) close((AutoCloseable) c); } catch (Throwable __e) { pcallFail(__e); }}
}
static void sleepInCleanUp(long ms) { try {
if (ms < 0) return;
Thread.sleep(ms);
} catch (Exception __e) { throw rethrow(__e); } }
static void pcallFail(Throwable e) {
pcallPolicyForThread().handlePcallFail(e);
}
static void pcallFail(String msg) {
pcallFail(new Throwable(msg));
}
static boolean cleanUp_interruptThreads = false; // experimental
static void cleanUp(Object c) {
if (c == null) return;
if (c instanceof AutoCloseable) { close_pcall((AutoCloseable) c); return; }
if (c instanceof java.util.Timer) { ((java.util.Timer) c).cancel(); return; }
if (c instanceof Collection) { cleanUp((Collection) c); return; }
if (c instanceof Map) {
for (Object o : keysList((Map) c)) cleanUp(o);
for (Object o : valuesList((Map) c)) cleanUp(o);
syncClear((Map) c);
return;
}
//if (!(c instanceof Class)) ret;
try {
// revoke license
preCleanUp(c);
// unpause
setOpt_raw(c, "ping_pauseAll", false);
// call custom cleanMeUp() and cleanMeUp_*() functions
innerCleanUp(c);
// Java spec says finalize should only be called by GC,
// but we care to differ.
// Edit: Not anymore (illegal access warnings)
/*if (isTrue(vmMap_get('callFinalize)))
pcallOpt(c, "finalize");*/
// remove all virtual bots (hope this works)
List androids = (List) getOpt(c, "record_list");
for (Object android : unnull(androids))
pcallOpt(android, "dispose"); // heck we'll dispose anything
// sub-cleanup
List classes = (List) (getOpt(c, "hotwire_classes"));
if (classes != null)
for (WeakReference cc : classes) { try {
cleanUp(cc.get());
} catch (Throwable __e) { pcallFail(__e); }}
// interrupt all threads (experimental, they might be doing cleanup?)
if (cleanUp_interruptThreads) {
List threads = registeredThreads(c);
if (nempty(threads)) {
print("cleanUp: Interrupting " + n2(threads, "thread") + ": " + joinWithComma(allToString(threads)));
interruptThreads(threads);
}
}
} catch (Throwable __e) { pcallFail(__e); }
setOpt_raw(c, "cleaningUp_flag" , false);
if (c instanceof Class && ((Class) c).getName().equals("main"))
retireClassLoader(((Class) c).getClassLoader());
}
static void cleanUp(Collection l) {
if (l == null) return;
for (Object c : l)
cleanUp(c);
l.clear();
}
static Set keys(Map map) {
return map == null ? new HashSet() : map.keySet();
}
// convenience shortcut for keys_gen
static Set keys(Object map) {
return keys((Map) map);
}
static Set keys(MultiSet ms) {
return ms.keySet();
}
static Set keys(IMultiMap mm) {
return mm.keySet();
}
// legacy mode
//sbool ping_actions_shareable = true;
static volatile boolean ping_pauseAll = false;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile boolean ping_anyActions = false;
static Map ping_actions = newWeakHashMap();
static ThreadLocal ping_isCleanUpThread = new ThreadLocal();
// ignore pingSource if not PingV3
static boolean ping(PingSource pingSource) { return ping(); }
// always returns true
static boolean ping() {
//ifdef useNewPing
newPing();
//endifdef
if (ping_pauseAll || ping_anyActions) ping_impl(true /* XXX */);
//ifndef LeanMode ping_impl(); endifndef
return true;
}
// returns true when it slept
static boolean ping_impl(boolean okInCleanUp) { try {
if (ping_pauseAll && !isAWTThread()) {
do
Thread.sleep(ping_sleep);
while (ping_pauseAll);
return true;
}
if (ping_anyActions) { // don't allow sharing ping_actions
if (!okInCleanUp && !isTrue(ping_isCleanUpThread.get()))
failIfUnlicensed();
Object action = null;
synchronized(ping_actions) {
if (!ping_actions.isEmpty()) {
action = ping_actions.get(currentThread());
if (action instanceof Runnable)
ping_actions.remove(currentThread());
if (ping_actions.isEmpty()) ping_anyActions = false;
}
}
if (action instanceof Runnable)
((Runnable) action).run();
else if (eq(action, "cancelled"))
throw fail("Thread cancelled.");
}
return false;
} catch (Exception __e) { throw rethrow(__e); } }
static ArrayList cloneList(Iterable l) {
return l instanceof Collection ? cloneList((Collection) l) : asList(l);
}
static ArrayList cloneList(Collection l) {
if (l == null) return new ArrayList();
synchronized(collectionMutex(l)) {
return new ArrayList (l);
}
}
static boolean warn_on = true;
static ThreadLocal> warn_warnings = new ThreadLocal();
static void warn(String s) {
if (warn_on)
print("Warning: " + s);
}
static void warn(String s, List warnings) {
warn(s);
if (warnings != null)
warnings.add(s);
addToCollection(warn_warnings.get(), s);
}
static String getClassName(Object o) {
return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName();
}
static Object first(Object list) {
return first((Iterable) list);
}
static A first(List list) {
return empty(list) ? null : list.get(0);
}
static A first(A[] bla) {
return bla == null || bla.length == 0 ? null : bla[0];
}
static Pair first(Map map) {
return mapEntryToPair(first(entrySet(map)));
}
static Pair first(MultiMap mm) {
if (mm == null) return null;
var e = first(mm.data.entrySet());
if (e == null) return null;
return pair(e.getKey(), first(e.getValue()));
}
static A first(IterableIterator i) {
return first((Iterator ) i);
}
static A first(Iterator i) {
return i == null || !i.hasNext() ? null : i.next();
}
static A first(Iterable i) {
if (i == null) return null;
Iterator it = i.iterator();
return it.hasNext() ? it.next() : null;
}
static Character first(String s) { return empty(s) ? null : s.charAt(0); }
static Character first(CharSequence s) { return empty(s) ? null : s.charAt(0); }
static A first(Pair p) {
return p == null ? null : p.a;
}
static A first(T3 t) {
return t == null ? null : t.a;
}
static Byte first(byte[] l) { return empty(l) ? null : l[0]; }
static Double first(double[] l) { return empty(l) ? null : l[0]; }
static A first(A[] l, IF1 pred) {
return firstThat(l, pred);
}
static A first(Iterable l, IF1 pred) {
return firstThat(l, pred);
}
static A first(IF1 pred, Iterable l) {
return firstThat(pred, l);
}
static A first(AppendableChain a) {
return a == null ? null : a.element;
}
static A first(IMultiSet a) {
return a == null ? null : first(a.keySet());
}
static A firstOfType(Collection c, Class type) {
for (Object x : c)
if (isInstanceX(type, x))
return (A) x;
return null;
}
static List conceptsOfType(String type) {
return db_mainConcepts().conceptsOfType(type);
}
static List concatLists_conservative(List a, List b) {
if (empty(a)) return b;
if (empty(b)) return a;
return concatLists(a, b);
}
static List concatLists_conservative(Collection a, Collection b) {
if (empty(a) && b instanceof List) return ((List) b);
if (empty(b) && a instanceof List) return ((List) a);
return concatLists(a, b);
}
static List filterByType(Iterable c, Class type) {
List l = new ArrayList();
if (c != null) for (Object x : c)
if (isInstanceX(type, x))
l.add((A) x);
return l;
}
static List filterByType(Object[] c, Class type) {
return filterByType(asList(c), type);
}
static List filterByType(Class type, Iterable c) {
return filterByType(c, type);
}
static List list(Class type) { return list(type, db_mainConcepts()); }
static List list(Class type, Concepts cc) {
return cc.list(type);
}
static List list(Concepts concepts, Class type) {
return concepts.list(type);
}
static List list(String type) {
return db_mainConcepts().list(type);
}
static List list(Concepts concepts, String type) {
return concepts.list(type);
}
static List list(Concepts concepts) {
return asList(concepts.allConcepts());
}
static List filterByDynamicType(Collection c, String type) {
List l = new ArrayList();
for (A x : c)
if (eq(dynamicClassName(x), type))
l.add(x);
return l;
}
static boolean hasType(Collection c, Class type) {
for (Object x : c)
if (isInstanceX(type, x))
return true;
return false;
}
static String shortClassName(Object o) {
if (o == null) return null;
Class c = o instanceof Class ? (Class) o : o.getClass();
String name = c.getName();
return shortenClassName(name);
}
static A findBackRef(Concept c, Class type) {
for (Concept.Ref r : c.backRefs)
if (instanceOf(r.concept(), type))
return (A) r.concept();
return null;
}
static A findBackRef(Class type, Concept c) {
return findBackRef(c, type);
}
static B mapGet(Map map, A a) {
return map == null || a == null ? null : map.get(a);
}
static B mapGet(A a, Map map) {
return map == null || a == null ? null : map.get(a);
}
static boolean checkConceptFields(Concept x, Object... data) {
for (int i = 0; i < l(data); i += 2)
if (neq(cget(x, (String) data[i]), deref(data[i+1])))
return false;
return true;
}
static void mapRemove(Map map, A key) {
if (map != null && key != null)
map.remove(key);
}
static List concatLists(Iterable ... lists) {
List l = new ArrayList();
if (lists != null) for (Iterable list : lists)
addAll(l, list);
return l;
}
static List concatLists(Collection extends Iterable > lists) {
List l = new ArrayList();
if (lists != null) for (Iterable list : lists)
addAll(l, list);
return l;
}
static Concept cnew(String name, Object... values) {
return cnew(db_mainConcepts(), name, values);
}
static Concept cnew(Concepts concepts, String name, Object... values) {
Class extends Concept> cc = findClass(name);
concepts_unlisted.set(true);
Concept c;
try {
c = cc != null ? nuObject(cc) : new Concept(name);
} finally {
concepts_unlisted.set(null);
}
csetAll(c, values);
concepts.register(c);
return c;
}
static A cnew(Class cc, Object... values) {
return cnew(db_mainConcepts(), cc, values);
}
static A cnew(Concepts concepts, Class cc, Object... values) {
concepts_unlisted.set(true);
A c;
try {
c = nuObject(cc);
} finally {
concepts_unlisted.set(null);
}
csetAll(c, values);
concepts.register(c);
return c;
}
// returns number of changes
static int cset(Concept c, Object... values) { try {
if (c == null) return 0;
warnIfOddCount(values = expandParams(c.getClass(), values));
int changes = 0;
for (int i = 0; i+1 < l(values); i += 2)
if (_csetField(c, (String) values[i], values[i+1])) ++changes;
return changes;
} catch (Exception __e) { throw rethrow(__e); } }
static int cset(Iterable extends Concept> l, Object... values) {
int changes = 0;
for (Concept c : unnullForIteration(l))
changes += cset(c, values);
return changes;
}
static int cset(Concept.Ref c, Object... values) {
return cset(getVar(c), values);
}
static Object cget(Object c, String field) {
c = derefRef(c);
Object o = getOpt(c, field);
return derefRef(o);
}
static Object cget(String field, Object c) {
return cget(c, field);
}
static Object deref(Object o) {
if (o instanceof IRef) return ((IRef) o).get();
return o;
}
static List map(Iterable l, Object f) { return map(f, l); }
static List map(Object f, Iterable l) {
List x = emptyList(l);
if (l != null) for (Object o : l)
{ ping(); x.add(callF(f, o)); }
return x;
}
// map: func(key, value) -> list element
static List map(Map map, Object f) {
List x = new ArrayList();
if (map != null) for (Object _e : map.entrySet()) { ping();
Map.Entry e = (Map.Entry) _e;
x.add(callF(f, e.getKey(), e.getValue()));
}
return x;
}
static List map(Object f, Object[] l) { return map(f, asList(l)); }
static List map(Object[] l, Object f) { return map(f, l); }
static List map(Object f, Map map) {
return map(map, f);
}
static List map(Iterable l, F1 f) { return map(f, l); }
static List map(F1 f, Iterable l) {
List x = emptyList(l);
if (l != null) for (A o : l)
{ ping(); x.add(callF(f, o)); }
return x;
}
static List map(IF1 f, Iterable l) { return map(l, f); }
static List map(Iterable l, IF1 f) {
List x = emptyList(l);
if (l != null) {
var it = l.iterator();
if (it.hasNext()) {
var pingSource = pingSource();
do {
ping(pingSource);
x.add(f.get(it.next()));
} while (it.hasNext());
}
}
return x;
}
static List map(IF1 f, A[] l) { return map(l, f); }
static List map(A[] l, IF1 f) {
List x = emptyList(l);
if (l != null) for (A o : l)
{ ping(); x.add(f.get(o)); }
return x;
}
static List map(Map map, IF2 f) {
List x = new ArrayList();
if (map != null) for (Map.Entry e : map.entrySet()) { ping();
x.add(f.get(e.getKey(), e.getValue()));
}
return x;
}
// new magic alias for mapLL - does it conflict?
static List map(IF1 f, A data1, A... moreData) {
List x = emptyList(l(moreData)+1);
x.add(f.get(data1));
if (moreData != null) for (A o : moreData)
{ ping(); x.add(f.get(o)); }
return x;
}
static void lock(Lock lock) { try {
ping();
if (lock == null) return;
try {
vmBus_send("locking", lock, "thread" , currentThread());
lock.lockInterruptibly();
vmBus_send("locked", lock, "thread" , currentThread());
} catch (InterruptedException e) {
Object reason = vm_threadInterruptionReasonsMap().get(currentThread());
print("Locking interrupted! Reason: " + strOr(reason, "Unknown"));
printStackTrace(e);
rethrow(e);
}
// NO call to ping here! Make sure lock is always released.
} catch (Exception __e) { throw rethrow(__e); } }
static void lock(Lock lock, String msg) {
print("Locking: " + msg);
lock(lock);
}
static void lock(Lock lock, String msg, long timeout) {
print("Locking: " + msg);
lockOrFail(lock, timeout);
}
static ReentrantLock lock() {
return fairLock();
}
static String loadConceptsStructure(String progID) {
return loadTextFilePossiblyGZipped(getProgramFile(progID, "concepts.structure"));
}
static String loadConceptsStructure() {
return loadConceptsStructure(dbProgramID());
}
static void unlock(Lock lock, String msg) {
if (lock == null) return;
lock.unlock();
vmBus_send("unlocked", lock, "thread" , currentThread());
print("Unlocked: " + msg); // print afterwards to make sure the lock is always unlocked
}
static void unlock(Lock lock) {
if (lock == null) return;
lock.unlock();
vmBus_send("unlocked", lock, "thread" , currentThread());
}
static void cleanKillVM() { try {
ping();
assertNotOnAWTThread();
cleanKillVM_noSleep();
Object o = new Object();
synchronized(o) { o.wait(); }
} catch (Exception __e) { throw rethrow(__e); } }
static void cleanKillVM_noSleep() {
call(getJavaX(), "cleanKill");
}
static int countConcepts(Concepts concepts, Class c, Object... params) {
return concepts.countConcepts(c, params);
}
static int countConcepts(Class c, Object... params) {
return db_mainConcepts().countConcepts(c, params);
}
static int countConcepts() {
return db_mainConcepts().countConcepts();
}
static int countConcepts(String className) {
return db_mainConcepts().countConcepts(className);
}
static int countConcepts(Concepts concepts, String className) {
return concepts.countConcepts(className);
}
static int countConcepts(Concepts concepts) {
return concepts.countConcepts();
}
static boolean containsKey(Map map, A key) {
return map != null && map.containsKey(key);
}
static boolean syncAdd(Collection c, A b) {
if (c == null) return false;
synchronized(collectionMutex(c)) { return c.add(b); }
}
static void syncAdd(List l, int idx, A b) {
if (l != null) synchronized(collectionMutex(l)) { l.add(idx, b); }
}
static boolean syncRemove(Collection c, A b) {
if (c == null) return false;
synchronized(collectionMutex(c)) { return c.remove(b); }
}
static A syncRemove(List l, int idx) {
if (l == null) return null;
synchronized(collectionMutex(l)) { return l.remove(idx); }
}
static B syncRemove(Map map, A key) {
return map == null ? null : map.remove(key);
}
static List syncAddOrCreate(List l, A a) {
if (l == null) l = syncList();
l.add(a);
return l;
}
static String nConcepts(long n) { return n2(n, "concept"); }
static String nConcepts(Collection l) { return nConcepts(l(l)); }
static String nConcepts(Map map) { return nConcepts(l(map)); }
static int identityHashCode(Object o) {
return System.identityHashCode(o);
}
static String shortDynamicClassName(Object o) {
if (o instanceof DynamicObject && ((DynamicObject) o).className != null)
return ((DynamicObject) o).className;
return shortClassName(o);
}
static boolean dynamicObjectIsLoading() {
return isUnstructuring();
}
static volatile Concepts mainConcepts; // Where we create new concepts
static Concepts db_mainConcepts() {
if (mainConcepts == null)
mainConcepts = newConceptsWithClassFinder(getDBProgramID());
return mainConcepts;
}
static void cleanMeUp_concepts() {
if (db_mainConcepts() != null) db_mainConcepts().cleanMeUp();
// mainConcepts = null; // TODO
}
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;
}
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);
}
}
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());
}
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());
}
static void set(BitSet bs, int idx) {
{ if (bs != null) bs.set(idx); }
}
// get purpose 1: access a list/array/map (safer version of x.get(y))
static A get(List l, int idx) {
return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}
// seems to conflict with other signatures
/*static B get(Map map, A key) {
ret map != null ? map.get(key) : null;
}*/
static A get(A[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : null;
}
// default to false
static boolean get(boolean[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : false;
}
// get purpose 2: access a field by reflection or a map
static Object get(Object o, String field) {
try {
if (o == null) return null;
if (o instanceof Class) return get((Class) o, field);
if (o instanceof Map)
return ((Map) o).get(field);
Field f = getOpt_findField(o.getClass(), field);
if (f != null) {
makeAccessible(f);
return f.get(o);
}
if (o instanceof DynamicObject)
return getOptDynOnly(((DynamicObject) o), field);
} catch (Exception e) {
throw asRuntimeException(e);
}
throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}
static Object get_raw(String field, Object o) {
return get_raw(o, field);
}
static Object get_raw(Object o, String field) { try {
if (o == null) return null;
Field f = get_findField(o.getClass(), field);
makeAccessible(f);
return f.get(o);
} catch (Exception __e) { throw rethrow(__e); } }
static Object get(Class c, String field) {
try {
Field f = get_findStaticField(c, field);
makeAccessible(f);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field get_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
static Field get_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static Object get(String field, Object o) {
return get(o, field);
}
static boolean get(BitSet bs, int idx) {
return bs != null && bs.get(idx);
}
static void assertIsInstance(Class type, Object o) {
if (!isInstance(type, o))
throw fail(_getClass(o) + " is not a subclass of " + type);
}
static void assertIsInstance(Object o, Class type) {
assertIsInstance(type, o);
}
static String b(Object contents, Object... params) {
return fulltag("b", contents, params);
}
static void change() {
//mainConcepts.allChanged();
// safe version for now cause function is sometimes included unnecessarily (e.g. by EGDiff)
callOpt(getOptMC("mainConcepts"), "allChanged");
}
static boolean has(String a, String b, String c) {
return false;
}
static boolean has(T3 t) {
return false;
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return c == null ? "null" : new String(c);
}
static String str(char[] c, int offset, int count) {
return new String(c, offset, count);
}
static boolean isEmpty(Collection c) {
return c == null || c.isEmpty();
}
static boolean isEmpty(File f) {
return f == null || f.length() == 0;
}
static boolean isEmpty(CharSequence s) {
return s == null || s.length() == 0;
}
static boolean isEmpty(Object[] a) { return a == null || a.length == 0; }
static boolean isEmpty(byte[] a) { return a == null || a.length == 0; }
static boolean isEmpty(Map map) {
return map == null || map.isEmpty();
}
static boolean isEmpty(AppendableChain c) { return c == null; }
static boolean isEmpty(IntSize l) { return l == null || l.size() == 0; }
static void removeLast(List l) {
if (!l.isEmpty())
l.remove(l(l)-1);
}
static void removeLast(List l, int n) {
removeSubList(l, l(l)-n);
}
static void removeLast(int n, List l) {
removeLast(l, n);
}
static void removeLast(StringBuilder buf) {
if (buf == null) return;
int n = buf.length();
if (n > 0) buf.setLength(n-1);
}
static void clear(Collection c) {
if (c != null) c.clear();
}
static void clear(Map map) {
if (map != null) map.clear();
}
static String unnullForIteration(String s) {
return s == null ? "" : s;
}
static Collection unnullForIteration(Collection l) {
return l == null ? immutableEmptyList() : l;
}
static List unnullForIteration(List l) { return l == null ? immutableEmptyList() : l; }
static byte[] unnullForIteration(byte[] l) { return l == null ? emptyByteArray() : l; }
static int[] unnullForIteration(int[] l) { return l == null ? emptyIntArray() : l; }
static char[] unnullForIteration(char[] l) { return l == null ? emptyCharArray() : l; }
static double[] unnullForIteration(double[] l) { return l == null ? emptyDoubleArray() : l; }
static short[] unnullForIteration(short[] l) { return l == null ? emptyShortArray() : l; }
static Map unnullForIteration(Map l) {
return l == null ? immutableEmptyMap() : l;
}
static Iterable unnullForIteration(Iterable i) {
return i == null ? immutableEmptyList() : i;
}
static A[] unnullForIteration(A[] a) {
return a == null ? (A[]) emptyObjectArray() : a;
}
static BitSet unnullForIteration(BitSet b) {
return b == null ? new BitSet() : b;
}
static Pt unnullForIteration(Pt p) {
return p == null ? new Pt() : p;
}
//ifclass Symbol
static Symbol unnullForIteration(Symbol s) {
return s == null ? emptySymbol() : s;
}
//endif
static Pair unnullForIteration(Pair p) {
return p != null ? p : new Pair(null, null);
}
static long unnullForIteration(Long l) { return l == null ? 0L : l; }
static void add(BitSet bs, int i) {
bs.set(i);
}
static boolean add(Collection c, A a) {
return c != null && c.add(a);
}
static void add(Container c, Component x) {
addToContainer(c, x);
}
static long add(AtomicLong l, long b) {
return l.addAndGet(b);
}
static A syncGet(List l, int idx) {
if (l == null || idx < 0) return null;
synchronized(l) {
return idx < l(l) ? l.get(idx) : null;
}
}
static B syncGet(Map map, A a) {
if (map == null) return null;
synchronized(map) {
return map.get(a);
}
}
static int syncL(Collection l) {
if (l == null) return 0;
synchronized(collectionMutex(l)) { return l.size(); }
}
static int syncL(Map map) {
if (map == null) return 0;
synchronized(collectionMutex(map)) { return map.size(); }
}
static List syncL() {
return syncList();
}
static List addDyn_quickSync(List l, A a) {
if (l == null) l = new ArrayList();
syncAdd(l, a);
return l;
}
static List removeDyn_quickSync(List l, A a) {
if (l == null) return null;
synchronized(collectionMutex(l)) {
l.remove(a);
return empty(l) ? null : l;
}
}
static Set> _entrySet(Map map) {
return map == null ? Collections.EMPTY_SET : map.entrySet();
}
static void crenameField_noOverwrite(Concept c, String oldField, String newField) {
if (c == null || eq(oldField, newField)) return;
Object value = cget(c, oldField);
if (newField != null && cget(c, newField) == null) cset(c, newField, value);
cset(c, oldField, null);
}
static Collection scanConceptForRefs(Concept c) {
Set refs = new HashSet();
if (c != null) for (Object o : values(objectToMap(c))) {
if (o instanceof Concept.Ref)
refs.add((Concept.Ref) o);
else if (o instanceof Concept.RefL)
addAll(refs, ((Concept.RefL) o).l);
}
return refs;
}
static Concepts concepts(Concept c) {
return c == null ? null : c._concepts;
}
static AutoCloseable tempInterceptPrintIfNotIntercepted(F1 f) {
return print_byThread().get() == null ? tempInterceptPrint(f) : null;
}
volatile static boolean conceptsAndBot_running = false;
static boolean conceptsAndBot_thinOnStart = true;
static void conceptsAndBot() {
conceptsAndBot(null);
}
static void conceptsAndBot(Integer autoSaveInterval) {
if (conceptsAndBot_running) return;
conceptsAndBot_running = true;
Concepts cc = db_mainConcepts();
cc.programID = getDBProgramID();
try {
if (cc.useFileLock) {
if (!cc.fileLock().tryToLock()) {
ensureDBNotRunning(dbBotStandardName());
cc.fileLock().forceLock();
}
} else
ensureDBNotRunning(dbBotStandardName());
} catch (Throwable e) { printStackTrace(e);
cc.dontSave = true; // SAFETY
throw rethrow(e); }
cc.persist(autoSaveInterval);
dbBot(false);
if (conceptsAndBot_thinOnStart) { try {
thinAProgramsBackups(getDBProgramID(), true);
} catch (Throwable __e) { pcallFail(__e); }}
}
static A or(A a, A b) {
return a != null ? a : b;
}
static Class> getClass(String name) {
return _getClass(name);
}
static Class getClass(Object o) {
return _getClass(o);
}
static Class getClass(Object realm, String name) {
return _getClass(realm, name);
}
static List lookupAllOpt(Map map, Collection l) {
List out = new ArrayList();
if (l != null) for (A a : l)
addIfNotNull(out, map.get(a));
return out;
}
static List lookupAllOpt(Collection l, Map map) {
return lookupAllOpt(map, l);
}
static > List getVars(Iterable l) {
return lambdaMap(__37 -> getVar(__37), l);
}
static void addAll(Collection c, Iterable b) {
if (c != null && b != null) for (A a : b) c.add(a);
}
static boolean addAll(Collection c, Collection b) {
return c != null && b != null && c.addAll(b);
}
static boolean addAll(Collection c, B... b) {
return c != null && b != null && c.addAll(Arrays.asList(b));
}
static Map addAll(Map a, Map extends A,? extends B> b) {
if (a != null && b != null) a.putAll(b);
return a;
}
static A addAll(A c, Collection extends Component> components) {
return addComponents(c, components);
}
static A addAll(A c, Component... components) {
return addComponents(c, components);
}
static String dropNumberPrefix(String s) {
return dropFirst(s, indexOfNonDigit(s));
}
static NavigableMap synchroNavigableMap(NavigableMap map) {
return Collections.synchronizedNavigableMap(map);
}
static Object defaultDefaultClassFinder() {
return new F1() {
public Class get(String name) {
// Fix some buggy concepts files out there
name = replacePrefix("main$main$", "main$", name);
Class c = get2(name);
return c;
}
Class get2(String name) {
// special invocation to find main class irrelevant of name
if (eq(name, "")) return mc();
{ Class c = findClass_fullName(name); if (c != null) return c; }
if (startsWithAny(name, "loadableUtils.utils$", "main$", mcDollar()))
for (String pkg : ll("loadableUtils.utils$", mcDollar())) {
String newName = pkg + afterDollar(name);
{ Class c = findClass_fullName(newName); if (c != null) return c; }
}
return null;
}
};
}
static Set syncLinkedHashSet() {
return synchroLinkedHashSet();
}
// TODO: if field is a Ref<>, you can thoretically use findBackRefs
static A findConceptWhere(Class c, Object... params) {
return findConceptWhere(db_mainConcepts(), c, params);
}
static A findConceptWhere(Concepts concepts, Class c, Object... params) {
ping();
params = expandParams(c, params);
// indexed
if (concepts.fieldIndices != null)
for (int i = 0; i < l(params); i += 2) {
IFieldIndex index = concepts.getFieldIndex(c, (String) params[i]);
if (index != null) {
for (A x : index.getAll(params[i+1]))
if (checkConceptFields(x, params)) return x;
return null;
}
}
// table scan
for (A x : concepts.list(c)) if (checkConceptFields(x, params)) return x;
return null;
}
static Concept findConceptWhere(Concepts concepts, String c, Object... params) {
for (Concept x : concepts.list(c)) if (checkConceptFields(x, params)) return x;
return null;
}
static String programIDWithCase() {
return nempty(caseID())
? programID() + "/" + quoteUnlessIdentifierOrInteger(caseID())
: programID();
}
static void readLocally(String progID, String varNames) {
readLocally2(mc(), progID, varNames);
}
static void readLocally(String varNames) {
readLocally2(mc(), programID(), varNames);
}
static void readLocally2(Object obj, String varNames) {
readLocally2(obj, programID(), varNames);
}
static int readLocally_stringLength;
static ThreadLocal readLocally2_allDynamic = new ThreadLocal();
static ThreadLocal readLocally2_classFinder = new ThreadLocal();
// read a string variable from standard storage
// does not overwrite variable contents if there is no file
static void readLocally2(Object obj, String progID, String varNames) { try {
boolean allDynamic = isTrue(getAndClearThreadLocal(readLocally2_allDynamic));
for (String variableName : javaTokC(varNames)) {
File textFile = new File(programDir(progID), variableName + ".text");
String value = loadTextFile(textFile);
if (value != null)
set(main.class, variableName, value);
else {
File structureFile = new File(programDir(progID), variableName + ".structure");
value = loadTextFile(structureFile);
if (value == null) {
File structureGZFile = new File(programDir(progID), variableName + ".structure.gz");
if (!structureGZFile.isFile()) return;
//value = loadGZTextFile(structureGZFile);
InputStream fis = new FileInputStream(structureGZFile); try {
GZIPInputStream gis = newGZIPInputStream(fis);
InputStreamReader reader = new InputStreamReader(gis, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(reader);
//O o = unstructure_reader(bufferedReader);
Object o = unstructure_tok(javaTokC_noMLS_onReader(bufferedReader), allDynamic, readLocally2_classFinder.get());
readLocally_set(obj, variableName, o);
return;
} finally { _close(fis); }}
readLocally_stringLength = l(value);
if (nempty(value))
readLocally_set(obj, variableName, unstructure(value, allDynamic, readLocally2_classFinder.get()));
}
}
} catch (Exception __e) { throw rethrow(__e); } }
static void readLocally_set(Object c, String varName, Object value) {
Object oldValue = get(c, varName);
if (oldValue instanceof List && !(oldValue instanceof ArrayList) && value != null) {
// Assume it's a synchroList.
value = synchroList((List) value);
}
set(c, varName, value);
}
static Class mc() {
return main.class;
}
static Map newDangerousWeakHashMap() {
return _registerDangerousWeakMap(synchroMap(new WeakHashMap()));
}
// initFunction: voidfunc(Map) - is called initially, and after clearing the map
static Map newDangerousWeakHashMap(Object initFunction) {
return _registerDangerousWeakMap(synchroMap(new WeakHashMap()), initFunction);
}
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); } }
static boolean call_checkArgs(Method m, Object[] args, boolean debug) {
Class>[] types = m.getParameterTypes();
if (types.length != l(args)) {
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;
}
static Field makeAccessible(Field f) {
try {
f.setAccessible(true);
} catch (Throwable e) {
// Note: The error reporting only works with Java VM option --illegal-access=deny
vmBus_send("makeAccessible_error", e, f);
}
return f;
}
static Method makeAccessible(Method m) {
try {
m.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, m);
}
return m;
}
static Constructor makeAccessible(Constructor c) {
try {
c.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, c);
}
return c;
}
static boolean cic(Collection l, String s) {
return containsIgnoreCase(l, s);
}
static boolean cic(Collection l, Symbol s) {
return contains(l, s);
}
static boolean cic(String[] l, String s) {
return containsIgnoreCase(l, s);
}
static boolean cic(String s, char c) {
return containsIgnoreCase(s, c);
}
static boolean cic(String a, String b) {
return containsIgnoreCase(a, b);
}
static String getInnerMessage(Throwable e) {
if (e == null) return null;
return getInnerException(e).getMessage();
}
static String exceptionToStringShort(Throwable e) {
try {
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;
} catch (Throwable _e) { printStackTrace(_e);
return "Error in exceptionToStringShort";
}
}
static String combinePrintParameters(String s, Object o) {
return (endsWithLetterOrDigit(s) ? s + ": " : s) + o;
}
static void ping_okInCleanUp() {
if (ping_pauseAll || ping_anyActions)
ping_impl(true);
}
// this syntax should be removed...
static Object getThreadLocal(Object o, String name) {
ThreadLocal t = (ThreadLocal) (getOpt(o, name));
return t != null ? t.get() : null;
}
static A getThreadLocal(ThreadLocal tl) {
return tl == null ? null : tl.get();
}
static A getThreadLocal(ThreadLocal tl, A defaultValue) {
return or(getThreadLocal(tl), defaultValue);
}
static ThreadLocal print_byThread_dontCreate() {
return print_byThread;
}
static String getStackTrace(Throwable throwable) {
lastException(throwable);
return getStackTrace_noRecord(throwable);
}
static String getStackTrace_noRecord(Throwable throwable) {
StringWriter writer = new StringWriter();
throwable.printStackTrace(new PrintWriter(writer));
return hideCredentials(writer.toString());
}
static String getStackTrace() {
return getStackTrace_noRecord(new Throwable());
}
static String getStackTrace(String msg) {
return getStackTrace_noRecord(new Throwable(msg));
}
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();
}
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); } }
static boolean fileExists(String path) {
return path != null && new File(path).exists();
}
static boolean fileExists(File f) {
return f != null && f.exists();
}
static BufferedReader utf8BufferedReader(InputStream in) {
return utf8bufferedReader(in);
}
static BufferedReader utf8BufferedReader(File f) {
return utf8bufferedReader(f);
}
static int gzInputStream_defaultBufferSize = 65536;
static GZIPInputStream gzInputStream(File f) { try {
return gzInputStream(new FileInputStream(f));
} catch (Exception __e) { throw rethrow(__e); } }
static GZIPInputStream gzInputStream(File f, int bufferSize) { try {
return gzInputStream(new FileInputStream(f), bufferSize);
} catch (Exception __e) { throw rethrow(__e); } }
static GZIPInputStream gzInputStream(InputStream in) {
return gzInputStream(in, gzInputStream_defaultBufferSize);
}
static GZIPInputStream gzInputStream(InputStream in, int bufferSize) { try {
return _registerIOWrap(new GZIPInputStream(in, gzInputStream_defaultBufferSize), in);
} catch (Exception __e) { throw rethrow(__e); } }
static Producer javaTokC_noMLS_onReader(final BufferedReader r) {
final class X implements Producer {
StringBuilder buf = new StringBuilder(); // stores from "i"
char c, d, e = 'x'; // just not '\0'
X() {
// fill c, d and e
nc();
nc();
nc();
}
// get next character(s) into c, d and e
void nc() { try {
c = d;
d = e;
if (e == '\0') return;
int i = r.read();
e = i < 0 ? '\0'
: i == '\0' ? '_' // shouldn't happen anymore
: (char) i;
} catch (Exception __e) { throw rethrow(__e); } }
void ncSave() {
if (c != '\0') {
buf.append(c);
nc();
}
}
public String next() {
// scan for whitespace
while (c != '\0') {
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
nc();
else if (c == '/' && d == '*') {
do nc(); while (c != '\0' && !(c == '*' && d == '/'));
nc(); nc();
} else if (c == '/' && d == '/') {
do nc(); while (c != '\0' && "\r\n".indexOf(c) < 0);
} else
break;
}
if (c == '\0') return null;
// scan for non-whitespace
if (c == '\'' || c == '"') {
char opener = c;
ncSave();
while (c != '\0') {
if (c == opener || c == '\n') { // end at \n to not propagate unclosed string literal errors
ncSave();
break;
} else if (c == '\\') {
ncSave();
ncSave();
} else
ncSave();
}
} else if (Character.isJavaIdentifierStart(c))
do ncSave(); while (Character.isJavaIdentifierPart(c) || c == '\''); // for stuff like "don't"
else if (Character.isDigit(c)) {
do ncSave(); while (Character.isDigit(c));
if (c == 'L') ncSave(); // Long constants like 1L
} else
ncSave();
String t = buf.toString();
buf.setLength(0);
return t;
}
}
return new X();
}
static String conceptsFileName() {
return "concepts.structure.gz";
}
static String dbProgramID() {
return getDBProgramID();
}
static boolean isString(Object o) {
return o instanceof String;
}
static HashMap litmap(Object... x) {
HashMap map = new HashMap();
litmap_impl(map, x);
return map;
}
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]);
}
static String formatWithThousands(long l) {
return formatWithThousandsSeparator(l);
}
static String plural(String s) {
return getPlural(s);
}
static double fraction(double d) {
return d % 1;
}
static String n_fancy2(long l, String singular, String plural) {
return formatWithThousandsSeparator(l) + " " + trim(l == 1 ? singular : plural);
}
static String n_fancy2(Collection l, String singular, String plural) {
return n_fancy2(l(l), singular, plural);
}
static String n_fancy2(Map m, String singular, String plural) {
return n_fancy2(l(m), singular, plural);
}
static String n_fancy2(Object[] a, String singular, String plural) {
return n_fancy2(l(a), singular, plural);
}
static String n_fancy2(MultiSet ms, String singular, String plural) {
return n_fancy2(l(ms), singular, plural);
}
static int iteratorCount_int_close(Iterator i) { try {
int n = 0;
if (i != null) while (i.hasNext()) { i.next(); ++n; }
if (i instanceof AutoCloseable) ((AutoCloseable) i).close();
return n;
} catch (Exception __e) { throw rethrow(__e); } }
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;
}
static Producer javaTokC_noMLS_iterator(final String s) {
return javaTokC_noMLS_iterator(s, 0);
}
static Producer javaTokC_noMLS_iterator(final String s, final int startIndex) {
return new Producer() {
final int l = s.length();
int i = startIndex;
public String next() {
if (i >= l) return null;
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;
}
i = j;
if (i >= l) return null;
c = s.charAt(i);
d = i+1 >= l ? '\0' : s.charAt(i+1);
// scan for non-whitespace
if (c == '\'' || c == '"') {
char opener = c;
++j;
while (j < l) {
if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
++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)));
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
++j;
String t = quickSubstring(s, i, j);
i = j;
return t;
}
};
}
static String actualMCDollar() {
return actualMC().getName() + "$";
}
static BigInteger parseBigInt(String s) {
return new BigInteger(s);
}
static double parseDouble(String s) {
return empty(s) ? 0.0 : Double.parseDouble(s);
}
static float parseFloat(String s) {
return Float.parseFloat(s);
}
static int parseInt(String s) {
return emptyString(s) ? 0 : Integer.parseInt(s);
}
static int parseInt(char c) {
return Integer.parseInt(str(c));
}
static void assertTrue(Object o) {
if (!(eq(o, true) /*|| isTrue(pcallF(o))*/))
throw fail(str(o));
}
static boolean assertTrue(String msg, boolean b) {
if (!b)
throw fail(msg);
return b;
}
static boolean assertTrue(boolean b) {
if (!b)
throw fail("oops");
return b;
}
static boolean assertTrue(Scorer scorer, boolean b) {
scorer.add(b);
return b;
}
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;
}
static String trivialUnquote(String s) {
return dropPrefixSuffix("\"", s);
}
static short[] shortArrayFromBytes(byte[] a) {
return shortArrayFromBytes(a, 0, l(a));
}
static short[] shortArrayFromBytes(byte[] a, int from, int to) {
short[] b = new short[(to-from)/2];
for (int i = 0; i < b.length; i++)
b[i] = shortFromBytes(a, from+i*2);
return b;
}
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;
}
static long[] longArrayFromBytes(byte[] a) {
return longArrayFromBytes(a, 0, l(a));
}
static long[] longArrayFromBytes(byte[] a, int from, int to) {
long[] b = new long[(to-from)/8];
for (int i = 0; i < b.length; i++)
b[i] = longFromBytes_bigEndian(a, from+i*8);
return b;
}
static HashMap findClass_fullName_cache = new HashMap();
// returns null on not found
// this is the simple version that is not case-tolerant
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;
}
}
static String unquoteUsingCharArray(String s, char[] buf) {
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();
if (l > buf.length) return unquote(s); // fallback
int n = 0;
for (int i = 1; i < l; i++) {
char ch = s.charAt(i);
if (ch == '\\') {
char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1);
// Octal escape?
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++;
}
}
buf[n++] = (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;
// Hex Unicode: u????
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);
char[] x = Character.toChars(code);
int lx = x.length;
for (int j = 0; j < lx; j++)
buf[n++] = x[j];
i += 5;
continue;
default:
ch = nextChar; // added by Stefan
}
i++;
}
buf[n++] = ch;
}
return new String(buf, 0, n);
}
}
return s; // not quoted - return original
}
static boolean structure_isMarker(String s, int i, int j) {
if (i >= j) return false;
if (s.charAt(i) != 'm') return false;
++i;
while (i < j) {
char c = s.charAt(i);
if (c < '0' || c > '9') return false;
++i;
}
return true;
}
static String internIfLongerThan(String s, int l) {
return s == null ? null : l(s) >= l ? intern(s) : s;
}
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);
}
}
return unquoteSingleOrDoubleQuotes(s);
}
static char unquoteCharacter(String s) {
assertTrue(s.startsWith("'") && s.length() > 1);
return unquote("\"" + s.substring(1, s.endsWith("'") ? s.length()-1 : s.length()) + "\"").charAt(0);
}
static boolean isLongConstant(String s) {
if (!s.endsWith("L")) return false;
s = s.substring(0, l(s)-1);
return isInteger(s);
}
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;
}
static boolean eqOneOf(Object o, Object... l) {
if (l != null) for (Object x : l) if (eq(o, x)) return true; return false;
}
static TreeMap ciMap() {
return caseInsensitiveMap();
}
static List parseList(String s) {
return (List) safeUnstructure(s);
}
static List synchroLinkedList() {
return synchroList(new LinkedList ());
}
static SortedMap synchroSortedMap(SortedMap map) {
return Collections.synchronizedSortedMap(map);
}
static Map synchroMap() {
return synchroHashMap();
}
static Map synchroMap(Map map) {
return Collections.synchronizedMap(map);
}
static boolean[] boolArrayFromBytes(byte[] a, int n) {
boolean[] b = new boolean[n];
int m = min(n, l(a)*8);
for (int i = 0; i < m; i++)
b[i] = (a[i/8] & 1 << (i & 7)) != 0;
return b;
}
static Object call(Object o) {
return callF(o);
}
// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] {arg});
}
static Object call(Object o, String method, Object... args) {
//ret call_cached(o, method, args);
return call_withVarargs(o, method, args);
}
static String quote(Object o) {
if (o == null) return "null";
return quote(str(o));
}
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();
}
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 == '\t')
out.append("\\t");
else if (c == '\0')
out.append("\\0");
else
out.append(c);
}
out.append('"');
}
static String assertIdentifier(String s) {
return assertIsIdentifier(s);
}
static String assertIdentifier(String msg, String s) {
return assertIsIdentifier(msg, s);
}
static boolean isAbstract(Class c) {
return (c.getModifiers() & Modifier.ABSTRACT) != 0;
}
static boolean isAbstract(Method m) {
return (m.getModifiers() & Modifier.ABSTRACT) != 0;
}
// Use like this: printVars_str(+x, +y);
// Or: printVars("bla", +x);
// Or: printVars bla(+x);
static void printVars_str(Object... params) {
print(renderVars_str(params));
}
static boolean startsWith(String a, String b) {
return a != null && a.startsWith(unnull(b));
}
static boolean startsWith(String a, char c) {
return nemptyString(a) && a.charAt(0) == c;
}
static boolean startsWith(String a, String b, Matches m) {
if (!startsWith(a, b)) return false;
if (m != null) m.m = new String[] {substring(a, strL(b))};
return true;
}
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;
}
static Constructor nuStubInnerObject_findConstructor(Class c) { return nuStubInnerObject_findConstructor(c, null); }
static Constructor nuStubInnerObject_findConstructor(Class c, Object classFinder) { try {
Class outerType = getOuterClass(c, classFinder);
Constructor m = c.getDeclaredConstructor(outerType);
makeAccessible(m);
return m;
} catch (Exception __e) { throw rethrow(__e); } }
static Map nuEmptyObject_cache = newDangerousWeakHashMap();
static A nuEmptyObject(Class c) { try {
Constructor ctr;
synchronized(nuEmptyObject_cache) {
ctr = nuEmptyObject_cache.get(c);
if (ctr == null) {
nuEmptyObject_cache.put(c, ctr = nuEmptyObject_findConstructor(c));
makeAccessible(ctr);
}
}
try {
return (A) ctr.newInstance();
} catch (InstantiationException e) {
if (empty(e.getMessage()))
if ((c.getModifiers() & Modifier.ABSTRACT) != 0)
throw fail("Can't instantiate abstract class " + className(c), e);
else
throw fail("Can't instantiate " + className(c), e);
else throw rethrow(e);
}
} catch (Exception __e) { throw rethrow(__e); } }
static Constructor nuEmptyObject_findConstructor(Class c) {
for (Constructor m : getDeclaredConstructors_cached(c))
if (m.getParameterTypes().length == 0)
return m;
throw fail("No default constructor declared in " + c.getName());
}
static void setOptAllDyn_pcall(DynamicObject o, Map fields) {
if (fields == null || o == null) return;
HashMap fieldMap = instanceFieldsMap(o);
for (Map.Entry e : fields.entrySet()) { try {
String field = e.getKey();
Object val = e.getValue();
Field f = fieldMap.get(field);
if (f != null)
smartSet(f, o, val);
else {
dynamicObject_setRawFieldValue(o, intern(field), val);
}
} catch (Throwable __e) { pcallFail(__e); }}
}
static void setOptAll_pcall(Object o, Map fields) {
if (fields == null) return;
for (String field : keys(fields))
try { setOpt(o, field, fields.get(field)); } catch (Throwable __e) { print(exceptionToStringShort(__e)); }
}
static void setOptAll_pcall(Object o, Object... values) {
//values = expandParams(c.getClass(), values);
warnIfOddCount(values);
for (int i = 0; i+1 < l(values); i += 2) {
String field = (String) values[i];
Object value = values[i+1];
try { setOpt(o, field, value); } catch (Throwable __e) { print(exceptionToStringShort(__e)); }
}
}
static void fixOuterRefs(Object o) { try {
if (o == null) return;
Field[] l = thisDollarOneFields(o.getClass());
if (l.length <= 1) return;
Object father = null;
for (Field f : l) {
father = f.get(o);
if (father != null) break;
}
if (father == null) return;
for (Field f : l)
f.set(o, father);
} catch (Exception __e) { throw rethrow(__e); } }
static void setDynObjectValue(DynamicObject o, String field, Object value) {
dynamicObject_setRawFieldValue(o, field, value);
}
static String intern(String s) {
return fastIntern(s);
}
static void pcallOpt_noArgs(Object o, String method) {
try { callOpt_noArgs(o, method); } catch (Throwable __e) { pcallFail(__e); }
}
static List subList(List l, int startIndex) {
return subList(l, startIndex, l(l));
}
static List subList(int startIndex, List l) {
return subList(l, startIndex);
}
static List subList(int startIndex, int endIndex, List l) {
return subList(l, startIndex, endIndex);
}
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 RuntimeException todo() {
throw new RuntimeException("TODO");
}
static RuntimeException todo(Object msg) {
throw new RuntimeException("TODO: " + msg);
}
static Object newMultiDimensionalOuterArray(Class elementType, int dimensions, int length) {
int[] dims = new int[dimensions];
dims[0] = length;
return Array.newInstance(elementType, dims);
}
static int[] toIntArray(Collection l) {
int[] a = new int[l(l)];
int i = 0;
if (a.length != 0) for (int x : l)
a[i++] = x;
return a;
}
static double[] toDoubleArray(Collection l) {
double[] a = new double[l(l)];
int i = 0;
if (a.length != 0) for (double x : l)
a[i++] = x;
return a;
}
static double[] toDoubleArray(float... l) {
double[] a = new double[l(l)];
for (int i = 0; i < a.length; i++)
a[i] = l[i];
return a;
}
static double[] toDoubleArray(int... l) {
double[] a = new double[l(l)];
for (int i = 0; i < a.length; i++)
a[i] = l[i];
return a;
}
static float[] toFloatArray(List l) {
float[] a = new float[l(l)];
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
return a;
}
static float[] toFloatArray(double[] l) {
float[] a = new float[l(l)];
for (int i = 0; i < a.length; i++)
a[i] = (float) l[i];
return a;
}
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;
}
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));
}
static String dropPrefix(String prefix, String s) {
return s == null ? null : s.startsWith(prefix) ? s.substring(l(prefix)) : s;
}
static void dynamicObject_setRawFieldValue(DynamicObject o, Object key, Object value) {
if (o == null) return;
// double sync, but should be OK here because of locking order o > o.fieldValues
synchronized(o) {
o.fieldValues = syncMapPut2_createLinkedHashMap((LinkedHashMap) o.fieldValues, key, value);
}
}
static TreeSet ciSet() {
return caseInsensitiveSet();
}
// DIFFERENCES to jfind: always ignores case, doesn't recognize etc
// You probably want jmatch2
static boolean jmatch(String pat, String s) {
return jmatch(pat, s, null);
}
static boolean jmatch(String pat, String s, Matches matches) {
if (s == null) return false;
return jmatch(pat, javaTok(s), matches);
}
static boolean jmatch(String pat, List toks) {
return jmatch(pat, toks, null);
}
static boolean jmatch(String pat, List toks, Matches matches) {
List tokpat = javaTok(pat);
String[] m = match2(tokpat, toks);
//print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
if (m == null)
return false;
else {
if (matches != null) matches.m = m;
return true;
}
}
static Object nuObject(String className, Object... args) { try {
return nuObject(classForName(className), args);
} catch (Exception __e) { throw rethrow(__e); } }
// too ambiguous - maybe need to fix some callers
/*static O nuObject(O realm, S className, O... args) {
ret nuObject(_getClass(realm, className), args);
}*/
static A nuObject(Class c, Object... args) { try {
if (args == null || args.length == 0) return nuObjectWithoutArguments(c); // cached!
Constructor m = nuObject_findConstructor(c, args);
makeAccessible(m);
return (A) m.newInstance(args);
} catch (Exception __e) { throw rethrow(__e); } }
static Constructor nuObject_findConstructor(Class c, Object... args) {
for (Constructor m : getDeclaredConstructors_cached(c)) {
if (!nuObject_checkArgs(m.getParameterTypes(), args, false))
continue;
return m;
}
throw fail("Constructor " + c.getName() + getClasses(args) + " not found"
+ (args.length == 0 && (c.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 ? " - hint: it's a non-static class!" : ""));
}
static boolean nuObject_checkArgs(Class[] types, Object[] args, boolean debug) {
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 A popLast(List l) {
return liftLast(l);
}
static List popLast(int n, List l) {
return liftLast(n, l);
}
static AutoCloseable tempSetTL(ThreadLocal tl, A a) {
return tempSetThreadLocal(tl, a);
}
static AutoCloseable tempSetTL(BetterThreadLocal tl, A a) {
return tempSetThreadLocalIfNecessary(tl, a);
}
static Iterator iterator(Iterable c) {
return c == null ? emptyIterator() : c.iterator();
}
static int cmp(Number a, Number b) {
return a == null ? b == null ? 0 : -1 : cmp(a.doubleValue(), b.doubleValue());
}
static int cmp(double a, double b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(int a, int b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(long a, long b) {
return a < b ? -1 : a == b ? 0 : 1;
}
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);
}
static ArrayList emptyList() {
return new ArrayList();
//ret Collections.emptyList();
}
static ArrayList emptyList(int capacity) {
return new ArrayList(max(0, capacity));
}
// Try to match capacity
static ArrayList emptyList(Iterable l) {
return l instanceof Collection ? emptyList(((Collection) l).size()) : emptyList();
}
static ArrayList emptyList(Object[] l) {
return emptyList(l(l));
}
// get correct type at once
static ArrayList emptyList(Class c) {
return new ArrayList();
}
static Pair pair(A a, B b) {
return new Pair(a, b);
}
static Pair pair(A a) {
return new Pair(a, a);
}
static String getType(Object o) {
return getClassName(o);
}
static long getFileSize(String path) {
return path == null ? 0 : new File(path).length();
}
static long getFileSize(File f) {
return f == null ? 0 : f.length();
}
static boolean isInstance(Class type, Object arg) {
return type.isInstance(arg);
}
static String dropSuffix(String suffix, String s) {
return nempty(suffix) && endsWith(s, suffix) ? s.substring(0, l(s)-l(suffix)) : s;
}
static File getProgramDir() {
return programDir();
}
static File getProgramDir(String snippetID) {
return programDir(snippetID);
}
static String programID;
static String getProgramID() {
return nempty(programID) ? formatSnippetIDOpt(programID) : "?";
}
// TODO: ask JavaX instead
static String getProgramID(Class c) {
String id = (String) getOpt(c, "programID");
if (nempty(id))
return formatSnippetID(id);
return "?";
}
static String getProgramID(Object o) {
return getProgramID(getMainClass(o));
}
static Object vmBus_wrapArgs(Object... args) {
return empty(args) ? null
: l(args) == 1 ? args[0]
: args;
}
static void pcallFAll_minimalExceptionHandling(Collection l, Object... args) {
if (l != null) for (Object f : cloneList(l)) { ping(); pcallF_minimalExceptionHandling(f, args); }
}
static void pcallFAll_minimalExceptionHandling(Iterator it, Object... args) {
while (it.hasNext()) { ping(); pcallF_minimalExceptionHandling(it.next(), args); }
}
static Set vm_busListeners_live_cache;
static Set vm_busListeners_live() { if (vm_busListeners_live_cache == null) vm_busListeners_live_cache = vm_busListeners_live_load(); return vm_busListeners_live_cache;}
static Set vm_busListeners_live_load() {
return vm_generalIdentityHashSet("busListeners");
}
static Map vm_busListenersByMessage_live_cache;
static Map vm_busListenersByMessage_live() { if (vm_busListenersByMessage_live_cache == null) vm_busListenersByMessage_live_cache = vm_busListenersByMessage_live_load(); return vm_busListenersByMessage_live_cache;}
static Map vm_busListenersByMessage_live_load() {
return vm_generalHashMap("busListenersByMessage");
}
static List callF_all(Collection l, Object... args) {
return map(l, f -> callF(f, args));
}
static File tempFileFor(File f) {
return new File(f.getPath() + "_temp");
}
static FileOutputStream newFileOutputStream(File path) throws IOException {
return newFileOutputStream(path.getPath());
}
static FileOutputStream newFileOutputStream(String path) throws IOException {
return newFileOutputStream(path, false);
}
static FileOutputStream newFileOutputStream(File path, boolean append) throws IOException {
return newFileOutputStream(path.getPath(), append);
}
static FileOutputStream newFileOutputStream(String path, boolean append) throws IOException {
mkdirsForFile(path);
FileOutputStream f = new FileOutputStream(path, append);
_registerIO(f, path, true);
return f;
}
static void _handleError(Error e) {
//call(javax(), '_handleError, e);
}
static String formatDouble(double d, int digits) {
String format = digits <= 0 ? "0" : "0." + rep(digits, '#');
return decimalFormatEnglish(format, d);
}
static String formatDouble(double d) {
return str(d);
}
static double toM_double(long l) {
return l/(1024*1024.0);
}
public static File mkdirsForFile(File file) {
File dir = file.getParentFile();
if (dir != null) { // is null if file is in current dir
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;
}
// TODO: extended multi-line strings
static int javaTok_n, javaTok_elements;
static boolean javaTok_opt = false;
static List javaTok(String s) {
++javaTok_n;
ArrayList tok = new ArrayList();
int l = s == null ? 0 : s.length();
int i = 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 && !regionMatches(s, j, "*/"));
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));
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) {
int c2 = s.charAt(j);
if (c2 == opener || c2 == '\n' && opener == '\'') { // allow multi-line strings, but not for '
++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) == '\'')); // 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 < l && !regionMatches(s, j, "]]"));
j = Math.min(j+2, l);
} else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
do ++j; while (j+2 < l && !regionMatches(s, j, "]=]"));
j = Math.min(j+3, l);
} else
++j;
tok.add(javaTok_substringC(s, i, j));
i = j;
}
if ((tok.size() % 2) == 0) tok.add("");
javaTok_elements += tok.size();
return tok;
}
static List javaTok(List tok) {
return javaTokWithExisting(join(tok), tok);
}
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)); }
static boolean containsNewLine(String s) {
return contains(s, '\n'); // screw \r, nobody needs it
}
public static String rtrimSpaces(String s) {
if (s == null) return null;
int i = s.length();
while (i > 0 && " \t".indexOf(s.charAt(i-1)) >= 0)
--i;
return i < s.length() ? s.substring(0, i) : s;
}
public static String join(String glue, Iterable strings) {
if (strings == null) return "";
if (strings instanceof Collection) {
if (((Collection) strings).size() == 1) return strOrEmpty(first((Collection) strings));
}
StringBuilder buf = new StringBuilder();
Iterator i = strings.iterator();
if (i.hasNext()) {
buf.append(strOrEmpty(i.next()));
while (i.hasNext())
buf.append(glue).append(strOrEmpty(i.next()));
}
return buf.toString();
}
public static String join(String glue, String... strings) {
return join(glue, Arrays.asList(strings));
}
public static String join(String glue, Object... strings) {
return join(glue, Arrays.asList(strings));
}
static String join(Iterable strings) {
return join("", strings);
}
static String join(Iterable strings, String glue) {
return join(glue, strings);
}
public static String join(String[] strings) {
return join("", strings);
}
static String join(String glue, Pair p) {
return p == null ? "" : str(p.a) + glue + str(p.b);
}
static int year() {
return localYear();
}
static int year(Timestamp now) {
return localYear(toLong(now));
}
static int year(long now) {
return localYear(now);
}
static int year(long now, TimeZone tz) {
return parseInt(simpleDateFormat("y", tz).format(now));
}
static int month() {
return localMonth();
}
static int month(Timestamp now) {
return localMonth(toLong(now));
}
static int month(long now) {
return localMonth(now);
}
static int month(long now, TimeZone tz) {
return parseInt(simpleDateFormat("M", tz).format(now));
}
static int dayOfMonth() {
return localDayOfMonth();
}
static int dayOfMonth(long now) {
return localDayOfMonth(now);
}
static int dayOfMonth(long now, TimeZone tz) {
return parseInt(simpleDateFormat("d", tz).format(now));
}
static String padLeft(String s, char c, int n) {
return rep(c, n-l(s)) + s;
}
// default to space
static String padLeft(String s, int n) {
return padLeft(s, ' ', n);
}
static java.util.Calendar calendarFromTime(long time, TimeZone tz) {
java.util.Calendar c = java.util.Calendar.getInstance(tz);
c.setTimeInMillis(time);
return c;
}
static java.util.Calendar calendarFromTime(long time) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTimeInMillis(time);
return c;
}
// TODO: optimize to x-(x%n) in case that's the same thing
// (or x-mod(x,n)?)
static int roundDownTo(int n, int x) {
return x/n*n;
}
static long roundDownTo(long n, long x) {
return x/n*n;
}
static double roundDownTo(double n, double x) {
return floor(x/n)*n;
}
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); } }
static String trim(String s) { return s == null ? null : s.trim(); }
static String trim(StringBuilder buf) { return buf.toString().trim(); }
static String trim(StringBuffer buf) { return buf.toString().trim(); }
static Map singular_specials = litmap(
"children", "child", "images", "image", "chess", "chess");
static Set singular_specials2 = litciset("time", "machine", "line", "rule");
static String singular(String s) {
if (s == null) return null;
{ String __1 = singular_specials.get(s); if (!empty(__1)) return __1; }
//try answer hippoSingulars().get(lower(s));
if (singular_specials2.contains(dropSuffix("s", afterLastSpace(s))))
return dropSuffix("s", s);
if (s.endsWith("ness")) return s;
if (s.endsWith("ges")) return dropSuffix("s", s);
if (endsWith(s, "bases")) return dropLast(s);
s = dropSuffix("es", s);
s = dropSuffix("s", s);
return s;
}
static Set