Skip to content

Commit

Permalink
Introduced package private class StreamUtils
Browse files Browse the repository at this point in the history
```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
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

package org.junit.platform.commons.util;

import org.apiguardian.api.API;

import static java.util.Spliterator.ORDERED;
import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.stream.Collectors.collectingAndThen;
Expand All @@ -18,7 +20,6 @@
import static org.apiguardian.api.API.Status.INTERNAL;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -27,7 +28,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Optional;
import java.util.Set;
import java.util.Spliterator;
import java.util.function.Consumer;
Expand All @@ -37,8 +37,6 @@
import java.util.stream.LongStream;
import java.util.stream.Stream;

import org.apiguardian.api.API;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.PreconditionViolationException;

/**
Expand Down Expand Up @@ -215,67 +213,7 @@ public static Stream<?> toStream(Object object) {
if (object.getClass().isArray() && object.getClass().getComponentType().isPrimitive()) {
return IntStream.range(0, Array.getLength(object)).mapToObj(i -> Array.get(object, i));
}
return tryConvertToStreamByIterator(object);
}

private 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);
}
return StreamUtils.tryConvertToStreamByIterator(object);
}

/**
Expand Down
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);
}
}
}

0 comments on commit 21028c8

Please sign in to comment.