// collects into l until max is reached or target element is found static Collector limitedSearchCollector(final Collection l, final int max, final A target) { ret new Collector() { bool found; bool full() { ret found || l(l) >= max; } void add(A a) { if (!full()) l.add(a); if (eq(a, target)) found = true; } bool contains(A a) { ret l.contains(a); } }; }