1. import java.util.List;
  2. public class ListIntegrityChecker {
  3. /**
  4. * Checks if a list's size is within the defined limits.
  5. * @param list The list to check.
  6. * @param minLimit The minimum acceptable size.
  7. * @param maxLimit The maximum acceptable size.
  8. * @return True if the list size is within the limits, false otherwise.
  9. */
  10. public static boolean verifyListSize(List<?> list, int minLimit, int maxLimit) {
  11. if (list == null) {
  12. System.err.println("List is null. Integrity check failed.");
  13. return false; // Or throw an exception, depending on desired behavior
  14. }
  15. int size = list.size();
  16. return size >= minLimit && size <= maxLimit;
  17. }
  18. /**
  19. * Checks if a list contains at least a certain number of elements.
  20. * @param list The list to check.
  21. * @param minCount The minimum number of elements required.
  22. * @return True if the list contains at least minCount elements, false otherwise.
  23. */
  24. public static boolean verifyListContainsElements(List<?> list, int minCount) {
  25. if (list == null) {
  26. System.err.println("List is null. Integrity check failed.");
  27. return false;
  28. }
  29. return list.size() >= minCount;
  30. }
  31. /**
  32. * Checks if a list contains at most a certain number of elements.
  33. * @param list The list to check.
  34. * @param maxCount The maximum number of elements allowed.
  35. * @return True if the list contains at most maxCount elements, false otherwise.
  36. */
  37. public static boolean verifyListContainsAtMostElements(List<?> list, int maxCount) {
  38. if (list == null) {
  39. System.err.println("List is null. Integrity check failed.");
  40. return false;
  41. }
  42. return list.size() <= maxCount;
  43. }
  44. public static void main(String[] args) {
  45. // Example usage
  46. List<String> myList = List.of("item1", "item2", "item3");
  47. // Check size limits
  48. boolean sizeValid = verifyListSize(myList, 2, 4);
  49. System.out.println("Size Valid: " + sizeValid); // Expected: true
  50. boolean sizeInvalid = verifyListSize(myList, 0, 1);
  51. System.out.println("Size Valid: " + sizeInvalid); // Expected: false
  52. //Check elements count
  53. boolean elementCountValid = verifyListContainsElements(myList, 2);
  54. System.out.println("Element count Valid: " + elementCountValid); // Expected: true
  55. boolean elementCountInvalid = verifyListContainsElements(myList, 5);
  56. System.out.println("Element count Valid: " + elementCountInvalid); //Expected: false
  57. }
  58. }

Add your comment