The returned list iterator is fail-fast.
*
* @see #listIterator(int)
*/
public ListIterator listIterator() {
return new ListItr(0);
}
/**
* Returns an iterator over the elements in this list in proper sequence.
*
* The returned iterator is fail-fast.
*
* @return an iterator over the elements in this list in proper sequence
*/
public Iterator iterator() {
return concurrentlyIterateList(this);
}
/**
* An optimized version of AbstractList.Itr
*/
private class Itr implements Iterator {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public A next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = SynchronizedTypedArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (A) elementData[lastRet = i];
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
SynchronizedTypedArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
/**
* An optimized version of AbstractList.ListItr - TODO: replace with concurrent iterator or synchronize
*/
private class ListItr extends Itr implements ListIterator {
ListItr(int index) {
super();
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
@SuppressWarnings("unchecked")
public A previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = SynchronizedTypedArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (A) elementData[lastRet = i];
}
public void set(A e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
SynchronizedTypedArrayList.this.set(lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(A e) {
checkForComodification();
try {
int i = cursor;
SynchronizedTypedArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
public List subList(int fromIndex, int toIndex) {
_subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
}
private class SubList extends SynchronizedArrayList_Base implements RandomAccess {
private final SynchronizedArrayList_Base parent;
private final int parentOffset;
private final int offset;
int size;
SubList(SynchronizedArrayList_Base parent,
int offset, int fromIndex, int toIndex) {
this.parent = parent;
this.parentOffset = fromIndex;
this.offset = offset + fromIndex;
this.size = toIndex - fromIndex;
this.modCount = SynchronizedTypedArrayList.this.modCount;
}
public A set(int index, A e) { synchronized(SynchronizedTypedArrayList.this) {
rangeCheck(index);
checkForComodification();
A oldValue = SynchronizedTypedArrayList.this.elementData(offset + index);
SynchronizedTypedArrayList.this.elementData[offset + index] = e;
return oldValue;
}}
public A get(int index) { synchronized(SynchronizedTypedArrayList.this) {
rangeCheck(index);
checkForComodification();
return SynchronizedTypedArrayList.this.elementData(offset + index);
}}
public int size() { synchronized(SynchronizedTypedArrayList.this) {
checkForComodification();
return this.size;
}}
public void add(int index, A e) { synchronized(SynchronizedTypedArrayList.this) {
rangeCheckForAdd(index);
checkForComodification();
parent.add(parentOffset + index, e);
this.modCount = parent.modCount();
this.size++;
}}
public A remove(int index) { synchronized(SynchronizedTypedArrayList.this) {
rangeCheck(index);
checkForComodification();
A result = parent.remove(parentOffset + index);
this.modCount = parent.modCount();
this.size--;
return result;
}}
public void removeRange(int fromIndex, int toIndex) { synchronized(SynchronizedTypedArrayList.this) {
checkForComodification();
parent.removeRange(parentOffset + fromIndex,
parentOffset + toIndex);
this.modCount = parent.modCount();
this.size -= toIndex - fromIndex;
}}
public boolean addAll(Collection extends A> c) {
return addAll(this.size, c);
}
public boolean addAll(int index, Collection extends A> c) { synchronized(SynchronizedTypedArrayList.this) {
rangeCheckForAdd(index);
int cSize = c.size();
if (cSize==0)
return false;
checkForComodification();
parent.addAll(parentOffset + index, c);
this.modCount = parent.modCount();
this.size += cSize;
return true;
}}
public Iterator iterator() {
return listIterator();
}
public ListIterator listIterator(final int index) { synchronized(SynchronizedTypedArrayList.this) {
checkForComodification();
rangeCheckForAdd(index);
final int offset = this.offset;
return new ListIterator() {
int cursor = index;
int lastRet = -1;
int expectedModCount = SynchronizedTypedArrayList.this.modCount;
public boolean hasNext() {
return cursor != SubList.this.size;
}
@SuppressWarnings("unchecked")
public A next() {
checkForComodification();
int i = cursor;
if (i >= SubList.this.size)
throw new NoSuchElementException();
Object[] elementData = SynchronizedTypedArrayList.this.elementData;
if (offset + i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (A) elementData[offset + (lastRet = i)];
}
public boolean hasPrevious() {
return cursor != 0;
}
@SuppressWarnings("unchecked")
public A previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = SynchronizedTypedArrayList.this.elementData;
if (offset + i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (A) elementData[offset + (lastRet = i)];
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
SubList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = SynchronizedTypedArrayList.this.modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void set(A e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
SynchronizedTypedArrayList.this.set(offset + lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(A e) {
checkForComodification();
try {
int i = cursor;
SubList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = SynchronizedTypedArrayList.this.modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (expectedModCount != SynchronizedTypedArrayList.this.modCount)
throw new ConcurrentModificationException();
}
};
}}
public List subList(int fromIndex, int toIndex) { synchronized(SynchronizedTypedArrayList.this) {
_subListRangeCheck(fromIndex, toIndex, size);
return new SubList(parent, offset, fromIndex, toIndex);
}}
private void rangeCheck(int index) {
if (index < 0 || index >= this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void rangeCheckForAdd(int index) {
if (index < 0 || index > this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+this.size;
}
private void checkForComodification() {
if (SynchronizedTypedArrayList.this.modCount != this.modCount)
throw new ConcurrentModificationException();
}
}
@Override
@SuppressWarnings("unchecked")
public synchronized void sort(Comparator super A> c) {
final int expectedModCount = modCount;
Arrays.sort((A[]) elementData, 0, size, c);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
Class extends A> elementType() {
return arrayElementType(elementData);
}
}
/*
* @(#)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();
}
}
// TODO: subclass RuntimeException and use Meta instead of DynamicObject
static class PersistableThrowable extends DynamicObject {
String className;
String msg;
String stacktrace;
final public PersistableThrowable setActualThrowable(Throwable actualThrowable){ return actualThrowable(actualThrowable); }
public PersistableThrowable actualThrowable(Throwable actualThrowable) { this.actualThrowable = actualThrowable; return this; } final public Throwable getActualThrowable(){ return actualThrowable(); }
public Throwable actualThrowable() { return actualThrowable; }
transient Throwable actualThrowable;
PersistableThrowable() {}
PersistableThrowable(Throwable e) {
actualThrowable = e;
if (e == null)
className = "Crazy Null Error";
else {
className = getClassName(e).replace('/', '.');
msg = e.getMessage();
stacktrace = getStackTrace_noRecord(e);
}
}
public String toString() {
return nempty(msg) ? className + ": " + msg : className;
}
RuntimeException asRuntimeException() {
if (actualThrowable != null)
return main.asRuntimeException(actualThrowable);
return new Fail(this);
}
}
static interface IVF1 {
void get(A a);
}
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 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); }
}
static class PingSourceCancelledException extends RuntimeException implements IFieldsToList{
PingSource pingSource;
PingSourceCancelledException() {}
PingSourceCancelledException(PingSource pingSource) {
this.pingSource = pingSource;}
public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + pingSource + ")"; }public Object[] _fieldsToList() { return new Object[] {pingSource}; }
}
static interface IFieldsToList {
Object[] _fieldsToList();
}
interface ISleeper_v2 {
default Sleeping doLater(long targetTime, Runnable r) {
return doLater(sysTimeToTimestamp(targetTime), r);
}
Sleeping doLater(Timestamp targetTime, Runnable r);
public default Sleeping doAfter(double seconds, Runnable r) {
return doLater(tsNow().plusSeconds(seconds), r);
}
}
abstract static class SynchronizedArrayList_Base extends AbstractList {
final int modCount() { return modCount; }
public void removeRange(int i, int j) { super.removeRange(i, j); }
}
// The idea is to leave max as the actual number of cores the system
// has (numberOfCores()), and in case of being fully booked, raise an
// alert (customerMustWaitAlert) which can be handled by a strategy
// object (different reactions are possible).
// If nothing is done in such an event, clients are processed serially
// (no guarantees of order), split up among the available threads.
/* SYNChronisation order:
1. PooledThread
2. ThreadPool */
static class ThreadPool implements AutoCloseable {
int max = numberOfCores();
List all = new ArrayList();
Set used = new HashSet();
Set free = new HashSet();
boolean verbose, retired;
// our own ping surce so we can start threads & keep them running
class InternalPingSource extends PingSource {}
InternalPingSource internalPingSource = new InternalPingSource();
MultiSleeper sleeper = new MultiSleeper();
ThreadPool() {}
ThreadPool(int max) {
this.max = max;}
synchronized int maxSize() { return max; }
synchronized int total() { return l(used)+l(free); }
transient Set onCustomerMustWaitAlert;
public ThreadPool onCustomerMustWaitAlert(Runnable r) { onCustomerMustWaitAlert = createOrAddToSyncLinkedHashSet(onCustomerMustWaitAlert, r); return this; }
public ThreadPool removeCustomerMustWaitAlertListener(Runnable r) { main.remove(onCustomerMustWaitAlert, r); return this; }
public void customerMustWaitAlert() { if (onCustomerMustWaitAlert != null) for (var listener : onCustomerMustWaitAlert) pcallF_typed(listener); }
void fireCustomerMustWaitAlert() {
vmBus_send("customerMustWaitAlert", this, currentThread());
customerMustWaitAlert();
}
// DOESN'T WAIT. adds action to a thread's queue if nothing is
// available immediately.
PooledThread acquireThreadOrQueue(Runnable action) {
if (action == null) return null;
PooledThread t;
synchronized(this) {
if (_hasFreeAfterCreating()) {
t = _firstFreeThread();
markUsed(t);
} else
t = _anyThread();
}
t.addWork(action); // will move it from free to used
return t;
}
// run in synchronized block
boolean _hasFreeAfterCreating() {
checkNotRetired();
if (nempty(free)) return true;
if (total() < max) {
PooledThread t = newThread();
all.add(t);
free.add(t);
return true;
}
return false;
}
// WAITS until thread is available
PooledThread acquireThreadOrWait(Runnable action) { try {
if (action == null) return null;
PooledThread t;
while (true) {
synchronized(this) {
if (_hasFreeAfterCreating()) {
t = _firstFreeThread();
break;
} else
_waitWaitWait();
}
}
t.addWork(action);
return t;
} catch (Exception __e) { throw rethrow(__e); } }
PooledThread _firstFreeThread() {
return first(free);
}
PooledThread _anyThread() {
return random(used);
}
class PooledThread extends Thread {
PooledThread(String name) { super(name); }
AppendableChain q;
synchronized Runnable _grabWorkOrSleep() { try {
Runnable r = first(q);
if (r == null) {
markFree(this);
if (verbose) print("Thread sleeps");
synchronized(this) { wait(); }
if (verbose) print("Thread woke up");
return null;
}
q = popFirst(q);
return r;
} catch (Exception __e) { throw rethrow(__e); } }
public void run() { try {
pingSource_tl().set(internalPingSource);
while (!retired()) { ping();
Runnable r = _grabWorkOrSleep();
if (verbose) print(this + " work: " + r);
if (r != null)
try {
if (verbose) print(this + " running: " + r);
r.run();
pingSource_tl().set(internalPingSource);
if (verbose) print(this + " done");
} catch (Throwable e) {
pingSource_tl().set(internalPingSource);
if (verbose) print(this + " error");
printStackTrace(e);
} finally {
pingSource_tl().set(internalPingSource);
if (verbose) print("ThreadPool finally");
}
}
} catch (Exception __e) { throw rethrow(__e); } }
synchronized boolean isEmpty() { return empty(q); }
// append to q (do later)
void addWork(Runnable r) {
if (verbose) print("Added work to " + this + ": " + r);
synchronized(this) {
q = chainPlus(q, r);
notifyAll();
}
}
}
PooledThread newThread() {
PooledThread t = new PooledThread("Thread Pool Inhabitant " + n2(total()+1));
t.start();
return t;
}
synchronized void markFree(PooledThread t) {
used.remove(t);
free.add(t);
notifyAll();
}
synchronized void markUsed(PooledThread t) {
free.remove(t);
used.add(t);
}
synchronized public String toString() {
return retired()
? "Retired ThreadPool"
: "ThreadPool " + roundBracket(commaCombine(
n2(used) + " used out of " + n2(total()),
max <= total() ? null : "could grow to " + n2(max)));
}
synchronized boolean retired() { return retired; }
synchronized void retire() {
if (verbose) print("ThreadPool Retiring");
retired = true;
for (var thread : free) syncNotifyAll(thread); // wake it up so it exits
}
void checkNotRetired() {
if (retired()) throw fail("retired");
}
// We could do a soft-close here (stop the idle threads, let running threads finish, then end those too, stop accepting new orders)
// or a hard close (interrupt all threads, stop accepting new orders)
synchronized public void close() { try {
retire();
} catch (Exception __e) { throw rethrow(__e); } }
// run in synchronized block
void _waitWaitWait() { try {
do {
fireCustomerMustWaitAlert();
wait();
checkNotRetired();
} while (empty(free));
} catch (Exception __e) { throw rethrow(__e); } }
void dO(String text, Runnable r) {
if (r == null) return;
new PingSource(this, text).dO(r);
}
ISleeper_v2 sleeper() { return sleeper; }
}
interface IMultiMap {
public Set keySet();
public Collection get(A a);
public int size();
public int keyCount();
}
// differences to AbstractList.subList / ArrayList.subList:
// -probably doesn't handle modCount the same way
//
// made from AbstractList.subList
static class SubList extends AbstractList implements ISubList {
List root;
SubList parent;
int offset;
int size;
/**
* Constructs a sublist of an arbitrary AbstractList, which is
* not a SubList itself.
*/
public SubList(List root, int fromIndex, int toIndex) {
if (root instanceof SubList) {
this.parent = (SubList) root;
this.root = ((SubList) root).root;
this.offset = ((SubList) root).offset + fromIndex;
} else {
this.parent = null;
this.root = root;
this.offset = fromIndex;
}
this.size = toIndex - fromIndex;
}
public E set(int index, E element) {
Objects.checkIndex(index, size);
checkForComodification();
return root.set(offset + index, element);
}
public E get(int index) {
Objects.checkIndex(index, size);
checkForComodification();
return root.get(offset + index);
}
public int size() {
checkForComodification();
return size;
}
public void add(int index, E element) {
rangeCheckForAdd(index);
checkForComodification();
root.add(offset + index, element);
updateSizeAndModCount(1);
}
public E remove(int index) {
Objects.checkIndex(index, size);
checkForComodification();
E result = root.remove(offset + index);
updateSizeAndModCount(-1);
return result;
}
protected void removeRange(int fromIndex, int toIndex) {
checkForComodification();
root.subList(offset + fromIndex, offset + toIndex).clear();
updateSizeAndModCount(fromIndex - toIndex);
}
public boolean addAll(Collection extends E> c) {
return addAll(size, c);
}
public boolean addAll(int index, Collection extends E> c) {
rangeCheckForAdd(index);
int cSize = c.size();
if (cSize==0)
return false;
checkForComodification();
root.addAll(offset + index, c);
updateSizeAndModCount(cSize);
return true;
}
public Iterator iterator() {
return listIterator();
}
public ListIterator listIterator(int index) {
checkForComodification();
rangeCheckForAdd(index);
return new ListIterator() {
private final ListIterator i =
root.listIterator(offset + index);
public boolean hasNext() {
return nextIndex() < size;
}
public E next() {
if (hasNext())
return i.next();
else
throw new NoSuchElementException();
}
public boolean hasPrevious() {
return previousIndex() >= 0;
}
public E previous() {
if (hasPrevious())
return i.previous();
else
throw new NoSuchElementException();
}
public int nextIndex() {
return i.nextIndex() - offset;
}
public int previousIndex() {
return i.previousIndex() - offset;
}
public void remove() {
i.remove();
updateSizeAndModCount(-1);
}
public void set(E e) {
i.set(e);
}
public void add(E e) {
i.add(e);
updateSizeAndModCount(1);
}
};
}
public List subList(int fromIndex, int toIndex) {
_subListRangeCheck(fromIndex, toIndex, size);
return new SubList<>(this, fromIndex, toIndex);
}
private void rangeCheckForAdd(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
private void checkForComodification() {}
private void updateSizeAndModCount(int sizeChange) {
SubList slist = this;
do {
slist.size += sizeChange;
slist = slist.parent;
} while (slist != null);
}
public List rootList() { return root; }
public List parentList() { return parent; }
public int subListOffset() { return offset; }
}
abstract static class Sleeping implements AutoCloseable , IFieldsToList{
Timestamp targetTime;
Runnable action;
Sleeping() {}
Sleeping(Timestamp targetTime, Runnable action) {
this.action = action;
this.targetTime = targetTime;}
public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + targetTime + ", " + action + ")"; }public Object[] _fieldsToList() { return new Object[] {targetTime, action}; }
long remainingMS() { return targetTime.minus(tsNow()); }
}
// AppendableChain has one "smart" head element (with size counter
// and pointer to the chain's last element), all the other nodes are
// maximally simple (MinimalChain).
// This allows O(1) front insertion, front removal and back insertion
// (not removal at the back though) which is fine for what I need this
// for (event queues).
//
// Stefan Reich, Oct 21
static class AppendableChain extends MinimalChain implements Iterable, IntSize {
MinimalChain last; // pointer to last element in chain (which may be us)
final public int getSize(){ return size(); }
public int size() { return size; }
int size; // total length of chain
AppendableChain() {} // only used internally
AppendableChain(A element) {
this.element = element; size = 1; last = this; }
// intermediate constructor called by itemPlusChain()
AppendableChain(A element, AppendableChain next) {
this.next = next;
this.element = element;
if (next == null) return;
MinimalChain b = new MinimalChain();
b.element = next.element;
b.next = next.next;
this.next = b;
last = next.last;
size = next.size+1;
}
public String toString() { return str(toList()); }
// append at the end
boolean add(A a) {
MinimalChain newLast = new MinimalChain(a);
last.next = newLast;
last = newLast;
++size;
return true;
}
// drop first element
AppendableChain popFirst() {
if (next == null) return null;
element = next.element;
if (last == next) last = this;
next = next.next;
--size;
return this;
}
ArrayList toList() {
ArrayList l = emptyList(size);
MinimalChain c = this;
while (c != null) {
l.add(c.element);
c = c.next;
}
return l;
}
//public Iterator iterator() { ret toList().iterator(); }
class ACIt extends IterableIterator < A > {
MinimalChain c = AppendableChain.this;
public boolean hasNext() {
return c != null;
}
public A next() {
var a = c.element;
c = c.next;
return a;
}
}
public IterableIterator iterator() {
return new ACIt();
}
}
static class MultiSleeper extends RestartableCountdown implements ISleeper_v2 {
MultiSetMap entries = treeMultiSetMap();
void check() {
var time = nextWakeUpTime();
var action = firstValue(entries);
setTargetTime(time == null ? 0 : time.sysTime(), new Runnable() { public void run() { try {
Set toCall;
synchronized(MultiSleeper.this) {
toCall = entries.get(time);
entries.remove(time);
check();
}
pcallFAll(toCall);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Set toCall;\r\n synchronized(MultiSleeper.this) {\r\n toCal..."; }});
}
synchronized void removeEntry(Timestamp targetTime, Runnable action) {
entries.remove(targetTime, action);
}
// API
synchronized Timestamp nextWakeUpTime() {
return firstKey(entries);
}
public synchronized Sleeping doLater(Timestamp targetTime, Runnable r) {
if (r == null || targetTime == null) return null;
targetTime = max(targetTime, tsNow());
entries.put(targetTime, r);
check();
return new Sleeping(targetTime, r) {
public void close() { try {
removeEntry(targetTime, r);
} catch (Exception __e) { throw rethrow(__e); } }
};
}
}
static class Timestamp implements Comparable , IFieldsToList{
long date;
Timestamp(long date) {
this.date = date;}
public boolean equals(Object o) {
if (!(o instanceof Timestamp)) return false;
Timestamp __1 = (Timestamp) o;
return date == __1.date;
}
public int hashCode() {
int h = 2059094262;
h = boostHashCombine(h, _hashCode(date));
return h;
}
public Object[] _fieldsToList() { return new Object[] {date}; }
Timestamp() { date = now(); }
Timestamp(Date date) { if (date != null) this.date = date.getTime(); }
final long toLong(){ return unixDate(); }
long unixDate() { return date; }
long unixSeconds() { return unixDate()/1000; }
public String toString() { return formatLocalDateWithSeconds(date); }
// Hmm. Should Timestamp(0) be equal to null? Question, questions...
public int compareTo(Timestamp t) {
return t == null ? 1 : cmp(date, t.date);
}
Timestamp plus(Seconds seconds) {
return plus(seconds == null ? null : seconds.getDouble());
}
final Timestamp plusSeconds(double seconds){ return plus(seconds); }
Timestamp plus(double seconds) {
return new Timestamp(date+toMS(seconds));
}
// returns milliseconds
long minus(Timestamp ts) {
return unixDate()-ts.unixDate();
}
long sysTime() {
return clockTimeToSystemTime(date);
}
Duration minusAsDuration(Timestamp ts) {
return Duration.ofMillis(minus(ts));
}
}
static interface ISubList {
public List rootList();
public List parentList();
public int subListOffset();
}
// uses hash sets as inner sets unless subclassed
// uses a hash map as the outer map by default
static class MultiSetMapimplements IMultiMap {
Map> data = new HashMap>();
int size; // number of values
MultiSetMap() {}
MultiSetMap(boolean useTreeMap) { if (useTreeMap) data = new TreeMap(); }
MultiSetMap(MultiSetMap map) { putAll(map); }
MultiSetMap(Map> data) {
this.data = data;}
boolean put(A key, B value) { synchronized(data) {
Set set = data.get(key);
if (set == null)
data.put(key, set = _makeEmptySet());
if (!set.add(value)) return false;
{ ++size; return true; }
}}
boolean add(A key, B value) { return put(key, value); }
void addAll(A key, Collection values) { synchronized(data) {
putAll(key, values);
}}
void addAllIfNotThere(A key, Collection values) { synchronized(data) {
for (B value : values)
setPut(key, value);
}}
void setPut(A key, B value) { synchronized(data) {
if (!containsPair(key, value))
put(key, value);
}}
final boolean contains(A key, B value){ return containsPair(key, value); }
boolean containsPair(A key, B value) { synchronized(data) {
return get(key).contains(value);
}}
void putAll(A key, Collection values) { synchronized(data) {
for (B value : values)
put(key, value);
}}
void removeAll(A key, Collection values) { synchronized(data) {
for (B value : values)
remove(key, value);
}}
public Set get(A key) { synchronized(data) {
Set set = data.get(key);
return set == null ? Collections. emptySet() : set;
}}
List getAndClear(A key) { synchronized(data) {
List l = cloneList(data.get(key));
remove(key);
return l;
}}
// return null if empty
Set getOpt(A key) { synchronized(data) {
return data.get(key);
}}
// returns actual mutable live set
// creates the set if not there
Set getActual(A key) { synchronized(data) {
Set set = data.get(key);
if (set == null)
data.put(key, set = _makeEmptySet());
return set;
}}
// TODO: this looks unnecessary
void clean(A key) { synchronized(data) {
Set list = data.get(key);
if (list != null && list.isEmpty())
data.remove(key);
}}
final public Set keys(){ return keySet(); }
public Set keySet() { synchronized(data) {
return data.keySet();
}}
void remove(A key) { synchronized(data) {
size -= l(data.get(key));
data.remove(key);
}}
void remove(A key, B value) { synchronized(data) {
Set set = data.get(key);
if (set != null) {
if (set.remove(value)) {
--size;
if (set.isEmpty())
data.remove(key);
}
}
}}
void clear() { synchronized(data) {
data.clear();
size = 0;
}}
boolean containsKey(A key) { synchronized(data) {
return data.containsKey(key);
}}
B getFirst(A key) { synchronized(data) {
return first(get(key));
}}
void addAll(MultiSetMap map) { putAll(map); }
void putAll(MultiSetMap map) { synchronized(data) {
for (A key : map.keySet())
putAll(key, map.get(key));
}}
void putAll(Map map) { synchronized(data) {
if (map != null) for (Map.Entry e : map.entrySet())
put(e.getKey(), e.getValue());
}}
final public int keyCount(){ return keysSize(); }
public int keysSize() { synchronized(data) { return l(data); }}
// full size
public int size() { synchronized(data) {
return size;
}}
// count values for key
int getSize(A key) { return l(data.get(key)); }
int count(A key) { return getSize(key); }
// expensive operation
Set reverseGet(B b) { synchronized(data) {
Set l = new HashSet();
for (A key : data.keySet())
if (data.get(key).contains(b))
l.add(key);
return l;
}}
// expensive operation
A keyForValue(B b) { synchronized(data) {
for (A key : data.keySet())
if (data.get(key).contains(b))
return key;
return null;
}}
Map> asMap() { synchronized(data) {
return cloneMap(data);
}}
boolean isEmpty() { synchronized(data) { return data.isEmpty(); }}
// override in subclasses
Set _makeEmptySet() {
return new HashSet();
}
Collection> allLists() {
synchronized(data) {
return new HashSet(data.values());
}
}
List allValues() {
return concatLists(values(data));
}
List> allEntries() { synchronized(data) {
List> l = emptyList(size);
for (Map.Entry extends A, ? extends Set> __0 : _entrySet( data))
{ A a = __0.getKey(); Set set = __0.getValue(); for (B b : set)
l.add(pair(a, b)); }
return l;
}}
Object mutex() { return data; }
public String toString() { return "mm" + str(data); }
Pair firstEntry() { synchronized(data) {
if (empty(data)) return null;
Map.Entry> entry = data.entrySet().iterator().next();
return pair(entry.getKey(), first(entry.getValue()));
}}
A firstKey() { synchronized(data) { return main.firstKey(data); }}
A lastKey() { synchronized(data) { return (A) ((NavigableMap) data).lastKey(); }}
A higherKey(Object a) { synchronized(data) { return (A) ((NavigableMap) data).higherKey(a); }}
}
static class MinimalChain implements Iterable {
A element;
MinimalChain next;
MinimalChain() {}
MinimalChain(A element) {
this.element = element;}
MinimalChain(A element, MinimalChain next) {
this.next = next;
this.element = element;}
public String toString() { return str(toList()); }
ArrayList toList() {
ArrayList l = new ArrayList();
MinimalChain c = this;
while (c != null) {
l.add(c.element);
c = c.next;
}
return l;
}
void setElement(A a) { element = a; }
void setNext(MinimalChain next) { this.next = next; }
// TODO: optimize
public Iterator iterator() { return toList().iterator(); }
A get() { return element; }
}
static class RestartableCountdown implements AutoCloseable {
java.util.Timer timer;
long targetTime; // in sys time
long /*firings,*/ totalSleepTime; // stats
synchronized void setTargetTime(long targetTime, Runnable action) {
if (targetTime <= 0)
stop();
else if (targetTime != this.targetTime) {
start(targetTime-sysNow(), action);
this.targetTime = targetTime;
}
}
// stops the countdown and restarts it
synchronized void start(long delayMS, Object action) {
stop();
if (delayMS <= 0)
{ startThread(new Runnable() { public void run() { try { callF(action);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(action);"; }}); }
else {
totalSleepTime += delayMS;
timer = doLater_daemon(delayMS, action);
targetTime = sysNow()+delayMS;
}
}
void start(double delaySeconds, Object action) {
start(toMS(delaySeconds), action);
}
synchronized void stop() {
cancelTimer(timer);
timer = null;
targetTime = 0;
}
public void close() { stop(); }
}
static class Seconds implements Comparable , IFieldsToList{
double seconds;
Seconds() {}
Seconds(double seconds) {
this.seconds = seconds;}
public boolean equals(Object o) {
if (!(o instanceof Seconds)) return false;
Seconds __1 = (Seconds) o;
return seconds == __1.seconds;
}
public int hashCode() {
int h = -660217249;
h = boostHashCombine(h, _hashCode(seconds));
return h;
}
public Object[] _fieldsToList() { return new Object[] {seconds}; }
final double get(){ return seconds(); }
final double getDouble(){ return seconds(); }
double seconds() { return seconds; }
public String toString() { return formatDouble(seconds, 3) + " s"; }
public int compareTo(Seconds s) {
return cmp(seconds, s.seconds);
}
Seconds div(double x) { return new Seconds(get()/x); }
Seconds minus(Seconds x) { return new Seconds(get()-x.get()); }
}
/*sinterface ISleeper extends ISleeper_v2, AutoCloseable {
void doLater(long targetSysTime, Runnable r); // call only once
}*/
static Class> getClass(String name) {
return _getClass(name);
}
static Class getClass(Object o) {
return _getClass(o);
}
static Class getClass(Object realm, String name) {
return _getClass(realm, name);
}
static boolean classIsExportedTo(Class c, java.lang.Module destModule) {
if (c == null || destModule == null) return false;
java.lang.Module srcModule = c.getModule();
String packageName = c.getPackageName();
return srcModule.isExported(packageName, destModule);
}
static boolean isAbstract(Class c) {
return (c.getModifiers() & Modifier.ABSTRACT) != 0;
}
static boolean isAbstract(Method m) {
return (m.getModifiers() & Modifier.ABSTRACT) != 0;
}
static boolean reflection_isForbiddenMethod(Method m) {
return m.getDeclaringClass() == Object.class
&& eqOneOf(m.getName(), "finalize", "clone", "registerNatives");
}
static Set allInterfacesImplementedBy(Object o) {
return allInterfacesImplementedBy(_getClass(o));
}
static Set allInterfacesImplementedBy(Class c) {
if (c == null) return null;
HashSet set = new HashSet();
allInterfacesImplementedBy_find(c, set);
return set;
}
static void allInterfacesImplementedBy_find(Class c, Set set) {
if (c.isInterface() && !set.add(c)) return;
do {
for (Class intf : c.getInterfaces())
allInterfacesImplementedBy_find(intf, set);
} while ((c = c.getSuperclass()) != null);
}
static Method findMethod(Object o, String method, Object... args) {
return findMethod_cached(o, method, args);
}
static boolean findMethod_checkArgs(Method m, Object[] args, boolean debug) {
Class>[] types = m.getParameterTypes();
if (types.length != args.length) {
if (debug)
System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
return false;
}
for (int i = 0; i < types.length; i++)
if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
if (debug)
System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
return false;
}
return true;
}
static Method findStaticMethod(Class c, String method, Object... args) {
Class _c = c;
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (!m.getName().equals(method))
continue;
if ((m.getModifiers() & Modifier.STATIC) == 0 || !findStaticMethod_checkArgs(m, args))
continue;
return m;
}
c = c.getSuperclass();
}
return null;
}
static boolean findStaticMethod_checkArgs(Method m, Object[] args) {
Class>[] types = m.getParameterTypes();
if (types.length != args.length)
return false;
for (int i = 0; i < types.length; i++)
if (!(args[i] == null || isInstanceX(types[i], args[i])))
return false;
return true;
}
static String unquote(String s) {
if (s == null) return null;
if (startsWith(s, '[')) {
int i = 1;
while (i < s.length() && s.charAt(i) == '=') ++i;
if (i < s.length() && s.charAt(i) == '[') {
String m = s.substring(1, i);
if (s.endsWith("]" + m + "]"))
return s.substring(i+1, s.length()-i-1);
}
}
return unquoteSingleOrDoubleQuotes(s);
}
static List quoteAll(String[] l) {
return quoteAll(asList(l));
}
static List