1. import java.util.List;
  2. public class ListValidator {
  3. /**
  4. * Validates a list for basic sanity checks.
  5. *
  6. * @param list The list to validate.
  7. * @param <T> The type of elements in the list.
  8. * @return True if the list is valid, false otherwise.
  9. */
  10. public static <T> boolean validateList(List<T> list) {
  11. if (list == null) {
  12. System.err.println("List is null.");
  13. return false;
  14. }
  15. if (list.isEmpty()) {
  16. System.err.println("List is empty.");
  17. return true; // Empty lists are considered valid by default
  18. }
  19. for (T element : list) {
  20. if (element == null) {
  21. System.err.println("List contains a null element.");
  22. return false;
  23. }
  24. }
  25. return true;
  26. }
  27. /**
  28. * Validates if all elements in a list are of the same type.
  29. * @param list The list to validate
  30. * @param <T> The type of the list elements
  31. * @return True if all elements are of the same type, false otherwise.
  32. */
  33. public static <T> boolean validateSameType(List<T> list) {
  34. if (list == null || list.isEmpty()) {
  35. return true; // Consider null or empty lists as valid
  36. }
  37. T firstElement = list.get(0);
  38. if (firstElement == null) {
  39. System.err.println("List contains a null element.");
  40. return false;
  41. }
  42. for (T element : list) {
  43. if (element == null) {
  44. System.err.println("List contains a null element.");
  45. return false;
  46. }
  47. if (!firstElement.getClass().equals(element.getClass())) {
  48. System.err.println("List contains elements of different types.");
  49. return false;
  50. }
  51. }
  52. return true;
  53. }
  54. /**
  55. * Validates if the list contains only positive numbers.
  56. * @param list The list to validate
  57. * @param <T> The type of list elements (must be Number)
  58. * @return True if all elements are positive numbers, false otherwise.
  59. */
  60. public static <T extends Number> boolean validatePositiveNumbers(List<T> list) {
  61. if (list == null) {
  62. System.err.println("List is null.");
  63. return false;
  64. }
  65. for (T element : list) {
  66. if (element == null) {
  67. System.err.println("List contains a null element.");
  68. return false;
  69. }
  70. if (element.doubleValue() <= 0) {
  71. System.err.println("List contains non-positive numbers.");
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77. public static void main(String[] args) {
  78. // Example usage
  79. List<String> stringList = List.of("apple", "banana", "cherry");
  80. System.out.println("String list is valid: " + validateList(stringList));
  81. List<Integer> intList = List.of(1, 2, 3, 4, 5);
  82. System.out.println("Integer list is valid: " + validateList(intList));
  83. List<Object> mixedList = List.of(1, "hello", 3.14);
  84. System.out.println("Mixed list is valid: " + validateList(mixedList));
  85. List<String> nullList = null;
  86. System.out.println("Null list is valid: " + validateList(nullList));
  87. List<String> emptyList = List.of();
  88. System.out.println("Empty list is valid: " + validateList(emptyList));
  89. List<Integer> sameType = List.of(1, 2, 3, 4, 5);
  90. System.out.println("Same Type: " + validateSameType(sameType));
  91. List<Object> differentType = List.of(1, "hello", 3.14);
  92. System.out.println("Different Type: " + validateSameType(differentType));
  93. List<Double> positiveNumbers = List.of(1.0, 2.5, 3.0

Add your comment