public static <E> E getOnlyElement(Iterator<E> traversal, E defaultElement) { if (!traversal.hasNext()) return defaultElement; E result = traversal.next(); if (traversal.hasNext()) throw new IllegalArgumentException( "Traversal contains more than 1 element: " + result + ", " + traversal.next()); return result; }
public static void verifyElementOrder( Iterator<? extends Element> elements, String key, Order order, int expectedCount) { Comparable previous = null; int count = 0; while (elements.hasNext()) { Element element = elements.next(); Comparable current = element.value(key); if (previous != null) { int cmp = previous.compareTo(current); assertTrue( previous + " <> " + current + " @ " + count, order == Order.ASC ? cmp <= 0 : cmp >= 0); } previous = current; count++; } assertEquals(expectedCount, count); }