import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
class CollectionConstraintChecker {
/**
* Checks if a collection satisfies specified constraints.
*
* @param collection The collection to check.
* @param constraintFunction A function that takes a Collection and returns true if it satisfies the constraint, false otherwise.
* @param errorMessage The message to display if the constraint is not met.
* @param <T> The type of elements in the collection.
* @throws IllegalArgumentException if the collection is null or the constraint function is null.
*/
public static <T> void checkCollectionConstraint(Collection<T> collection,
ConstraintFunction<T> constraintFunction,
String errorMessage) {
if (collection == null) {
throw new IllegalArgumentException("Collection cannot be null.");
}
if (constraintFunction == null) {
throw new IllegalArgumentException("Constraint function cannot be null.");
}
if (!constraintFunction.isValid(collection)) {
System.err.println("Collection constraint failed: " + errorMessage);
throw new IllegalArgumentException(errorMessage); // Or handle the failure appropriately
}
}
/**
* Functional interface representing a constraint function for collections.
* @param <T> The type of elements in the collection.
*/
public interface ConstraintFunction<T> {
boolean isValid(Collection<T> collection);
}
public static void main(String[] args) {
// Example usage: Check if a list contains only positive numbers.
ConstraintFunction<Integer> positiveNumbersOnly = collection -> {
for (Integer num : collection) {
if (num <= 0) {
return false;
}
}
return true;
};
List<Integer> numbers1 = new ArrayList<>();
numbers1.add(1);
numbers1.add(2);
numbers1.add(3);
List<Integer> numbers2 = new ArrayList<>();
numbers2.add(1);
numbers2.add(-2);
numbers2.add(3);
try {
checkCollectionConstraint(numbers1, positiveNumbersOnly, "List must contain only positive numbers.");
System.out.println("numbers1 passed constraint.");
} catch (IllegalArgumentException e) {
System.err.println("numbers1 failed constraint: " + e.getMessage());
}
try {
checkCollectionConstraint(numbers2, positiveNumbersOnly, "List must contain only positive numbers.");
System.out.println("numbers2 passed constraint.");
} catch (IllegalArgumentException e) {
System.err.println("numbers2 failed constraint: " + e.getMessage());
}
//Example with string collection
ConstraintFunction<String> nonBlankStrings = collection -> {
for(String str : collection){
if(str == null || str.trim().isEmpty()){
return false;
}
}
return true;
};
List<String> strings1 = new ArrayList<>();
strings1.add("hello");
strings1.add("world");
List<String> strings2 = new ArrayList<>();
strings2.add("hello");
strings2.add("");
try {
checkCollectionConstraint(strings1, nonBlankStrings, "List must contain non-blank strings");
System.out.println("strings1 passed constraint");
} catch (IllegalArgumentException e) {
System.err.println("strings1 failed constraint: " + e.getMessage());
}
try {
checkCollectionConstraint(strings2, nonBlankStrings, "List must contain non-blank strings");
System.out.println("strings2 passed constraint");
} catch (IllegalArgumentException e) {
System.err.println("strings2 failed constraint: " + e.getMessage());
}
}
}
Add your comment