static void addToListWithMaxSize(L l, A a, int maxSize) { if (l == null || maxSize <= 0) ret; synchronized(collectionMutex(l)) { int n; while ((n = l.size()) >= maxSize) { l.remove(0); if (l.size() != n-1) fail("List remove failed!? " + l.size() + " / " + n + " / " + objectToStringWithClassName(l)); } l.add(a); } }