import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
import java.text.*;
import java.text.NumberFormat;
import java.nio.charset.Charset;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
class main {
static class Bla extends Concept {
int a, b;
}
static void test_conceptsWithFieldLessThan() {
Concepts cc = new Concepts();
indexConceptFieldDesc(cc, Bla.class, "a");
Bla x = cnew(cc, Bla.class, "a" , 3, "b" , 3);
for (int i : ll(5, 7))
cnew(cc, Bla.class, "a" , i, "b" , i);
assertEqualsVerbose(ll(x), conceptsWithFieldLessThan(cc, Bla.class, "a" , 5));
assertEqualsVerbose(ll(x), conceptsWithFieldLessThan(cc, Bla.class, "b" , 5));
}
static void indexConceptFieldDesc(Class extends Concept> c, String field) {
indexConceptFieldDesc(db_mainConcepts(), c, field);
}
// if there is a non-ordered index, it keeps that one
static void indexConceptFieldDesc(Concepts concepts, Class extends Concept> c, String field) {
if (!isConceptFieldIndexed(concepts, c, field))
new ConceptFieldIndexDesc(concepts, c, field);
}
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);
}
concepts.register(c);
csetAll(c, values);
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);
}
concepts.register(c);
csetAll(c, values);
return c;
}
static List ll(A... a) {
ArrayList l = new ArrayList(a.length);
if (a != null) for (A x : a) l.add(x);
return l;
}
static A assertEqualsVerbose(Object x, A y) {
assertEqualsVerbose((String) null, x, y);
return y;
}
// x = expected, y = actual
static A assertEqualsVerbose(String msg, Object x, A y) {
if (!eq(x, y)) {
throw fail((msg != null ? msg + ": " : "") + "expected: "+ x + ", got: " + y);
} else
print("OK" + (empty(msg) ? "" : " " + msg) + ": " + /*sfu*/(x));
return y;
}
static boolean conceptsSortedByField_verbose = false;
// tries to use index if possible
// result is sorted if index was used
// result is always an independent collection
static Collection conceptsWithFieldLessThan (Class c, String field, Object value) {
return conceptsWithFieldLessThan(db_mainConcepts(), c, field, value);
}
static Collection conceptsWithFieldLessThan(Concepts concepts, Class c, String field, Object value) {
// indexed
IFieldIndex index = concepts.getFieldIndex(c, field);
if (index instanceof ConceptFieldIndexDesc) synchronized(((ConceptFieldIndexDesc) index).mutex()) {
return reverseInPlace(valuesList(((NavigableMap) ((ConceptFieldIndexDesc) index).valueToObject).tailMap(value, false)));
}
// filter manually
return filter(concepts.list(c), o -> cmp(getOpt(o, field), value) < 0);
}
static volatile Concepts mainConcepts; // Where we create new concepts
static Concepts db_mainConcepts() {
if (mainConcepts == null)
mainConcepts = newConceptsWithClassFinder(getDBProgramID());
return mainConcepts;
}
static boolean isConceptFieldIndexed(Class extends Concept> c, String field) {
return isConceptFieldIndexed(db_mainConcepts(), c, field);
}
static boolean isConceptFieldIndexed(Concepts concepts, Class extends Concept> c, String field) {
return concepts.getFieldIndex(c, field) != null;
}
static HashMap findClass_cache = new HashMap();
// currently finds only inner classes of class "main"
// returns null on not found
// this is the simple version that is not case-tolerant
static Class findClass(String name) {
synchronized(findClass_cache) {
if (findClass_cache.containsKey(name))
return findClass_cache.get(name);
if (!isJavaIdentifier(name)) return null;
Class c;
try {
c = Class.forName("main$" + name);
} catch (ClassNotFoundException e) {
c = null;
}
findClass_cache.put(name, c);
return c;
}
}
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.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 : c.getDeclaredConstructors()) {
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 int csetAll(Concept c, Object... values) {
return cset(c, values);
}
static int csetAll(Iterable extends Concept> l, Object... values) {
int n = 0;
for (Concept c : unnull(l))
n += cset(c, values);
return n;
}
static boolean eq(Object a, Object b) {
return a == b || a != null && b != null && a.equals(b);
}
static RuntimeException fail() { throw new RuntimeException("fail"); }
static RuntimeException fail(Throwable e) { throw asRuntimeException(e); }
static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); }
static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); }
static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); }
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal