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;
import static x30_pkg.x30_util.DynamicObject;
import java.nio.file.Path;
import java.awt.geom.*;
import java.text.NumberFormat;
import java.text.*;
import java.nio.charset.Charset;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
class main {
static class G22ScreenPart extends ConceptWithChangeListeners {
static final String _fieldOrder = "name bounds";
String name;
Rect bounds;
public String toString() {
return commaCombine(name, str_megapixels(bounds));
}
}
static String commaCombine(Object... l) {
return joinNemptiesWithComma(flattenCollectionsAndArrays(l));
}
static String str_megapixels(BufferedImage img) {
return formatDouble(megapixels(img), 1) + " MP";
}
static String str_megapixels(WidthAndHeight img) {
return formatDouble(megapixels(img), 1) + " MP";
}
static String joinNemptiesWithComma(Object... strings) {
return joinNempties(", ", strings);
}
static String joinNemptiesWithComma(Iterable strings) {
return joinNempties(", ", strings);
}
static List flattenCollectionsAndArrays(Iterable a) {
List l = new ArrayList();
for (Object x : a)
if (x instanceof Collection)
l.addAll(flattenCollectionsAndArrays((Collection) x));
else if (x instanceof Object[])
l.addAll(flattenCollectionsAndArrays(asList((Object[]) x)));
else
l.add(x);
return l;
}
static List flattenCollectionsAndArrays(Object... whatever) {
return flattenCollectionsAndArrays(ll(whatever));
}
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 megapixels(BufferedImage img) {
return img == null ? 0 : area(imageSize(img))/1_000_000.0;
}
static double megapixels(WidthAndHeight img) {
return img == null ? 0 : area(imageSize(img))/1_000_000.0;
}
static String joinNempties(String sep, Object... strings) {
return joinStrings(sep, strings);
}
static String joinNempties(String sep, Iterable strings) {
return joinStrings(sep, strings);
}
// unclear semantics as to whether return null on null
static ArrayList asList(A[] a) {
return a == null ? new ArrayList() : new ArrayList(Arrays.asList(a));
}
static ArrayList asList(byte[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (var i : a) l.add(i);
return l;
}
static ArrayList asList(int[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (int i : a) l.add(i);
return l;
}
static ArrayList asList(long[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (long i : a) l.add(i);
return l;
}
static ArrayList asList(float[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (float i : a) l.add(i);
return l;
}
static ArrayList asList(double[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (double i : a) l.add(i);
return l;
}
static ArrayList asList(short[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (short i : a) l.add(i);
return l;
}
static ArrayList asList(Iterator it) {
ArrayList l = new ArrayList();
if (it != null)
while (it.hasNext())
l.add(it.next());
return l;
}
// disambiguation
static ArrayList asList(IterableIterator s) {
return asList((Iterator) s);
}
static ArrayList asList(Iterable s) {
if (s instanceof ArrayList) return (ArrayList) s;
ArrayList l = new ArrayList();
if (s != null)
for (A a : s)
l.add(a);
return l;
}
static ArrayList asList(Producer p) {
ArrayList l = new ArrayList();
A a;
if (p != null) while ((a = p.next()) != null)
l.add(a);
return l;
}
static ArrayList asList(Enumeration e) {
ArrayList l = new ArrayList();
if (e != null)
while (e.hasMoreElements())
l.add(e.nextElement());
return l;
}
static ArrayList asList(ReverseChain c) {
return c == null ? emptyList() : c.toList();
}
static List asList(Pair p) {
return p == null ? null : ll(p.a, p.b);
}
static List ll(A... a) {
ArrayList l = new ArrayList(a.length);
if (a != null) for (A x : a) l.add(x);
return l;
}
static String rep(int n, char c) {
return repeat(c, n);
}
static String rep(char c, int n) {
return repeat(c, n);
}
static List rep(A a, int n) {
return repeat(a, n);
}
static List rep(int n, A a) {
return repeat(n, a);
}
static String decimalFormatEnglish(String format, double d) {
return decimalFormatEnglish(format).format(d);
}
static java.text.DecimalFormat decimalFormatEnglish(String format) {
return new java.text.DecimalFormat(format, new java.text.DecimalFormatSymbols(Locale.ENGLISH));
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static String str(char[] c, int offset, int count) {
return new String(c, offset, count);
}
static int area(Rect r) {
return rectArea(r);
}
static double area(DoubleRect r) {
return r == null ? 0 : r.w*r.h;
}
static int area(WidthAndHeight img) {
return img == null ? 0 : img.getWidth()*img.getHeight();
}
static int area(BufferedImage img) {
return img == null ? 0 : img.getWidth()*img.getHeight();
}
static WidthAndHeight imageSize(BufferedImage img) {
return img == null ? null : widthAndHeight(img.getWidth(), img.getHeight());
}
static WidthAndHeight imageSize(WidthAndHeight img) {
return img == null ? null : widthAndHeight(img.getWidth(), img.getHeight());
}
static String joinStrings(String sep, Object... strings) {
return joinStrings(sep, Arrays.asList(strings));
}
static String joinStrings(String sep, Iterable strings) {
StringBuilder buf = new StringBuilder();
for (Object o : unnull(strings)) {
String s = strOrNull(o);
if (nempty(s)) {
if (nempty(buf)) buf.append(sep);
buf.append(s);
}
}
return str(buf);
}
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 String repeat(char c, int n) {
n = Math.max(n, 0);
char[] chars = new char[n];
for (int i = 0; i < n; i++)
chars[i] = c;
return new String(chars);
}
static List repeat(A a, int n) {
n = Math.max(n, 0);
List l = new ArrayList(n);
for (int i = 0; i < n; i++)
l.add(a);
return l;
}
static List repeat(int n, A a) {
return repeat(a, n);
}
static int rectArea(Rect r) {
return r == null ? 0 : r.w*r.h;
}
static WidthAndHeight widthAndHeight(BufferedImage image) {
return image == null ? null : widthAndHeight(image.getWidth(), image.getHeight());
}
static WidthAndHeight widthAndHeight(int w) { return widthAndHeight(w, w); }
static WidthAndHeight widthAndHeight(int w, int h) {
return new WidthAndHeightFinal(w, h);
}
static String unnull(String s) {
return s == null ? "" : s;
}
static Collection unnull(Collection l) {
return l == null ? emptyList() : l;
}
static List unnull(List l) { return l == null ? emptyList() : l; }
static int[] unnull(int[] l) { return l == null ? emptyIntArray() : l; }
static char[] unnull(char[] l) { return l == null ? emptyCharArray() : l; }
static double[] unnull(double[] l) { return l == null ? emptyDoubleArray() : l; }
static Map unnull(Map l) {
return l == null ? emptyMap() : l;
}
static Iterable unnull(Iterable i) {
return i == null ? emptyList() : i;
}
static A[] unnull(A[] a) {
return a == null ? (A[]) emptyObjectArray() : a;
}
static BitSet unnull(BitSet b) {
return b == null ? new BitSet() : b;
}
static Pt unnull(Pt p) {
return p == null ? new Pt() : p;
}
//ifclass Symbol
static Symbol unnull(Symbol s) {
return s == null ? emptySymbol() : s;
}
//endif
static Pair unnull(Pair p) {
return p != null ? p : new Pair(null, null);
}
static int unnull(Integer i) { return i == null ? 0 : i; }
static long unnull(Long l) { return l == null ? 0L : l; }
static double unnull(Double l) { return l == null ? 0.0 : l; }
static String strOrNull(Object o) {
return o == null ? null : str(o);
}
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 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 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(MultiSet 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 int[] emptyIntArray_a = new int[0];
static int[] emptyIntArray() { return emptyIntArray_a; }
static char[] emptyCharArray = new char[0];
static char[] emptyCharArray() { return emptyCharArray; }
static double[] emptyDoubleArray = new double[0];
static double[] emptyDoubleArray() { return emptyDoubleArray; }
static Map emptyMap() {
return new HashMap();
}
static Object[] emptyObjectArray_a = new Object[0];
static Object[] emptyObjectArray() { return emptyObjectArray_a; }
static Symbol emptySymbol_value;
static Symbol emptySymbol() {
if (emptySymbol_value == null) emptySymbol_value = symbol("");
return emptySymbol_value;
}
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 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 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); } }
static WeakHasherMap symbol_map = new WeakHasherMap(new Hasher() {
public int hashCode(Symbol symbol) { return symbol.text.hashCode(); }
public boolean equals(Symbol a, Symbol b) {
if (a == null) return b == null;
return b != null && eq(a.text, b.text);
}
});
static Symbol symbol(String s) {
if (s == null) return null;
synchronized(symbol_map) {
// TODO: avoid object creation by passing the string to findKey
Symbol symbol = new Symbol(s, true);
Symbol existingSymbol = symbol_map.findKey(symbol);
if (existingSymbol == null)
symbol_map.put(existingSymbol = symbol, true);
return existingSymbol;
}
}
static Symbol symbol(CharSequence s) {
if (s == null) return null;
if (s instanceof Symbol) return (Symbol) s;
if (s instanceof String) return symbol((String) s);
return symbol(str(s));
}
static Symbol symbol(Object o) {
return symbol((CharSequence) o);
}
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); }
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 Iterator emptyIterator() {
return Collections.emptyIterator();
}
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 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 RuntimeException asRuntimeException(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static String getClassName(Object o) {
return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName();
}
static void _handleError(Error e) {
//call(javax(), '_handleError, e);
}
static class ConceptWithChangeListeners extends Concept implements IHasChangeListeners, ChangeTriggerable {
transient Set onChange;
public ConceptWithChangeListeners onChange(Runnable r) { onChange = createOrAddToSyncLinkedHashSet(onChange, r); return this; }
public ConceptWithChangeListeners removeChangeListener(Runnable r) { main.remove(onChange, r); return this; }
public void fireChange() { if (onChange != null) for (var listener : onChange) pcallF_typed(listener); }
void _onChange() { super._onChange();
fireChange();
}
public void change() { super.change(); }
}
static class Rect implements WidthAndHeight , IFieldsToList{
static final String _fieldOrder = "x y w h";
int x;
int y;
int w;
int h;
Rect() {}
Rect(int x, int y, int w, int h) {
this.h = h;
this.w = w;
this.y = y;
this.x = x;}
public boolean equals(Object o) {
if (!(o instanceof Rect)) return false;
Rect __1 = (Rect) o;
return x == __1.x && y == __1.y && w == __1.w && h == __1.h;
}
public int hashCode() {
int h = 2543108;
h = boostHashCombine(h, _hashCode(x));
h = boostHashCombine(h, _hashCode(y));
h = boostHashCombine(h, _hashCode(w));
h = boostHashCombine(h, _hashCode(h));
return h;
}
public Object[] _fieldsToList() { return new Object[] {x, y, w, h}; }
Rect(Rectangle r) {
x = r.x;
y = r.y;
w = r.width;
h = r.height;
}
Rect(Pt p, int w, int h) {
this.h = h;
this.w = w; x = p.x; y = p.y; }
Rect(Rect r) { x = r.x; y = r.y; w = r.w; h = r.h; }
final Rectangle getRectangle() {
return new Rectangle(x, y, w, h);
}
public String toString() {
return x + "," + y + " / " + w + "," + h;
}
final int x1() { return x; }
final int y1() { return y; }
final int x2() { return x + w; }
final int y2() { return y + h; }
final boolean contains(Pt p) {
return contains(p.x, p.y);
}
final boolean contains(int _x, int _y) {
return _x >= x && _y >= y && _x < x+w && _y < y+h;
}
final boolean contains(Rectangle r) {
return rectContains(this, r);
}
final boolean empty() { return w <= 0 || h <= 0; }
final public int getWidth() { return w; }
final public int getHeight() { return h; }
final public int area() { return w*h; }
final public int x() { return x; }
final public int y() { return y; }
WidthAndHeight widthAndHeight() { return main.widthAndHeight(w, h); }
}
static interface Hasher {
int hashCode(A a);
boolean equals(A a, A b);
}
// for the version with MasterSymbol (used WAY back in "Smart Bot"!) see #1010608
static class Symbol implements CharSequence {
String text;
Symbol() {}
Symbol(String text, boolean dummy) {
this.text = text;} // weird signature to prevent accidental calling
public int hashCode() { return _hashCode(text); }
public String toString() { return text; }
public boolean equals(Object o) {
return this == o;
}
// implementation of CharSequence methods
public int length() { return text.length(); }
public char charAt(int index) { return text.charAt(index); }
public CharSequence subSequence(int start, int end) {
return text.substring(start, end);
}
}
/*
* @(#)WeakHashMap.java 1.5 98/09/30
*
* Copyright 1998 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Sun Microsystems, Inc. ("Confidential Information"). You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with Sun.
*/
// From https://github.com/mernst/plume-lib/blob/df0bfafc3c16848d88f4ea0ef3c8bf3367ae085e/java/src/plume/WeakHasherMap.java
static final class WeakHasherMap extends AbstractMap implements Map {
private Hasher hasher = null;
/*@Pure*/
private boolean keyEquals(Object k1, Object k2) {
return (hasher==null ? k1.equals(k2)
: hasher.equals(k1, k2));
}
/*@Pure*/
private int keyHashCode(Object k1) {
return (hasher==null ? k1.hashCode()
: hasher.hashCode(k1));
}
// The WeakKey class can't be static because it depends on the hasher.
// That in turn means that its methods can't be static.
// However, I need to be able to call the methods such as create() that
// were static in the original version of this code.
// This finesses that.
private /*@Nullable*/ WeakKey WeakKeyCreate(K k) {
if (k == null) return null;
else return new WeakKey(k);
}
private /*@Nullable*/ WeakKey WeakKeyCreate(K k, ReferenceQueue super K> q) {
if (k == null) return null;
else return new WeakKey(k, q);
}
// Cannot be a static class: uses keyHashCode() and keyEquals()
private final class WeakKey extends WeakReference {
private int hash; /* Hashcode of key, stored here since the key
may be tossed by the GC */
private WeakKey(K k) {
super(k);
hash = keyHashCode(k);
}
private /*@Nullable*/ WeakKey create(K k) {
if (k == null) return null;
else return new WeakKey(k);
}
private WeakKey(K k, ReferenceQueue super K> q) {
super(k, q);
hash = keyHashCode(k);
}
private /*@Nullable*/ WeakKey create(K k, ReferenceQueue super K> q) {
if (k == null) return null;
else return new WeakKey(k, q);
}
/* A WeakKey is equal to another WeakKey iff they both refer to objects
that are, in turn, equal according to their own equals methods */
/*@Pure*/
@Override
public boolean equals(/*@Nullable*/ Object o) {
if (o == null) return false; // never happens
if (this == o) return true;
// This test is illegal because WeakKey is a generic type,
// so use the getClass hack below instead.
// if (!(o instanceof WeakKey)) return false;
if (!(o.getClass().equals(WeakKey.class))) return false;
Object t = this.get();
@SuppressWarnings("unchecked")
Object u = ((WeakKey)o).get();
if ((t == null) || (u == null)) return false;
if (t == u) return true;
return keyEquals(t, u);
}
/*@Pure*/
@Override
public int hashCode() {
return hash;
}
}
/* Hash table mapping WeakKeys to values */
private HashMap hash;
/* Reference queue for cleared WeakKeys */
private ReferenceQueue super K> queue = new ReferenceQueue();
/* Remove all invalidated entries from the map, that is, remove all entries
whose keys have been discarded. This method should be invoked once by
each public mutator in this class. We don't invoke this method in
public accessors because that can lead to surprising
ConcurrentModificationExceptions. */
@SuppressWarnings("unchecked")
private void processQueue() {
WeakKey wk;
while ((wk = (WeakKey)queue.poll()) != null) { // unchecked cast
hash.remove(wk);
}
}
/* -- Constructors -- */
/**
* Constructs a new, empty WeakHashMap
with the given
* initial capacity and the given load factor.
*
* @param initialCapacity the initial capacity of the
* WeakHashMap
*
* @param loadFactor the load factor of the WeakHashMap
*
* @throws IllegalArgumentException If the initial capacity is less than
* zero, or if the load factor is
* nonpositive
*/
public WeakHasherMap(int initialCapacity, float loadFactor) {
hash = new HashMap(initialCapacity, loadFactor);
}
/**
* Constructs a new, empty WeakHashMap
with the given
* initial capacity and the default load factor, which is
* 0.75
.
*
* @param initialCapacity the initial capacity of the
* WeakHashMap
*
* @throws IllegalArgumentException If the initial capacity is less than
* zero
*/
public WeakHasherMap(int initialCapacity) {
hash = new HashMap(initialCapacity);
}
/**
* Constructs a new, empty WeakHashMap
with the default
* capacity and the default load factor, which is 0.75
.
*/
public WeakHasherMap() {
hash = new HashMap();
}
/**
* Constructs a new, empty WeakHashMap
with the default
* capacity and the default load factor, which is 0.75
.
* The WeakHashMap
uses the specified hasher for hashing
* keys and comparing them for equality.
* @param h the Hasher to use when hashing values for this map
*/
public WeakHasherMap(Hasher h) {
hash = new HashMap();
hasher = h;
}
/* -- Simple queries -- */
/**
* Returns the number of key-value mappings in this map.
* Note: In contrast to most implementations of the
* Map
interface, the time required by this operation is
* linear in the size of the map.
*/
/*@Pure*/
@Override
public int size() {
return entrySet().size();
}
/**
* Returns true
if this map contains no key-value mappings.
*/
/*@Pure*/
@Override
public boolean isEmpty() {
return entrySet().isEmpty();
}
/**
* Returns true
if this map contains a mapping for the
* specified key.
*
* @param key the key whose presence in this map is to be tested
*/
/*@Pure*/
@Override
public boolean containsKey(Object key) {
@SuppressWarnings("unchecked")
K kkey = (K) key;
return hash.containsKey(WeakKeyCreate(kkey));
}
/* -- Lookup and modification operations -- */
/**
* Returns the value to which this map maps the specified key
.
* If this map does not contain a value for this key, then return
* null
.
*
* @param key the key whose associated value, if any, is to be returned
*/
/*@Pure*/
@Override
public /*@Nullable*/ V get(Object key) { // type of argument is Object, not K
@SuppressWarnings("unchecked")
K kkey = (K) key;
return hash.get(WeakKeyCreate(kkey));
}
/**
* Updates this map so that the given key
maps to the given
* value
. If the map previously contained a mapping for
* key
then that mapping is replaced and the previous value is
* returned.
*
* @param key the key that is to be mapped to the given
* value
* @param value the value to which the given key
is to be
* mapped
*
* @return the previous value to which this key was mapped, or
* null
if if there was no mapping for the key
*/
@Override
public V put(K key, V value) {
processQueue();
return hash.put(WeakKeyCreate(key, queue), value);
}
/**
* Removes the mapping for the given key
from this map, if
* present.
*
* @param key the key whose mapping is to be removed
*
* @return the value to which this key was mapped, or null
if
* there was no mapping for the key
*/
@Override
public V remove(Object key) { // type of argument is Object, not K
processQueue();
@SuppressWarnings("unchecked")
K kkey = (K) key;
return hash.remove(WeakKeyCreate(kkey));
}
/**
* Removes all mappings from this map.
*/
@Override
public void clear() {
processQueue();
hash.clear();
}
/* -- Views -- */
/* Internal class for entries */
// This can't be static, again because of dependence on hasher.
@SuppressWarnings("TypeParameterShadowing")
private final class Entry implements Map.Entry {
private Map.Entry ent;
private K key; /* Strong reference to key, so that the GC
will leave it alone as long as this Entry
exists */
Entry(Map.Entry ent, K key) {
this.ent = ent;
this.key = key;
}
/*@Pure*/
@Override
public K getKey() {
return key;
}
/*@Pure*/
@Override
public V getValue() {
return ent.getValue();
}
@Override
public V setValue(V value) {
return ent.setValue(value);
}
/*@Pure*/
private boolean keyvalEquals(K o1, K o2) {
return (o1 == null) ? (o2 == null) : keyEquals(o1, o2);
}
/*@Pure*/
private boolean valEquals(V o1, V o2) {
return (o1 == null) ? (o2 == null) : o1.equals(o2);
}
/*@Pure*/
@SuppressWarnings("NonOverridingEquals")
public boolean equals(Map.Entry e /* Object o*/) {
// if (! (o instanceof Map.Entry)) return false;
// Map.Entry e = (Map.Entry)o;
return (keyvalEquals(key, e.getKey())
&& valEquals(getValue(), e.getValue()));
}
/*@Pure*/
@Override
public int hashCode() {
V v;
return (((key == null) ? 0 : keyHashCode(key))
^ (((v = getValue()) == null) ? 0 : v.hashCode()));
}
}
/* Internal class for entry sets */
private final class EntrySet extends AbstractSet> {
Set> hashEntrySet = hash.entrySet();
@Override
public Iterator> iterator() {
return new Iterator>() {
Iterator> hashIterator = hashEntrySet.iterator();
Map.Entry next = null;
@Override
public boolean hasNext() {
while (hashIterator.hasNext()) {
Map.Entry ent = hashIterator.next();
WeakKey wk = ent.getKey();
K k = null;
if ((wk != null) && ((k = wk.get()) == null)) {
/* Weak key has been cleared by GC */
continue;
}
next = new Entry(ent, k);
return true;
}
return false;
}
@Override
public Map.Entry next() {
if ((next == null) && !hasNext())
throw new NoSuchElementException();
Map.Entry e = next;
next = null;
return e;
}
@Override
public void remove() {
hashIterator.remove();
}
};
}
/*@Pure*/
@Override
public boolean isEmpty() {
return !(iterator().hasNext());
}
/*@Pure*/
@Override
public int size() {
int j = 0;
for (Iterator> i = iterator(); i.hasNext(); i.next()) j++;
return j;
}
@Override
public boolean remove(Object o) {
processQueue();
if (!(o instanceof Map.Entry,?>)) return false;
@SuppressWarnings("unchecked")
Map.Entry e = (Map.Entry)o; // unchecked cast
Object ev = e.getValue();
WeakKey wk = WeakKeyCreate(e.getKey());
Object hv = hash.get(wk);
if ((hv == null)
? ((ev == null) && hash.containsKey(wk)) : hv.equals(ev)) {
hash.remove(wk);
return true;
}
return false;
}
/*@Pure*/
@Override
public int hashCode() {
int h = 0;
for (Iterator> i = hashEntrySet.iterator(); i.hasNext(); ) {
Map.Entry ent = i.next();
WeakKey wk = ent.getKey();
Object v;
if (wk == null) continue;
h += (wk.hashCode()
^ (((v = ent.getValue()) == null) ? 0 : v.hashCode()));
}
return h;
}
}
private /*@Nullable*/ Set> entrySet = null;
/**
* Returns a Set
view of the mappings in this map.
*/
/*@SideEffectFree*/
@Override
public Set> entrySet() {
if (entrySet == null) entrySet = new EntrySet();
return entrySet;
}
// find matching key
K findKey(Object key) {
processQueue();
K kkey = (K) key;
// TODO: use replacement for HashMap to avoid reflection
WeakKey wkey = WeakKeyCreate(kkey);
WeakKey found = hashMap_findKey(hash, wkey);
return found == null ? null : found.get();
}
}
// you still need to implement hasNext() and next()
static abstract class IterableIterator implements Iterator, Iterable {
public Iterator iterator() {
return this;
}
public void remove() {
unsupportedOperation();
}
}
static class DoubleRect {
double x, y, w, h;
DoubleRect() {}
DoubleRect(Rectangle r) {
x = r.x;
y = r.y;
w = r.width;
h = r.height;
}
DoubleRect(double x, double y, double w, double h) {
this.h = h;
this.w = w;
this.y = y;
this.x = x;}
// Huh. not implementing equals()/hashCode? Stefan is mysterious
boolean eq(Object o) {
if (!(o instanceof DoubleRect)) return false;
if (o == this) return true;
DoubleRect r = (DoubleRect) o;
return x == r.x && y == r.y && w == r.w && h == r.h;
}
public String toString() {
return x + "," + y + " / " + w + "," + h;
}
double x1() { return x; }
double y1() { return y; }
double x2() { return x + w; }
double y2() { return y + h; }
boolean contains(Pt p) {
return contains(p.x, p.y);
}
boolean contains(double _x, double _y) {
return _x >= x && _y >= y && _x < x+w && _y < y+h;
}
boolean empty() { return w <= 0 || h <= 0; }
}
static interface WidthAndHeight {
default int w(){ return getWidth(); }
default int width(){ return getWidth(); }
int getWidth();
default int h(){ return getHeight(); }
default int height(){ return getHeight(); }
int getHeight();
public default Rect bounds() { return rect(0, 0, getWidth(), getHeight()); }
default int area() { return toInt(areaAsLong()); }
default long areaAsLong() { return longMul(w(), h()); }
}
static final class WidthAndHeightFinal extends Meta implements WidthAndHeight
, IFieldsToList{
static final String _fieldOrder = "width height";
int width;
int height;
WidthAndHeightFinal() {}
WidthAndHeightFinal(int width, int height) {
this.height = height;
this.width = width;}
public boolean equals(Object o) {
if (!(o instanceof WidthAndHeightFinal)) return false;
WidthAndHeightFinal __0 = (WidthAndHeightFinal) o;
return width == __0.width && height == __0.height;
}
public int hashCode() {
int h = -1177452162;
h = boostHashCombine(h, _hashCode(width));
h = boostHashCombine(h, _hashCode(height));
return h;
}
public Object[] _fieldsToList() { return new Object[] {width, height}; }
public int getWidth() { return width; }
public int getHeight() { return height; }
WidthAndHeightFinal(int wAndH) { this(wAndH, wAndH); }
public String toString() { return n2(width) + "*" + n2(height) + " px"; }
}
static class Fail extends RuntimeException implements IFieldsToList{
Object[] objects;
Fail() {}
Fail(Object... objects) {
this.objects = objects;}public Object[] _fieldsToList() { return new Object[] {objects}; }
Fail(Throwable cause, Object... objects) {
super(cause);
this.objects = objects;
}
public String toString() { return joinNemptiesWithColon("Fail", getMessage()); }
public String getMessage() { return commaCombine(getCause(), objects); }
}
// 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:
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 __3 = (ConceptCreate) o;
return eq(c, __3.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 __4 = (ConceptChange) o;
return eq(c, __4.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 __5 = (ConceptDelete) o;
return id == __5.id && eq(c, __5.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;
// 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;
} 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) {
print("Concept save failed, will try again");
printStackTrace(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) { printStackTrace(__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 __1 = lock(); lock(__1); try {
if (changes == changesWritten && !isTransient())
return loadConceptsStructure(programID);
return fullStructure();
} finally { unlock(__1); } }
/* 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> __0 : _entrySet( map))
{ String oldField = __0.getKey(); FieldMigration m = __0.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 __6 = (FieldMigration) o;
return eq(newField, __6.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;
}
// Meta - a "minimal" approach to adding meta-level to Java objects
static class Meta implements IMeta {
// Meta - a "minimal" approach to adding meta-level to Java objects
// (implementing the interface IMeta)
// We allocate one extra field for each Java object to make it
// reasoning-compatible (reasoning-compatible = extensible with
// fields of any name at runtime).
//
// We couldn't go for 0 extra fields (meta values must be linked
// directly from the object) and there are no half fields in
// Java... so there you go.
//
// Also, if you don't use any meta data, you are probably not
// reasoning about anything. The point of reasoning in JavaX is
// to attach information to objects directly used in the program.
// Possible information contained in the meta field:
// Origin, destination, security level, sender, cost center,
// purpose, list of reifications, ...
// So here it is. THE FIELD YOU HAVE BEEN WAITING FOR!
// [We also have IMeta to retrofit foreign classes (rare but
// probably useful).]
//////////////////////
// The "meta" field //
//////////////////////
// Generic meta value of any kind, but the typical case is it's a
// Map with extra field values for the object etc.
// "meta" is volatile to avoid synchronization; but you can also synchronize on
// _tempMetaMutex() which is usually the object itself. Collections
// and maps are exempt from using the collections's monitor as the meta
// mutex because their monitor tends to be held for long operations
// (e.g. cloneList). For those we use a substantially more complex
// algorithm using a weakMap. Probably overkill. I may reconsider.
volatile Object meta;
// The meta field is not transient, thus by default it will be
// persisted like anything else unless you customize your object
// to suppress or modulate this.
// ...and the interface methods
public void _setMeta(Object meta) { this.meta = meta; }
public Object _getMeta() { return meta; }
// MOST functions are implemented in IMeta (default implementations)
// Scaffolding convenience functions
final boolean scaffolding(){ return scaffoldingEnabled(); }
final boolean scaffolded(){ return scaffoldingEnabled(); }
boolean scaffoldingEnabled() { return main.scaffoldingEnabled(this); }
boolean scaffoldingEnabled(Object o) { return main.scaffoldingEnabled(o); }
// Implementing setMetaToString
String toString_base() { return super.toString(); }
public String toString() {
Object o = metaGet("toString", this);
if (o instanceof String) return ((String) o);
if (o instanceof IF1) return str(((IF1) o).get(this));
return toString_base();
}
}
interface IHasChangeListeners {
IHasChangeListeners onChange(Runnable r);
IHasChangeListeners removeChangeListener(Runnable r);
default IHasChangeListeners onChangeAndNow(Runnable l) {
onChange(l);
callF(l);
return this;
}
}
static interface IFieldsToList {
Object[] _fieldsToList();
}
static class Pt implements Comparable, IDoublePt {
int x, y;
Pt() {}
Pt(Point p) {
x = p.x;
y = p.y;
}
Pt(int x, int y) {
this.y = y;
this.x = x;}
Point getPoint() {
return new Point(x, y);
}
public boolean equals(Object o) {
return o instanceof Pt && x == ((Pt) o).x && y == ((Pt) o).y;
}
public int hashCode() {
return boostHashCombine(x, y);
}
// compare in scan order
public int compareTo(Pt p) {
if (y != p.y) return cmp(y, p.y);
return cmp(x, p.x);
}
public String toString() {
return x + ", " + y;
}
double length() { return sqrt(x*x+y*y); }
public Pt minus(Pt p) { return ptMinus(this, p); }
public double x_double() { return x; }
public double y_double() { return y; }
}
interface ChangeTriggerable {
public void change();
}
public static interface IF0 {
A get();
}
// the lockFile must be a file separate from any data files.
// It is created & deleted by this class, and will always have
// size 0.
static class FileBasedLock implements AutoCloseable {
File lockFile;
double timeout = 60.0; // in seconds. refresh happens twice as often
boolean verbose = false;
boolean haveLock = false;
java.util.Timer touchTimer;
// what to put in the lock file when we create it
final public FileBasedLock setContentsForLockFile(String contentsForLockFile){ return contentsForLockFile(contentsForLockFile); }
public FileBasedLock contentsForLockFile(String contentsForLockFile) { this.contentsForLockFile = contentsForLockFile; return this; } final public String getContentsForLockFile(){ return contentsForLockFile(); }
public String contentsForLockFile() { return contentsForLockFile; }
String contentsForLockFile;
FileBasedLock() {}
FileBasedLock(File lockFile) {
this.lockFile = lockFile;}
FileBasedLock(File lockFile, double timeout) {
this.timeout = timeout;
this.lockFile = lockFile;}
// returns true iff lock was acquired (or kept)
synchronized boolean tryToLock() {
if (haveLock) return true;
if (fileExists(lockFile)) {
double age = fileAgeInSeconds(lockFile);
double remaining = timeout-age;
print("Lock file age: " + lockFile + ": " + iround(age) + " s"
+ (remaining <= 0
? " - old, deleting"
: " - please start again in " + nSeconds(iceil(remaining))));
if (remaining <= 0) {
print("Deleting old lock file (program crashed?): " + lockFile + " (age: " + iround(age) + " seconds)");
deleteFile(lockFile);
}
}
try {
mkdirsForFile(lockFile);
// This does the actual atomic file creation
java.nio.file.Files.createFile(toPath(lockFile));
// Optionally set the lock file's contents
if (nempty(contentsForLockFile))
writeContents();
acquired();
return true;
} catch (Throwable e) {
printExceptionShort("Can't lock", e);
return false;
}
}
void writeContents() {
saveTextFileWithoutTemp(lockFile, unnull(contentsForLockFile));
}
private void acquired() {
haveLock = true;
startTouchTimer();
}
void forceLock() { try {
print("Force-locking " + lockFile);
writeContents();
acquired();
} catch (Exception __e) { throw rethrow(__e); } }
String lockError() {
return "Couldn't aquire lock file: " + lockFile;
}
void lockOrFail() {
if (!tryToLock())
throw fail(lockError());
}
synchronized void startTouchTimer() {
if (touchTimer != null) return;
double interval = timeout/2;
touchTimer = doEvery(interval, new Runnable() { public void run() { try { doTouch();
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "doTouch();"; }});
if (verbose) print("Touch timer started for " + lockFile + " (" + interval + "s)");
}
synchronized void doTouch() { try {
if (haveLock) {
if (verbose) print("Touching lock file: " + lockFile);
touchExistingFile(lockFile);
}
} catch (Throwable __e) { printStackTrace(__e); }}
public synchronized void close() { try {
{ cleanUp(touchTimer); touchTimer = null; }
if (haveLock) {
haveLock = false;
if (verbose) print("Deleting lock file: " + lockFile);
deleteFile(lockFile);
}
} catch (Throwable __e) { printStackTrace(__e); }}
synchronized void _simulateCrash() {
{ cleanUp(touchTimer); touchTimer = null; }
}
void deleteOnExit() {
if (haveLock)
lockFile.deleteOnExit();
}
String actualContents() {
return loadTextFile(lockFile);
}
boolean hasExpectedContents() {
return eq(unnull(contentsForLockFile), actualContents());
}
}
static interface IMeta {
// see class "Meta" for the bla bla
public void _setMeta(Object meta);
public Object _getMeta();
default public IAutoCloseableF0 _tempMetaMutex() {
return new IAutoCloseableF0() {
public Object get() { return IMeta.this; }
public void close() {}
};
}
// actually query another object
default public Object getMeta(Object obj, Object key){ return metaGet(obj, key); }
default public Object metaGet(Object obj, Object key) {
// call global function
return metaMapGet(obj, key);
}
default public Object metaGet(String key, Object obj) {
// call global function
return metaMapGet(obj, key);
}
default public Object getMeta(Object key){ return metaGet(key); }
default public Object metaGet(Object key) {
if (key == null) return null;
Object meta = _getMeta();
if (meta instanceof Map) return ((Map) meta).get(key);
return null;
}
default public void metaSet(IMeta obj, Object key, Object value){ metaPut(obj, key, value); }
default public void metaPut(IMeta obj, Object key, Object value) {
// call global function
metaMapPut(obj, key, value);
}
default public void metaSet(Object key, Object value){ metaPut(key, value); }
default public void metaPut(Object key, Object value) {
if (key == null) return;
Map map = convertObjectMetaToMap(this);
syncMapPutOrRemove(map, key, value);
}
}
static interface IConceptCounter {
Class extends Concept> conceptClass();
int countConcepts();
Collection allConcepts();
}
// could almost add IVar here - but void set() and bool set() are incompatible in some places
static interface IRef extends IF0 {
// called by the referencee to atomically replace itself
// Passing oldValue to avoid race conditions
public default void replaceValue(A oldValue, A newValue) {}
}
static interface IVF1 {
void get(A a);
}
// uses HashMap by default
static class MultiSet implements IMultiSet {
Map map = new HashMap();
int size; // now maintaining a size counter
MultiSet(boolean useTreeMap) {
if (useTreeMap) map = new TreeMap();
}
MultiSet(TreeMap map) {
this.map = map;}
MultiSet() {}
MultiSet(Iterable c) { addAll(c); }
MultiSet(MultiSet ms) { synchronized(ms) {
for (A a : ms.keySet()) add(a, ms.get(a));
}}
// returns new count
public synchronized int add(A key) { return add(key, 1); }
synchronized void addAll(Iterable c) {
if (c != null) for (A a : c) add(a);
}
synchronized void addAll(MultiSet ms) {
for (A a : ms.keySet()) add(a, ms.get(a));
}
synchronized int add(A key, int count) {
if (count <= 0) return 0; // don't calculate return value in this case
size += count;
Integer i = map.get(key);
map.put(key, i != null ? (count += i) : count);
return count;
}
synchronized void put(A key, int count) {
int oldCount = get(key);
if (count == oldCount) return;
size += count-oldCount;
if (count != 0)
map.put(key, count);
else
map.remove(key);
}
public synchronized int get(A key) {
Integer i = map.get(key);
return i != null ? i : 0;
}
synchronized boolean contains(A key) {
return map.containsKey(key);
}
synchronized void remove(A key) {
Integer i = map.get(key);
if (i != null) {
--size;
if (i > 1)
map.put(key, i - 1);
else
map.remove(key);
}
}
synchronized List topTen() { return getTopTen(); }
synchronized List getTopTen() { return getTopTen(10); }
synchronized List getTopTen(int maxSize) {
List list = getSortedListDescending();
return list.size() > maxSize ? list.subList(0, maxSize) : list;
}
synchronized List highestFirst() {
return getSortedListDescending();
}
synchronized List lowestFirst() {
return reversedList(getSortedListDescending());
}
synchronized List getSortedListDescending() {
List list = new ArrayList(map.keySet());
Collections.sort(list, new Comparator() {
public int compare(A a, A b) {
return map.get(b).compareTo(map.get(a));
}
});
return list;
}
synchronized int getNumberOfUniqueElements() {
return map.size();
}
synchronized int uniqueSize() {
return map.size();
}
synchronized Set asSet() {
return map.keySet();
}
synchronized NavigableSet navigableSet() {
return navigableKeys((NavigableMap) map);
}
synchronized Set keySet() {
return map.keySet();
}
final A mostPopular(){ return getMostPopularEntry(); }
synchronized A getMostPopularEntry() {
int max = 0;
A a = null;
for (Map.Entry entry : map.entrySet()) {
if (entry.getValue() > max) {
max = entry.getValue();
a = entry.getKey();
}
}
return a;
}
synchronized void removeAll(A key) {
size -= get(key);
map.remove(key);
}
synchronized int size() {
return size;
}
synchronized MultiSet mergeWith(MultiSet set) {
MultiSet result = new MultiSet();
for (A a : set.asSet()) {
result.add(a, set.get(a));
}
return result;
}
synchronized boolean isEmpty() {
return map.isEmpty();
}
synchronized public String toString() { // hmm. sync this?
return str(map);
}
synchronized void clear() {
map.clear();
size = 0;
}
final Map toMap(){ return asMap(); }
synchronized Map asMap() {
return cloneMap(map);
}
}
interface IDoublePt {
public double x_double();
public double y_double();
}
static class RemoteDB implements AutoCloseable {
DialogIO db;
String name;
// s = bot name or snippet ID
RemoteDB(String s) {
this(s, false);
}
RemoteDB(String s, boolean autoStart) {
name = s;
if (isSnippetID(s)) name = dbBotName(s);
db = findBot(name);
if (db == null)
if (autoStart) {
nohupJavax(fsI(s));
waitForBotStartUp(name);
assertNotNull("Weird problem", db = findBot(s));
} else
throw fail("DB " + s + " not running");
}
boolean functional() { return db != null; } // now always true
List list() { return adopt((List) rpc(db, "xlist")); }
List list(String className) { return adopt((List) rpc(db, "xlist", className)); }
List xlist() { return list(); }
List xlist(String className) { return list(className); }
// adopt is an internal method
List adopt(List l) {
if (l != null) for (RC rc : l) adopt(rc);
return l;
}
RC adopt(RC rc) { if (rc != null) rc.db = this; return rc; }
Object adopt(Object o) {
if (o instanceof RC) return adopt((RC) o);
return o;
}
String xclass(RC o) {
return (String) rpc(db, "xclass", o);
}
Object xget(RC o, String field) {
return adopt(rpc(db, "xget", o, field));
}
String xS(RC o, String field) {
return (String) xget(o, field);
}
RC xgetref(RC o, String field) {
return adopt((RC) xget(o, field));
}
void xset(RC o, String field, Object value) {
rpc(db, "xset", o, field, value);
}
RC uniq(String className) {
RC ref = first(list(className));
if (ref == null)
ref = xnew(className);
return ref;
}
RC xuniq(String className) { return uniq(className); }
RC xnew(String className, Object... values) {
return adopt((RC) rpc(db, "xnew", className, values));
}
void xdelete(RC o) {
rpc(db, "xdelete", o);
}
void xdelete(List l) {
rpc(db, "xdelete", l);
}
public void close() {
_close(db);
}
String fullgrab() { return (String) rpc(db, "xfullgrab"); }
String xfullgrab() { return fullgrab(); }
void xshutdown() { rpc(db, "xshutdown"); }
long xchangeCount() { return (long) rpc(db, "xchangeCount"); }
int xcount() { return (int) rpc(db, "xcount"); }
void reconnect() {
close();
db = findBot(name);
}
RC rc(long id) { return new RC(this, id); }
}
static class Pair implements Comparable> {
final public Pair setA(A a){ return a(a); }
public Pair a(A a) { this.a = a; return this; } final public A getA(){ return a(); }
public A a() { return a; }
A a;
final public Pair setB(B b){ return b(b); }
public Pair b(B b) { this.b = b; return this; } final public B getB(){ return b(); }
public B b() { return b; }
B b;
Pair() {}
Pair(A a, B b) {
this.b = b;
this.a = a;}
public int hashCode() {
return hashCodeFor(a) + 2*hashCodeFor(b);
}
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Pair)) return false;
Pair t = (Pair) o;
return eq(a, t.a) && eq(b, t.b);
}
public String toString() {
return "<" + a + ", " + b + ">";
}
public int compareTo(Pair p) {
if (p == null) return 1;
int i = ((Comparable) a).compareTo(p.a);
if (i != 0) return i;
return ((Comparable) b).compareTo(p.b);
}
}
static interface IMultiSet {
// returns new count
int add(A key);
/*void addAll(Iterable c) {
if (c != null) for (A a : c) add(a);
}
void addAll(MultiSet ms) {
for (A a : ms.keySet()) add(a, ms.get(a));
}*/
//int add(A key, int count);
//void put(A key, int count);
int get(A key);
//bool contains(A key);
//void remove(A key);
/*List topTen();
synchronized List getTopTen() { ret getTopTen(10); }
synchronized List getTopTen(int maxSize) {
List list = getSortedListDescending();
return list.size() > maxSize ? list.subList(0, maxSize) : list;
}
synchronized L highestFirst() {
ret getSortedListDescending();
}
synchronized L lowestFirst() {
ret reversedList(getSortedListDescending());
}
synchronized L getSortedListDescending() {
List list = new ArrayList(map.keySet());
Collections.sort(list, new Comparator() {
public int compare(A a, A b) {
return map.get(b).compareTo(map.get(a));
}
});
ret list;
}
synchronized int getNumberOfUniqueElements() {
return map.size();
}
synchronized int uniqueSize() {
ret map.size();
}
synchronized Set asSet() {
return map.keySet();
}
synchronized NavigableSet navigableSet() {
return navigableKeys((NavigableMap) map);
}
synchronized Set keySet() {
return map.keySet();
}
synchronized A getMostPopularEntry() {
int max = 0;
A a = null;
for (Map.Entry entry : map.entrySet()) {
if (entry.getValue() > max) {
max = entry.getValue();
a = entry.getKey();
}
}
return a;
}
synchronized void removeAll(A key) {
size -= get(key);
map.remove(key);
}
synchronized int size() {
ret size;
}
synchronized MultiSet mergeWith(MultiSet set) {
MultiSet result = new MultiSet();
for (A a : set.asSet()) {
result.add(a, set.get(a));
}
return result;
}
synchronized boolean isEmpty() {
return map.isEmpty();
}
synchronized public String toString() { // hmm. sync this?
return str(map);
}
synchronized void clear() {
map.clear();
size = 0;
}
synchronized Map asMap() {
ret cloneMap(map);
}*/
}
static abstract class DialogIO implements AutoCloseable {
String line;
boolean eos, loud, noClose;
Lock lock = lock();
abstract String readLineImpl();
abstract boolean isStillConnected();
abstract void sendLine(String line);
abstract boolean isLocalConnection();
abstract Socket getSocket();
int getPort() { Socket s = getSocket(); return s == null ? 0 : s.getPort(); }
boolean helloRead = false;
int shortenOutputTo = 500;
String readLineNoBlock() {
String l = line;
line = null;
return l;
}
boolean waitForLine() { try {
ping();
if (line != null) return true;
//print("Readline");
line = readLineImpl();
//print("Readline done: " + line);
if (line == null) eos = true;
return line != null;
} catch (Exception __e) { throw rethrow(__e); } }
String readLine() {
waitForLine();
helloRead = true;
return readLineNoBlock();
}
String ask(String s, Object... args) {
if (loud) return askLoudly(s, args);
if (!helloRead) readLine();
if (args.length != 0) s = format3(s, args);
sendLine(s);
return readLine();
}
String askLoudly(String s, Object... args) {
if (!helloRead) readLine();
if (args.length != 0) s = format3(s, args);
print("> " + shorten(s, shortenOutputTo));
sendLine(s);
String answer = readLine();
print("< " + shorten(answer, shortenOutputTo));
return answer;
}
void pushback(String l) {
if (line != null)
throw fail();
line = l;
helloRead = false;
}
}
static abstract class DialogHandler implements IDialogHandler {}
interface IDialogHandler {
void run(DialogIO io);
}
static interface IAutoCloseableF0 extends IF0, AutoCloseable {}
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 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) { printStackTrace(__e); } return null;
}
static B pcallF_typed(F1 f, A a) {
try { return f == null ? null : f.get(a); } catch (Throwable __e) { printStackTrace(__e); } return null;
}
static void pcallF_typed(VF1 f, A a) {
try {
{ if (f != null) f.get(a); }
} catch (Throwable __e) { printStackTrace(__e); }
}
static void pcallF_typed(IVF1 f, A a) {
try {
{ if (f != null) f.get(a); }
} catch (Throwable __e) { printStackTrace(__e); }
}
static void pcallF_typed(IVF2 f, A a, B b) {
try {
{ if (f != null) f.get(a, b); }
} catch (Throwable __e) { printStackTrace(__e); }
}
static Object pcallF_typed(Runnable r) {
try { { if (r != null) r.run(); } } catch (Throwable __e) { printStackTrace(__e); } return null;
}
static A pcallF_typed(IF0 f) {
try { return f == null ? null : f.get(); } catch (Throwable __e) { printStackTrace(__e); } return null;
}
static B pcallF_typed(IF1 f, A a) {
try { return f == null ? null : f.get(a); } catch (Throwable __e) { printStackTrace(__e); } return null;
}
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 boolean contains(Collection c, Object o) {
return c != null && c.contains(o);
}
static boolean contains(Iterable it, Object a) {
if (it != null)
for (Object o : it)
if (eq(a, o))
return true;
return false;
}
static boolean contains(Object[] x, Object o) {
if (x != null)
for (Object a : x)
if (eq(a, o))
return true;
return false;
}
static boolean contains(String s, char c) {
return s != null && s.indexOf(c) >= 0;
}
static boolean contains(String s, String b) {
return s != null && s.indexOf(b) >= 0;
}
static boolean contains(BitSet bs, int i) {
return bs != null && bs.get(i);
}
static boolean contains(Producer p, A a) {
if (p != null && a != null) while (true) {
A x = p.next();
if (x == null) break;
if (eq(x, a)) return true;
}
return false;
}
static boolean contains(Rect r, Pt p) { return rectContains(r, p); }
static boolean rectContains(int x1, int y1, int w, int h, Pt p) {
return p.x >= x1 && p.y >= y1 && p.x < x1+w && p.y < y1+h;
}
static boolean rectContains(Rect a, Rect b) {
return b.x >= a.x && b.y >= a.y && b.x2() <= a.x2() && b.y2() <= a.y2();
}
static boolean rectContains(Rect a, Rectangle b) {
return rectContains(a, toRect(b));
}
static boolean rectContains(Rect a, int x, int y) {
return a != null && a.contains(x, y);
}
static boolean rectContains(Rect a, Pt p) {
return a != null && p != null && a.contains(p);
}
static Set> entrySet(Map map) {
return _entrySet(map);
}
// 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 put(Map map, A a, B b) {
if (map != null) map.put(a, b);
}
static void put(List l, int i, A a) {
if (l != null && i >= 0 && i < l(l)) l.set(i, a);
}
static Method hashMap_findKey_method;
static A hashMap_findKey(HashMap map, Object key) { try {
if (hashMap_findKey_method == null)
hashMap_findKey_method = findMethodNamed(HashMap.class, "getNode");
Map.Entry entry = (Map.Entry) hashMap_findKey_method.invoke(map, hashMap_internalHash(key), key);
// java.util.Map.Entry entry = (java.util.Map.Entry) call(hash, 'getNode, hashMap_internalHash(key), wkey);
return entry == null ? null : entry.getKey();
} catch (Exception __e) { throw rethrow(__e); } }
static UnsupportedOperationException unsupportedOperation() {
throw new UnsupportedOperationException();
}
static int getWidth(Component c) {
return c == null ? 0 : (int) swingCall(c, "getWidth");
}
static int getHeight(Component c) {
return c == null ? 0 : (int) swingCall(c, "getHeight");
}
static Rect rect(int x, int y, int w, int h) {
return new Rect(x, y, w, h);
}
static Rect rect(Pt p, int w, int h) {
return new Rect(p.x, p.y, w, h);
}
static Rect rect(int w, int h) {
return new Rect(0, 0, w, h);
}
static int toInt(Object o) {
if (o == null) return 0;
if (o instanceof Number)
return ((Number) o).intValue();
if (o instanceof String)
return parseInt((String) o);
if (o instanceof Boolean)
return boolToInt((Boolean) o);
throw fail("woot not int: " + getClassName(o));
}
static int toInt(long l) {
if (l != (int) l) throw fail("Too large for int: " + l);
return (int) l;
}
static long longMul(long a, long b) {
return a*b;
}
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(double l, String singular) {
return empty(singular) ? str(l) : n2(l, singular, singular + "s");
}
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, singular + "s");
}
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, singular + "s"); }
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, singular + "s"); }
static String n2(IMultiMap mm, String singular, String plural) {
return n_fancy2(l(mm), singular, plural);
}
static String joinNemptiesWithColon(String... strings) {
return joinNempties(": ", strings);
}
static String joinNemptiesWithColon(Collection strings) {
return joinNempties(": ", strings);
}
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 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 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]