Switch to using predicate for testing existing value

This commit is contained in:
Isabel Drost-Fromm 2016-05-02 15:41:05 +02:00
parent 4cf5385e4d
commit 372eceb854
3 changed files with 10 additions and 8 deletions

View File

@ -30,6 +30,7 @@ import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
public class FieldSortBuilderTests extends AbstractSortTestCase<FieldSortBuilder> {

View File

@ -60,7 +60,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
exceptThis.add(SortMode.SUM);
exceptThis.add(SortMode.AVG);
exceptThis.add(SortMode.MEDIAN);
builder.sortMode(ESTestCase.randomValueOtherThanMany(exceptThis, () -> randomFrom(SortMode.values())));
builder.sortMode(ESTestCase.randomValueOtherThanMany(exceptThis::contains, () -> randomFrom(SortMode.values())));
}
}
if (randomBoolean()) {

View File

@ -87,6 +87,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import static org.elasticsearch.common.util.CollectionUtils.arrayAsArrayList;
@ -410,21 +411,21 @@ public abstract class ESTestCase extends LuceneTestCase {
* helper to get a random value in a certain range that's different from the input
*/
public static <T> T randomValueOtherThan(T input, Supplier<T> randomSupplier) {
T randomValue = null;
do {
randomValue = randomSupplier.get();
} while (randomValue.equals(input));
return randomValue;
if (input != null) {
return randomValueOtherThanMany(input::equals, randomSupplier);
}
return(randomSupplier.get());
}
/**
* helper to get a random value in a certain range that's different from the input
*/
public static <T> T randomValueOtherThanMany(Collection<T> input, Supplier<T> randomSupplier) {
public static <T> T randomValueOtherThanMany(Predicate<T> input, Supplier<T> randomSupplier) {
T randomValue = null;
do {
randomValue = randomSupplier.get();
} while (input.contains(randomValue));
} while (input.test(randomValue));
return randomValue;
}