test/java/util/stream/bootlib/java/util/stream/LambdaTestHelpers.java

Print this page
rev 7573 : 8015315: Stream.concat methods
Contributed-by: brian.goetz@oracle.com, henry.jen@oracle.com


 343      * This ensures equality comparisons for test results do not trip
 344      * the boxing trip-wires.
 345      */
 346     public static<T> List<T> toBoxedList(Spliterator<T> sp) {
 347         List<T> l = new ArrayList<>();
 348         sp.forEachRemaining(toBoxingConsumer(l::add));
 349         return l;
 350     }
 351 
 352     /**
 353      * Convert an iterator to a multi-set, represented as a Map, using forEach with an implementation of
 354      * {@link java.util.stream.LambdaTestHelpers.OmnivorousConsumer}.
 355      *
 356      * This ensures equality comparisons for test results do not trip
 357      * the boxing trip-wires.
 358      */
 359     @SuppressWarnings("unchecked")
 360     private static<T> Map<T, Integer> toBoxedMultiset(Iterator<T> it) {
 361         Map<Object, Integer> result = new HashMap<>();
 362 
 363         it.forEachRemaining(new OmnivorousConsumer<T>() {
 364             @Override
 365             public void accept(T t) {
 366                 add(t);
 367             }
 368 
 369             @Override
 370             public void accept(int value) {
 371                 add(value);
 372             }
 373 
 374             @Override
 375             public void accept(long value) {
 376                 add(value);
 377             }
 378 
 379             @Override
 380             public void accept(double value) {
 381                 add(value);
 382             }
 383 
 384             void add(Object o) {
 385                 if (result.containsKey(o))
 386                     result.put(o, result.get(o) + 1);
 387                 else
 388                     result.put(o, 1);
 389             }
 390 
 391         });
 392 
 393         return (Map<T, Integer>) result;
 394     }
 395 
 396     @SuppressWarnings("unchecked")
 397     public static void assertContentsEqual(Object a, Object b) {
 398         if (a instanceof Iterable && b instanceof Iterable)
 399             assertContents((Iterable) a, (Iterable) b);
 400         else
 401             assertEquals(a, b);
 402     }
 403 
 404     public static<T> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) {
 405         assertContentsUnordered(actual.iterator(), expected.iterator());
 406     }
 407 
 408     public static<T> void assertContentsUnordered(Iterator<T> actual, Iterator<T> expected) {
 409         assertEquals(toBoxedMultiset(actual), toBoxedMultiset(expected));
 410     }
 411 




 343      * This ensures equality comparisons for test results do not trip
 344      * the boxing trip-wires.
 345      */
 346     public static<T> List<T> toBoxedList(Spliterator<T> sp) {
 347         List<T> l = new ArrayList<>();
 348         sp.forEachRemaining(toBoxingConsumer(l::add));
 349         return l;
 350     }
 351 
 352     /**
 353      * Convert an iterator to a multi-set, represented as a Map, using forEach with an implementation of
 354      * {@link java.util.stream.LambdaTestHelpers.OmnivorousConsumer}.
 355      *
 356      * This ensures equality comparisons for test results do not trip
 357      * the boxing trip-wires.
 358      */
 359     @SuppressWarnings("unchecked")
 360     private static<T> Map<T, Integer> toBoxedMultiset(Iterator<T> it) {
 361         Map<Object, Integer> result = new HashMap<>();
 362 
 363         it.forEachRemaining(toBoxingConsumer(o -> {
 364                 if (result.containsKey(o))
 365                     result.put(o, result.get(o) + 1);
 366                 else
 367                     result.put(o, 1);
 368             }));




 369 
 370         return (Map<T, Integer>) result;


 371     }
 372 
 373     @SuppressWarnings("unchecked")
 374     public static<T> Map<T, Integer> toBoxedMultiset(Spliterator<T> it) {
 375         Map<Object, Integer> result = new HashMap<>();

 376 
 377         it.forEachRemaining(toBoxingConsumer(o -> {
 378                 if (result.containsKey(o))
 379                     result.put(o, result.get(o) + 1);
 380                 else
 381                     result.put(o, 1);
 382             }));


 383 
 384         return (Map<T, Integer>) result;
 385     }
 386 
 387     @SuppressWarnings("unchecked")
 388     public static void assertContentsEqual(Object a, Object b) {
 389         if (a instanceof Iterable && b instanceof Iterable)
 390             assertContents((Iterable) a, (Iterable) b);
 391         else
 392             assertEquals(a, b);
 393     }
 394 
 395     public static<T> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) {
 396         assertContentsUnordered(actual.iterator(), expected.iterator());
 397     }
 398 
 399     public static<T> void assertContentsUnordered(Iterator<T> actual, Iterator<T> expected) {
 400         assertEquals(toBoxedMultiset(actual), toBoxedMultiset(expected));
 401     }
 402