import java.util.List;
public class ListIntegrityChecker {
/**
* Checks if a list's size is within the defined limits.
* @param list The list to check.
* @param minLimit The minimum acceptable size.
* @param maxLimit The maximum acceptable size.
* @return True if the list size is within the limits, false otherwise.
*/
public static boolean verifyListSize(List<?> list, int minLimit, int maxLimit) {
if (list == null) {
System.err.println("List is null. Integrity check failed.");
return false; // Or throw an exception, depending on desired behavior
}
int size = list.size();
return size >= minLimit && size <= maxLimit;
}
/**
* Checks if a list contains at least a certain number of elements.
* @param list The list to check.
* @param minCount The minimum number of elements required.
* @return True if the list contains at least minCount elements, false otherwise.
*/
public static boolean verifyListContainsElements(List<?> list, int minCount) {
if (list == null) {
System.err.println("List is null. Integrity check failed.");
return false;
}
return list.size() >= minCount;
}
/**
* Checks if a list contains at most a certain number of elements.
* @param list The list to check.
* @param maxCount The maximum number of elements allowed.
* @return True if the list contains at most maxCount elements, false otherwise.
*/
public static boolean verifyListContainsAtMostElements(List<?> list, int maxCount) {
if (list == null) {
System.err.println("List is null. Integrity check failed.");
return false;
}
return list.size() <= maxCount;
}
public static void main(String[] args) {
// Example usage
List<String> myList = List.of("item1", "item2", "item3");
// Check size limits
boolean sizeValid = verifyListSize(myList, 2, 4);
System.out.println("Size Valid: " + sizeValid); // Expected: true
boolean sizeInvalid = verifyListSize(myList, 0, 1);
System.out.println("Size Valid: " + sizeInvalid); // Expected: false
//Check elements count
boolean elementCountValid = verifyListContainsElements(myList, 2);
System.out.println("Element count Valid: " + elementCountValid); // Expected: true
boolean elementCountInvalid = verifyListContainsElements(myList, 5);
System.out.println("Element count Valid: " + elementCountInvalid); //Expected: false
}
}
Add your comment