-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced package private class StreamUtils
```markdown --- I hereby agree to the terms of the JUnit Contributor License Agreement. ```
- Loading branch information
Hans Zuidervaart
committed
Jul 2, 2023
1 parent
9b8ec45
commit 21028c8
Showing
2 changed files
with
85 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
junit-platform-commons/src/main/java/org/junit/platform/commons/util/StreamUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.junit.platform.commons.util; | ||
|
||
import org.junit.platform.commons.JUnitException; | ||
import org.junit.platform.commons.PreconditionViolationException; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.Optional; | ||
import java.util.Spliterator; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.Spliterator.ORDERED; | ||
import static java.util.Spliterators.spliteratorUnknownSize; | ||
import static java.util.stream.StreamSupport.stream; | ||
|
||
final class StreamUtils { | ||
|
||
private StreamUtils() { | ||
} | ||
|
||
static Stream<?> tryConvertToStreamByIterator(Object object) { | ||
Class<?> theClass = object.getClass(); | ||
try { | ||
Method method = theClass.getMethod("iterator"); | ||
return stream(() -> tryIteratorToSpliterator(object, method), ORDERED, false); | ||
} catch (NoSuchMethodException e) { | ||
return tryConvertToStreamBySpliterator(object); | ||
} | ||
} | ||
|
||
private static Stream<?> tryConvertToStreamBySpliterator(Object object) { | ||
try { | ||
Method method = object.getClass().getMethod("spliterator"); | ||
return stream(() -> tryInvokeSpliterator(object, method), ORDERED, false); | ||
} catch (NoSuchMethodException ex) { | ||
return tryConvertByIteratorSpliteratorReturnType(object); | ||
} | ||
} | ||
|
||
private static Stream<?> tryConvertByIteratorSpliteratorReturnType(Object object) { | ||
Optional<? extends Stream<?>> stream = streamFromSpliteratorSupplier(object); | ||
// is necessary because orElseGet gives generics compile errors | ||
return stream.isPresent() ? stream.get() : streamFromIteratorSupplier(object) | ||
.orElseThrow(() -> new PreconditionViolationException( | ||
"Cannot convert instance of " + object.getClass().getName() + " into a Stream: " + object)); | ||
|
||
} | ||
|
||
private static Optional<? extends Stream<?>> streamFromSpliteratorSupplier(Object object) { | ||
Method[] methods = object.getClass().getMethods(); | ||
return Arrays.stream(methods) | ||
.filter(m -> m.getReturnType() == Spliterator.class) | ||
.findFirst() | ||
.map(m -> stream(() -> tryInvokeSpliterator(object, m), ORDERED, false)); | ||
} | ||
|
||
private static Optional<Stream<?>> streamFromIteratorSupplier(Object object) { | ||
Class<?> theClass = object.getClass(); | ||
return Arrays.stream(theClass.getMethods()) | ||
.filter(m -> m.getReturnType() == Iterator.class) | ||
.findFirst() | ||
.map(m -> stream(() -> tryIteratorToSpliterator(object, m), ORDERED, false)); | ||
} | ||
|
||
private static Spliterator<?> tryInvokeSpliterator(Object object, Method method) { | ||
try { | ||
return (Spliterator<?>) method.invoke(object); | ||
} catch (IllegalAccessException | InvocationTargetException | ClassCastException ex) { | ||
throw new JUnitException("Cannot invoke method " + method.getName() + " onto " + object); | ||
} | ||
} | ||
|
||
private static Spliterator<?> tryIteratorToSpliterator(Object object, Method method) { | ||
try { | ||
return spliteratorUnknownSize((Iterator<?>) method.invoke(object), ORDERED); | ||
} catch (IllegalAccessException | InvocationTargetException | ClassCastException e) { | ||
throw new JUnitException("Cannot invoke method " + method.getName() + " onto " + object); | ||
} | ||
} | ||
} |