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

net.jqwik.engine.properties.FeatureExtractor Maven / Gradle / Ivy

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

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

@FunctionalInterface
public interface FeatureExtractor extends Function {

	static  FeatureExtractor identity() {
		return t -> t;
	}

	default Object applySafe(T t) {
		try {
			return apply(t);
		} catch (NullPointerException npe) {
			return null;
		}
	}

	default boolean isUniqueIn(T value, Collection elements) {
		Set elementFeatures = elements.stream().map(this::applySafe).collect(Collectors.toSet());
		return !elementFeatures.contains(this.applySafe(value));
	}

	default boolean areUnique(Collection elements) {
		Set elementFeatures = elements.stream().map(this::applySafe).collect(Collectors.toSet());
		return elementFeatures.size() == elements.size();
	}
}