1. import java.util.ArrayList;
  2. import java.util.Collection;
  3. import java.util.List;
  4. class CollectionConstraintChecker {
  5. /**
  6. * Checks if a collection satisfies specified constraints.
  7. *
  8. * @param collection The collection to check.
  9. * @param constraintFunction A function that takes a Collection and returns true if it satisfies the constraint, false otherwise.
  10. * @param errorMessage The message to display if the constraint is not met.
  11. * @param <T> The type of elements in the collection.
  12. * @throws IllegalArgumentException if the collection is null or the constraint function is null.
  13. */
  14. public static <T> void checkCollectionConstraint(Collection<T> collection,
  15. ConstraintFunction<T> constraintFunction,
  16. String errorMessage) {
  17. if (collection == null) {
  18. throw new IllegalArgumentException("Collection cannot be null.");
  19. }
  20. if (constraintFunction == null) {
  21. throw new IllegalArgumentException("Constraint function cannot be null.");
  22. }
  23. if (!constraintFunction.isValid(collection)) {
  24. System.err.println("Collection constraint failed: " + errorMessage);
  25. throw new IllegalArgumentException(errorMessage); // Or handle the failure appropriately
  26. }
  27. }
  28. /**
  29. * Functional interface representing a constraint function for collections.
  30. * @param <T> The type of elements in the collection.
  31. */
  32. public interface ConstraintFunction<T> {
  33. boolean isValid(Collection<T> collection);
  34. }
  35. public static void main(String[] args) {
  36. // Example usage: Check if a list contains only positive numbers.
  37. ConstraintFunction<Integer> positiveNumbersOnly = collection -> {
  38. for (Integer num : collection) {
  39. if (num <= 0) {
  40. return false;
  41. }
  42. }
  43. return true;
  44. };
  45. List<Integer> numbers1 = new ArrayList<>();
  46. numbers1.add(1);
  47. numbers1.add(2);
  48. numbers1.add(3);
  49. List<Integer> numbers2 = new ArrayList<>();
  50. numbers2.add(1);
  51. numbers2.add(-2);
  52. numbers2.add(3);
  53. try {
  54. checkCollectionConstraint(numbers1, positiveNumbersOnly, "List must contain only positive numbers.");
  55. System.out.println("numbers1 passed constraint.");
  56. } catch (IllegalArgumentException e) {
  57. System.err.println("numbers1 failed constraint: " + e.getMessage());
  58. }
  59. try {
  60. checkCollectionConstraint(numbers2, positiveNumbersOnly, "List must contain only positive numbers.");
  61. System.out.println("numbers2 passed constraint.");
  62. } catch (IllegalArgumentException e) {
  63. System.err.println("numbers2 failed constraint: " + e.getMessage());
  64. }
  65. //Example with string collection
  66. ConstraintFunction<String> nonBlankStrings = collection -> {
  67. for(String str : collection){
  68. if(str == null || str.trim().isEmpty()){
  69. return false;
  70. }
  71. }
  72. return true;
  73. };
  74. List<String> strings1 = new ArrayList<>();
  75. strings1.add("hello");
  76. strings1.add("world");
  77. List<String> strings2 = new ArrayList<>();
  78. strings2.add("hello");
  79. strings2.add("");
  80. try {
  81. checkCollectionConstraint(strings1, nonBlankStrings, "List must contain non-blank strings");
  82. System.out.println("strings1 passed constraint");
  83. } catch (IllegalArgumentException e) {
  84. System.err.println("strings1 failed constraint: " + e.getMessage());
  85. }
  86. try {
  87. checkCollectionConstraint(strings2, nonBlankStrings, "List must contain non-blank strings");
  88. System.out.println("strings2 passed constraint");
  89. } catch (IllegalArgumentException e) {
  90. System.err.println("strings2 failed constraint: " + e.getMessage());
  91. }
  92. }
  93. }

Add your comment