import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
class main {
static class DateStructures {
abstract static class SomeDate {}
abstract static class SomeDateDate extends SomeDate {} // day or higher granularity
abstract static class SomeTime extends SomeDate {}
abstract static class SomeWeek extends SomeDateDate {}
abstract static class DateProp extends SomeDate {} // proposition on a date, e.g. a date range
// years
static class Year extends SomeDateDate implements IFieldsToList, Transformable, Visitable{
int year;
Year() {}
Year(int year) {
this.year = year;}
public String toString() { return shortClassName(this) + "(" + year + ")"; }
public boolean equals(Object o) {
if (!(o instanceof Year)) return false;
Year x = (Year) o;
return eq(year, x.year);
}
public int hashCode() {
int h = 2751581;
h = boostHashCombine(h, _hashCode(year));
return h;
}
public Object[] _fieldsToList() { return new Object[] {year}; }
public Object transformUsing(IF1 f) { return new Year((int) f.get(year)); }
public void visitUsing(IVF1 f) { f.get(year); }
}
static class CurrentYearPlus extends SomeDateDate implements IFieldsToList, Transformable, Visitable{
int nYears;
CurrentYearPlus() {}
CurrentYearPlus(int nYears) {
this.nYears = nYears;}
public String toString() { return shortClassName(this) + "(" + nYears + ")"; }
public boolean equals(Object o) {
if (!(o instanceof CurrentYearPlus)) return false;
CurrentYearPlus x = (CurrentYearPlus) o;
return eq(nYears, x.nYears);
}
public int hashCode() {
int h = -283479056;
h = boostHashCombine(h, _hashCode(nYears));
return h;
}
public Object[] _fieldsToList() { return new Object[] {nYears}; }
public Object transformUsing(IF1 f) { return new CurrentYearPlus((int) f.get(nYears)); }
public void visitUsing(IVF1 f) { f.get(nYears); }
}
// months
static class Month extends SomeDateDate implements IFieldsToList, Transformable, Visitable{
int month;
Year year;
Month() {}
Month(int month, Year year) {
this.year = year;
this.month = month;}
public String toString() { return shortClassName(this) + "(" + month + ", " + year + ")"; }
public boolean equals(Object o) {
if (!(o instanceof Month)) return false;
Month x = (Month) o;
return eq(month, x.month) && eq(year, x.year);
}
public int hashCode() {
int h = 74527328;
h = boostHashCombine(h, _hashCode(month));
h = boostHashCombine(h, _hashCode(year));
return h;
}
public Object[] _fieldsToList() { return new Object[] {month, year}; }
public Object transformUsing(IF1 f) { return new Month((int) f.get(month), (Year) f.get(year)); }
public void visitUsing(IVF1 f) { f.get(month); f.get(year); }
Month(int month) { this(month, null); }
}
static class CurrentMonthPlus extends SomeDateDate implements IFieldsToList, Transformable, Visitable{
int nMonths;
CurrentMonthPlus() {}
CurrentMonthPlus(int nMonths) {
this.nMonths = nMonths;}
public String toString() { return shortClassName(this) + "(" + nMonths + ")"; }
public boolean equals(Object o) {
if (!(o instanceof CurrentMonthPlus)) return false;
CurrentMonthPlus x = (CurrentMonthPlus) o;
return eq(nMonths, x.nMonths);
}
public int hashCode() {
int h = -1003838751;
h = boostHashCombine(h, _hashCode(nMonths));
return h;
}
public Object[] _fieldsToList() { return new Object[] {nMonths}; }
public Object transformUsing(IF1 f) { return new CurrentMonthPlus((int) f.get(nMonths)); }
public void visitUsing(IVF1 f) { f.get(nMonths); }
}
// weeks
static class Week extends SomeWeek implements IFieldsToList, Transformable, Visitable{
int week;
Year year;
Week() {}
Week(int week, Year year) {
this.year = year;
this.week = week;}
public String toString() { return shortClassName(this) + "(" + week + ", " + year + ")"; }
public boolean equals(Object o) {
if (!(o instanceof Week)) return false;
Week x = (Week) o;
return eq(week, x.week) && eq(year, x.year);
}
public int hashCode() {
int h = 2692116;
h = boostHashCombine(h, _hashCode(week));
h = boostHashCombine(h, _hashCode(year));
return h;
}
public Object[] _fieldsToList() { return new Object[] {week, year}; }
public Object transformUsing(IF1 f) { return new Week((int) f.get(week), (Year) f.get(year)); }
public void visitUsing(IVF1 f) { f.get(week); f.get(year); }
}
static class CurrentWeekPlus extends SomeWeek implements IFieldsToList, Transformable, Visitable{
int nWeeks;
CurrentWeekPlus() {}
CurrentWeekPlus(int nWeeks) {
this.nWeeks = nWeeks;}
public String toString() { return shortClassName(this) + "(" + nWeeks + ")"; }
public boolean equals(Object o) {
if (!(o instanceof CurrentWeekPlus)) return false;
CurrentWeekPlus x = (CurrentWeekPlus) o;
return eq(nWeeks, x.nWeeks);
}
public int hashCode() {
int h = 633919527;
h = boostHashCombine(h, _hashCode(nWeeks));
return h;
}
public Object[] _fieldsToList() { return new Object[] {nWeeks}; }
public Object transformUsing(IF1 f) { return new CurrentWeekPlus((int) f.get(nWeeks)); }
public void visitUsing(IVF1 f) { f.get(nWeeks); }
}
// days
static class Day extends SomeDate implements IFieldsToList, Transformable, Visitable{
int day;
Month month;
Day() {}
Day(int day, Month month) {
this.month = month;
this.day = day;}
public String toString() { return shortClassName(this) + "(" + day + ", " + month + ")"; }
public boolean equals(Object o) {
if (!(o instanceof Day)) return false;
Day x = (Day) o;
return eq(day, x.day) && eq(month, x.month);
}
public int hashCode() {
int h = 68476;
h = boostHashCombine(h, _hashCode(day));
h = boostHashCombine(h, _hashCode(month));
return h;
}
public Object[] _fieldsToList() { return new Object[] {day, month}; }
public Object transformUsing(IF1 f) { return new Day((int) f.get(day), (Month) f.get(month)); }
public void visitUsing(IVF1 f) { f.get(day); f.get(month); }
}
static class TodayPlus extends SomeDate implements IFieldsToList, Transformable, Visitable{
int nDays;
TodayPlus() {}
TodayPlus(int nDays) {
this.nDays = nDays;}
public String toString() { return shortClassName(this) + "(" + nDays + ")"; }
public boolean equals(Object o) {
if (!(o instanceof TodayPlus)) return false;
TodayPlus x = (TodayPlus) o;
return eq(nDays, x.nDays);
}
public int hashCode() {
int h = 123418715;
h = boostHashCombine(h, _hashCode(nDays));
return h;
}
public Object[] _fieldsToList() { return new Object[] {nDays}; }
public Object transformUsing(IF1 f) { return new TodayPlus((int) f.get(nDays)); }
public void visitUsing(IVF1 f) { f.get(nDays); }
}
// weekdays
static class Weekday extends SomeDateDate implements IFieldsToList, Transformable, Visitable{
int weekday;
SomeWeek week;
Weekday() {}
Weekday(int weekday, SomeWeek week) {
this.week = week;
this.weekday = weekday;}
public String toString() { return shortClassName(this) + "(" + weekday + ", " + week + ")"; }
public boolean equals(Object o) {
if (!(o instanceof Weekday)) return false;
Weekday x = (Weekday) o;
return eq(weekday, x.weekday) && eq(week, x.week);
}
public int hashCode() {
int h = -1403451640;
h = boostHashCombine(h, _hashCode(weekday));
h = boostHashCombine(h, _hashCode(week));
return h;
}
public Object[] _fieldsToList() { return new Object[] {weekday, week}; }
public Object transformUsing(IF1 f) { return new Weekday((int) f.get(weekday), (SomeWeek) f.get(week)); }
public void visitUsing(IVF1 f) { f.get(weekday); f.get(week); }
// weekday is in Java count (1=Sunday)
Weekday(int weekday) { this.weekday = weekday; }
}
// hours
static class Hour extends SomeTime implements IFieldsToList, Transformable, Visitable{
int hour;
Boolean isPM;
Day day;
Hour() {}
Hour(int hour, Boolean isPM, Day day) {
this.day = day;
this.isPM = isPM;
this.hour = hour;}
public String toString() { return shortClassName(this) + "(" + hour + ", " + isPM + ", " + day + ")"; }
public boolean equals(Object o) {
if (!(o instanceof Hour)) return false;
Hour x = (Hour) o;
return eq(hour, x.hour) && eq(isPM, x.isPM) && eq(day, x.day);
}
public int hashCode() {
int h = 2255364;
h = boostHashCombine(h, _hashCode(hour));
h = boostHashCombine(h, _hashCode(isPM));
h = boostHashCombine(h, _hashCode(day));
return h;
}
public Object[] _fieldsToList() { return new Object[] {hour, isPM, day}; }
public Object transformUsing(IF1 f) { return new Hour((int) f.get(hour), (Boolean) f.get(isPM), (Day) f.get(day)); }
public void visitUsing(IVF1 f) { f.get(hour); f.get(isPM); f.get(day); }
Hour(int hour, Boolean isPM) { this(hour, isPM, null); }
}
static class CurrentHourPlus extends SomeTime implements IFieldsToList, Transformable, Visitable{
int nHours;
CurrentHourPlus() {}
CurrentHourPlus(int nHours) {
this.nHours = nHours;}
public String toString() { return shortClassName(this) + "(" + nHours + ")"; }
public boolean equals(Object o) {
if (!(o instanceof CurrentHourPlus)) return false;
CurrentHourPlus x = (CurrentHourPlus) o;
return eq(nHours, x.nHours);
}
public int hashCode() {
int h = 1011201559;
h = boostHashCombine(h, _hashCode(nHours));
return h;
}
public Object[] _fieldsToList() { return new Object[] {nHours}; }
public Object transformUsing(IF1 f) { return new CurrentHourPlus((int) f.get(nHours)); }
public void visitUsing(IVF1 f) { f.get(nHours); }
}
// minutes
static class Minute extends SomeTime implements IFieldsToList, Transformable, Visitable{
int minute;
Hour hour;
Minute() {}
Minute(int minute, Hour hour) {
this.hour = hour;
this.minute = minute;}
public String toString() { return shortClassName(this) + "(" + minute + ", " + hour + ")"; }
public boolean equals(Object o) {
if (!(o instanceof Minute)) return false;
Minute x = (Minute) o;
return eq(minute, x.minute) && eq(hour, x.hour);
}
public int hashCode() {
int h = -1990159820;
h = boostHashCombine(h, _hashCode(minute));
h = boostHashCombine(h, _hashCode(hour));
return h;
}
public Object[] _fieldsToList() { return new Object[] {minute, hour}; }
public Object transformUsing(IF1 f) { return new Minute((int) f.get(minute), (Hour) f.get(hour)); }
public void visitUsing(IVF1 f) { f.get(minute); f.get(hour); }
}
static class CurrentMinutePlus extends SomeTime implements IFieldsToList, Transformable, Visitable{
int nMinutes;
CurrentMinutePlus() {}
CurrentMinutePlus(int nMinutes) {
this.nMinutes = nMinutes;}
public String toString() { return shortClassName(this) + "(" + nMinutes + ")"; }
public boolean equals(Object o) {
if (!(o instanceof CurrentMinutePlus)) return false;
CurrentMinutePlus x = (CurrentMinutePlus) o;
return eq(nMinutes, x.nMinutes);
}
public int hashCode() {
int h = -1844800505;
h = boostHashCombine(h, _hashCode(nMinutes));
return h;
}
public Object[] _fieldsToList() { return new Object[] {nMinutes}; }
public Object transformUsing(IF1 f) { return new CurrentMinutePlus((int) f.get(nMinutes)); }
public void visitUsing(IVF1 f) { f.get(nMinutes); }
}
// seconds
static class Second extends SomeTime implements IFieldsToList, Transformable, Visitable{
int second;
Minute minute;
Second() {}
Second(int second, Minute minute) {
this.minute = minute;
this.second = second;}
public String toString() { return shortClassName(this) + "(" + second + ", " + minute + ")"; }
public boolean equals(Object o) {
if (!(o instanceof Second)) return false;
Second x = (Second) o;
return eq(second, x.second) && eq(minute, x.minute);
}
public int hashCode() {
int h = -1822412652;
h = boostHashCombine(h, _hashCode(second));
h = boostHashCombine(h, _hashCode(minute));
return h;
}
public Object[] _fieldsToList() { return new Object[] {second, minute}; }
public Object transformUsing(IF1 f) { return new Second((int) f.get(second), (Minute) f.get(minute)); }
public void visitUsing(IVF1 f) { f.get(second); f.get(minute); }
}
// special stuff
static class BeginningOfTime extends SomeDate implements IFieldsToList, Transformable, Visitable{
BeginningOfTime() {}
public String toString() { return shortClassName(this) + "(" + ")"; }
public boolean equals(Object o) {
return o instanceof BeginningOfTime;
}
public int hashCode() {
int h = -1618841503;
return h;
}
public Object[] _fieldsToList() { return null; }
public Object transformUsing(IF1 f) { return this; }
public void visitUsing(IVF1 f) { }
}
static class EndOfTime extends SomeDate implements IFieldsToList, Transformable, Visitable{
EndOfTime() {}
public String toString() { return shortClassName(this) + "(" + ")"; }
public boolean equals(Object o) {
return o instanceof EndOfTime;
}
public int hashCode() {
int h = -810134049;
return h;
}
public Object[] _fieldsToList() { return null; }
public Object transformUsing(IF1 f) { return this; }
public void visitUsing(IVF1 f) { }
}
// date ranges & boolean operations
static class Between extends DateProp implements IFieldsToList, Transformable, Visitable{
SomeDate from;
SomeDate to;
Between() {}
Between(SomeDate from, SomeDate to) {
this.to = to;
this.from = from;}
public String toString() { return shortClassName(this) + "(" + from + ", " + to + ")"; }
public boolean equals(Object o) {
if (!(o instanceof Between)) return false;
Between x = (Between) o;
return eq(from, x.from) && eq(to, x.to);
}
public int hashCode() {
int h = 1448018920;
h = boostHashCombine(h, _hashCode(from));
h = boostHashCombine(h, _hashCode(to));
return h;
}
public Object[] _fieldsToList() { return new Object[] {from, to}; }
public Object transformUsing(IF1 f) { return new Between((SomeDate) f.get(from), (SomeDate) f.get(to)); }
public void visitUsing(IVF1 f) { f.get(from); f.get(to); }
}
static class Or extends DateProp implements IFieldsToList, Transformable, Visitable{
DateProp a;
DateProp b;
Or() {}
Or(DateProp a, DateProp b) {
this.b = b;
this.a = a;}
public String toString() { return shortClassName(this) + "(" + a + ", " + b + ")"; }
public boolean equals(Object o) {
if (!(o instanceof Or)) return false;
Or x = (Or) o;
return eq(a, x.a) && eq(b, x.b);
}
public int hashCode() {
int h = 2563;
h = boostHashCombine(h, _hashCode(a));
h = boostHashCombine(h, _hashCode(b));
return h;
}
public Object[] _fieldsToList() { return new Object[] {a, b}; }
public Object transformUsing(IF1 f) { return new Or((DateProp) f.get(a), (DateProp) f.get(b)); }
public void visitUsing(IVF1 f) { f.get(a); f.get(b); }
}
static class And extends DateProp implements IFieldsToList, Transformable, Visitable{
DateProp a;
DateProp b;
And() {}
And(DateProp a, DateProp b) {
this.b = b;
this.a = a;}
public String toString() { return shortClassName(this) + "(" + a + ", " + b + ")"; }
public boolean equals(Object o) {
if (!(o instanceof And)) return false;
And x = (And) o;
return eq(a, x.a) && eq(b, x.b);
}
public int hashCode() {
int h = 65975;
h = boostHashCombine(h, _hashCode(a));
h = boostHashCombine(h, _hashCode(b));
return h;
}
public Object[] _fieldsToList() { return new Object[] {a, b}; }
public Object transformUsing(IF1 f) { return new And((DateProp) f.get(a), (DateProp) f.get(b)); }
public void visitUsing(IVF1 f) { f.get(a); f.get(b); }
}
static class Not extends DateProp implements IFieldsToList, Transformable, Visitable{
DateProp a;
Not() {}
Not(DateProp a) {
this.a = a;}
public String toString() { return shortClassName(this) + "(" + a + ")"; }
public boolean equals(Object o) {
if (!(o instanceof Not)) return false;
Not x = (Not) o;
return eq(a, x.a);
}
public int hashCode() {
int h = 78515;
h = boostHashCombine(h, _hashCode(a));
return h;
}
public Object[] _fieldsToList() { return new Object[] {a}; }
public Object transformUsing(IF1 f) { return new Not((DateProp) f.get(a)); }
public void visitUsing(IVF1 f) { f.get(a); }
}
// some utility functions
static boolean containsTimes(SomeDate d) {
return defaultMetaTransformer().any(o -> o instanceof SomeTime, d);
}
static boolean containsDateDates(SomeDate d) {
return defaultMetaTransformer().any(o -> o instanceof SomeDateDate, d);
}
}
static String shortClassName(Object o) {
if (o == null) return null;
Class c = o instanceof Class ? (Class) o : o.getClass();
String name = c.getName();
return shortenClassName(name);
}
static boolean eq(Object a, Object b) {
return a == b || a != null && b != null && a.equals(b);
}
static int boostHashCombine(int a, int b) {
return a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2));
}
static int _hashCode(Object a) {
return a == null ? 0 : a.hashCode();
}
// MetaTransformer that understands Transformable and List
static MetaTransformer defaultMetaTransformer() {
return metaTransformer_transformableAndList();
}
static String shortenClassName(String name) {
if (name == null) return null;
int i = lastIndexOf(name, "$");
if (i < 0) i = lastIndexOf(name, ".");
return i < 0 ? name : substring(name, i+1);
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
// MetaTransformer that understands Transformable and List
static MetaTransformer metaTransformer_transformableAndList() {
return new MetaTransformer(new MetaTransformer.StructureHandler() {
public Object transform(Object o, IF1 recurse) {
if (o instanceof Transformable)
return ((Transformable) o).transformUsing(recurse);
if (o instanceof List)
return map_ping(recurse, ((List) o));
return null;
}
public void visit(Object o, IVF1 recurse) {
if (o instanceof Visitable)
((Visitable) o).visitUsing(recurse);
else if (o instanceof List)
for (Object x : ((List) o))
{ ping(); recurse.get(x); }
}
});
}
static int lastIndexOf(String a, String b) {
return a == null || b == null ? -1 : a.lastIndexOf(b);
}
static int lastIndexOf(String a, char b) {
return a == null ? -1 : a.lastIndexOf(b);
}
// starts searching from i-1
static int lastIndexOf(List l, int i, A a) {
if (l == null) return -1;
for (i = min(l(l), i)-1; i >= 0; i--)
if (eq(l.get(i), a))
return i;
return -1;
}
static String substring(String s, int x) {
return substring(s, x, strL(s));
}
static String substring(String s, int x, int y) {
if (s == null) return null;
if (x < 0) x = 0;
if (x >= s.length()) return "";
if (y < x) y = x;
if (y > s.length()) y = s.length();
return s.substring(x, y);
}
// convenience method for quickly dropping a prefix
static String substring(String s, CharSequence l) {
return substring(s, l(l));
}
static List map_ping(Iterable l, Object f) { return map_ping(f, l); }
static List map_ping(Object f, Iterable l) {
List x = emptyList(l);
if (l != null) for (Object o : l)
x.add(callF(f, o));
return x;
}
static List map_ping(Iterable l, F1 f) { return map_ping(f, l); }
static List map_ping(F1 f, Iterable l) {
List x = emptyList(l);
if (l != null) for (A o : l)
x.add(callF(f, o));
return x;
}
static List map_ping(IF1 f, Iterable l) { return map_ping(l, f); }
static List map_ping(Iterable l, IF1 f) {
List x = emptyList(l);
if (l != null) for (A o : l)
{ ping(); x.add(f.get(o)); }
return x;
}
static List map_ping(IF1 f, A[] l) { return map_ping(l, f); }
static List map_ping(A[] l, IF1 f) {
List x = emptyList(l);
if (l != null) for (A o : l)
{ ping(); x.add(f.get(o)); }
return x;
}
static List map_ping(Object f, Object[] l) { return map_ping(f, asList(l)); }
static List map_ping(Object[] l, Object f) { return map_ping(f, l); }
static List map_ping(Object f, Map map) {
return map_ping(map, f);
}
// map_ping: func(key, value) -> list element
static List map_ping(Map map, Object f) {
List x = new ArrayList();
if (map != null) for (Object _e : map.entrySet()) { ping();
Map.Entry e = (Map.Entry) _e;
x.add(callF(f, e.getKey(), e.getValue()));
}
return x;
}
static List map_ping(Map map, IF2 f) {
return map_ping(map, (Object) f);
}
//sbool ping_actions_shareable = true;
static volatile boolean ping_pauseAll = false;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile boolean ping_anyActions = false;
static Map ping_actions = newWeakHashMap();
static ThreadLocal ping_isCleanUpThread = new ThreadLocal();
// always returns true
static boolean ping() {
if (ping_pauseAll || ping_anyActions) ping_impl(true /* XXX */);
//ifndef LeanMode ping_impl(); endifndef
return true;
}
// returns true when it slept
static boolean ping_impl(boolean okInCleanUp) { try {
if (ping_pauseAll && !isAWTThread()) {
do
Thread.sleep(ping_sleep);
while (ping_pauseAll);
return true;
}
if (ping_anyActions) { // don't allow sharing ping_actions
if (!okInCleanUp && !isTrue(ping_isCleanUpThread.get()))
failIfUnlicensed();
Object action = null;
synchronized(ping_actions) {
if (!ping_actions.isEmpty()) {
action = ping_actions.get(currentThread());
if (action instanceof Runnable)
ping_actions.remove(currentThread());
if (ping_actions.isEmpty()) ping_anyActions = false;
}
}
if (action instanceof Runnable)
((Runnable) action).run();
else if (eq(action, "cancelled"))
throw fail("Thread cancelled.");
}
return false;
} catch (Exception __e) { throw rethrow(__e); } }
static int min(int a, int b) {
return Math.min(a, b);
}
static long min(long a, long b) {
return Math.min(a, b);
}
static float min(float a, float b) { return Math.min(a, b); }
static float min(float a, float b, float c) { return min(min(a, b), c); }
static double min(double a, double b) {
return Math.min(a, b);
}
static double min(double[] c) {
double x = Double.MAX_VALUE;
for (double d : c) x = Math.min(x, d);
return x;
}
static float min(float[] c) {
float x = Float.MAX_VALUE;
for (float d : c) x = Math.min(x, d);
return x;
}
static byte min(byte[] c) {
byte x = 127;
for (byte d : c) if (d < x) x = d;
return x;
}
static short min(short[] c) {
short x = 0x7FFF;
for (short d : c) if (d < x) x = d;
return x;
}
static int min(int[] c) {
int x = Integer.MAX_VALUE;
for (int d : c) if (d < x) x = d;
return x;
}
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(Object o) {
return o == null ? 0
: o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: o instanceof Collection ? l((Collection) o)
: o instanceof Object[] ? l((Object[]) o)
: o instanceof boolean[] ? l((boolean[]) o)
: o instanceof byte[] ? l((byte[]) o)
: o instanceof char[] ? l((char[]) o)
: o instanceof short[] ? l((short[]) o)
: o instanceof int[] ? l((int[]) o)
: o instanceof float[] ? l((float[]) o)
: o instanceof double[] ? l((double[]) o)
: o instanceof long[] ? l((long[]) o)
: (Integer) call(o, "size");
}
static int strL(String s) {
return s == null ? 0 : s.length();
}
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 Map> callF_cache = newDangerousWeakHashMap();
static B callF(F1 f, A a) {
return f == null ? null : f.get(a);
}
static B callF(IF1 f, A a) {
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 Object callF(Object f, Object... args) { try {
if (f instanceof String)
return callMCWithVarArgs((String) f, args); // possible SLOWDOWN over callMC
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) {
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));
} catch (Exception __e) { throw rethrow(__e); } }
// 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;
}
// 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(int[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (int 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(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(Enumeration e) {
ArrayList l = new ArrayList();
if (e != null)
while (e.hasMoreElements())
l.add(e.nextElement());
return l;
}
static Map newWeakHashMap() {
return _registerWeakMap(synchroMap(new WeakHashMap()));
}
// TODO: test if android complains about this
static boolean isAWTThread() {
if (isAndroid()) return false;
if (isHeadless()) return false;
return isAWTThread_awt();
}
static boolean isAWTThread_awt() {
return SwingUtilities.isEventDispatchThread();
}
static boolean isTrue(Object o) {
if (o instanceof Boolean)
return ((Boolean) o).booleanValue();
if (o == null) return false;
if (o instanceof ThreadLocal) // TODO: remove this
return isTrue(((ThreadLocal) o).get());
throw fail(getClassName(o));
}
static void failIfUnlicensed() {
assertTrue("license off", licensed());
}
static Thread currentThread() {
return Thread.currentThread();
}
static RuntimeException fail() { throw new RuntimeException("fail"); }
static RuntimeException fail(Throwable e) { throw asRuntimeException(e); }
static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); }
static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); }
static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); }
static 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 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 Object call(Object o) {
return callF(o);
}
// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] {arg});
}
static Object call(Object o, String method, Object... args) {
//ret call_cached(o, method, args);
return call_withVarargs(o, method, args);
}
static 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 int max(Collection c) {
int x = Integer.MIN_VALUE;
for (int i : c) x = max(x, i);
return 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 Map newDangerousWeakHashMap() {
return _registerDangerousWeakMap(synchroMap(new WeakHashMap()));
}
// initFunction: voidfunc(Map) - is called initially, and after clearing the map
static Map newDangerousWeakHashMap(Object initFunction) {
return _registerDangerousWeakMap(synchroMap(new WeakHashMap()), initFunction);
}
static Object callMCWithVarArgs(String method, Object... args) {
return call_withVarargs(mc(), method, args);
}
static String getClassName(Object o) {
return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName();
}
static Object invokeMethod(Method m, Object o, Object... args) { try {
try {
return m.invoke(o, args);
} catch (InvocationTargetException e) {
throw rethrow(getExceptionCause(e));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(e.getMessage() + " - was calling: " + m + ", args: " + joinWithSpace(classNames(args)));
}
} catch (Exception __e) { throw rethrow(__e); } }
static boolean call_checkArgs(Method m, Object[] args, boolean debug) {
Class>[] types = m.getParameterTypes();
if (types.length != args.length) {
if (debug)
print("Bad parameter length: " + args.length + " vs " + types.length);
return false;
}
for (int i = 0; i < types.length; i++) {
Object arg = args[i];
if (!(arg == null ? !types[i].isPrimitive()
: isInstanceX(types[i], arg))) {
if (debug)
print("Bad parameter " + i + ": " + arg + " vs " + types[i]);
return false;
}
}
return true;
}
static Field makeAccessible(Field f) {
try {
f.setAccessible(true);
} catch (Throwable e) {
// Note: The error reporting only works with Java VM option --illegal-access=deny
vmBus_send("makeAccessible_error",e, f);
}
return f;
}
static Method makeAccessible(Method m) {
try {
m.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error",e, m);
}
return m;
}
static Constructor makeAccessible(Constructor c) {
try {
c.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error",e, c);
}
return c;
}
static List _registerWeakMap_preList;
static A _registerWeakMap(A map) {
if (javax() == null) {
// We're in class init
if (_registerWeakMap_preList == null) _registerWeakMap_preList = synchroList();
_registerWeakMap_preList.add(map);
return map;
}
try {
call(javax(), "_registerWeakMap", map);
} catch (Throwable e) {
printException(e);
print("Upgrade JavaX!!");
}
return map;
}
static void _onLoad_registerWeakMap() {
assertNotNull(javax());
if (_registerWeakMap_preList == null) return;
for (Object o : _registerWeakMap_preList)
_registerWeakMap(o);
_registerWeakMap_preList = null;
}
static Map synchroMap() {
return synchroHashMap();
}
static Map synchroMap(Map map) {
return Collections.synchronizedMap(map);
}
static int isAndroid_flag;
static boolean isAndroid() {
if (isAndroid_flag == 0)
isAndroid_flag = System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0 ? 1 : -1;
return isAndroid_flag > 0;
}
static Boolean isHeadless_cache;
static boolean isHeadless() {
if (isHeadless_cache != null) return isHeadless_cache;
if (isAndroid()) return isHeadless_cache = true;
if (GraphicsEnvironment.isHeadless()) return isHeadless_cache = true;
// Also check if AWT actually works.
// If DISPLAY variable is set but no X server up, this will notice.
try {
SwingUtilities.isEventDispatchThread();
return isHeadless_cache = false;
} catch (Throwable e) { return isHeadless_cache = true; }
}
static void assertTrue(Object o) {
if (!(eq(o, true) /*|| isTrue(pcallF(o))*/))
throw fail(str(o));
}
static boolean assertTrue(String msg, boolean b) {
if (!b)
throw fail(msg);
return b;
}
static boolean assertTrue(boolean b) {
if (!b)
throw fail("oops");
return b;
}
static volatile boolean licensed_yes = true;
static boolean licensed() {
if (!licensed_yes) return false;
ping_okInCleanUp();
return true;
}
static void licensed_off() {
licensed_yes = false;
}
static RuntimeException asRuntimeException(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static void _handleError(Error e) {
call(javax(), "_handleError", e);
}
static Object call_withVarargs(Object o, String method, Object... args) { try {
if (o == null) return null;
if (o instanceof Class) {
Class c = (Class) o;
_MethodCache cache = callOpt_getCache(c);
Method me = cache.findStaticMethod(method, args);
if (me != null)
return invokeMethod(me, null, args);
// try varargs
List methods = cache.cache.get(method);
if (methods != null) methodSearch: for (Method m : methods) {
{ if (!(m.isVarArgs())) continue; }
{ if (!(isStaticMethod(m))) continue; }
Object[] newArgs = massageArgsForVarArgsCall(m, args);
if (newArgs != null)
return invokeMethod(m, null, newArgs);
}
throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found");
} else {
Class c = o.getClass();
_MethodCache cache = callOpt_getCache(c);
Method me = cache.findMethod(method, args);
if (me != null)
return invokeMethod(me, o, args);
// try varargs
List methods = cache.cache.get(method);
if (methods != null) methodSearch: for (Method m : methods) {
{ if (!(m.isVarArgs())) continue; }
Object[] newArgs = massageArgsForVarArgsCall(m, args);
if (newArgs != null)
return invokeMethod(m, o, newArgs);
}
throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found");
}
} catch (Exception __e) { throw rethrow(__e); } }
static List _registerDangerousWeakMap_preList;
static A _registerDangerousWeakMap(A map) {
return _registerDangerousWeakMap(map, null);
}
static A _registerDangerousWeakMap(A map, Object init) {
callF(init, map);
if (init instanceof String) {
final String f = (String) init;
init = new VF1