All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jqwik.engine.properties.arbitraries.exhaustive.IgnoreExceptionExhaustiveGenerator Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
package net.jqwik.engine.properties.arbitraries.exhaustive;

import java.util.*;

import net.jqwik.api.*;
import net.jqwik.engine.properties.*;

public class IgnoreExceptionExhaustiveGenerator implements ExhaustiveGenerator {
	private final ExhaustiveGenerator toFilter;
	private final Class exceptionType;

	public IgnoreExceptionExhaustiveGenerator(ExhaustiveGenerator toFilter, Class exceptionType) {
		this.toFilter = toFilter;
		this.exceptionType = exceptionType;
	}

	@Override
	public long maxCount() {
		return toFilter.maxCount();
	}

	@Override
	public Iterator iterator() {
		final Iterator mappedIterator = toFilter.iterator();
		return new Iterator() {

			T next = findNext();

			@Override
			public boolean hasNext() {
				return next != null;
			}

			@Override
			public T next() {
				if (next == null) {
					throw new NoSuchElementException();
				}
				T result = next;
				next = findNext();
				return result;
			}

			private T findNext() {
				return MaxTriesLoop.loop(
					() -> true,
					next -> {
						if (!mappedIterator.hasNext()) {
							return Tuple.of(true, null);
						}
						try {
							next = mappedIterator.next();
							return Tuple.of(true, next);
						} catch (Throwable throwable) {
							if (exceptionType.isAssignableFrom(throwable.getClass())) {
								return Tuple.of(false, next);
							}
							throw throwable;
						}
					},
					maxMisses -> {
						String message =
							String.format("Filter missed more than %s times.", maxMisses);
						return new TooManyFilterMissesException(message);
					},
                        10000);
			}

		};
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy