1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class URLValidator {
  4. /**
  5. * Validates a list of URLs against predefined constraints.
  6. *
  7. * @param urls A list of URLs to validate.
  8. * @return A list of validation results, where each result indicates whether a URL is valid
  9. * and provides a default value if a constraint is violated.
  10. */
  11. public static List<ValidationResult> validateURLs(List<String> urls) {
  12. List<ValidationResult> results = new ArrayList<>();
  13. for (String url : urls) {
  14. ValidationResult result = validateURL(url);
  15. results.add(result);
  16. }
  17. return results;
  18. }
  19. private static ValidationResult validateURL(String url) {
  20. // Default values for constraints
  21. String defaultTitle = "Unknown Title";
  22. String defaultDescription = "No description provided";
  23. String defaultPriority = "Medium";
  24. // Basic URL format check
  25. if (url == null || url.trim().isEmpty()) {
  26. return new ValidationResult(false, "URL cannot be null or empty.");
  27. }
  28. //Check for valid URL format (very basic) - can be improved with regex
  29. if (!url.contains("http://") && !url.contains("https://")) {
  30. return new ValidationResult(false, "URL must start with http:// or https://");
  31. }
  32. //Title constraint
  33. if(url.contains("task/")){
  34. String title = extractTitle(url);
  35. if(title == null || title.trim().isEmpty()){
  36. title = defaultTitle;
  37. }
  38. } else {
  39. title = defaultTitle;
  40. }
  41. //Description constraint
  42. if (url.contains("description=")){
  43. String description = extractDescription(url);
  44. if(description == null || description.trim().isEmpty()){
  45. description = defaultDescription;
  46. }
  47. } else {
  48. description = defaultDescription;
  49. }
  50. //Priority constraint
  51. if(url.contains("priority=")){
  52. String priority = extractPriority(url);
  53. if(priority == null || priority.trim().isEmpty()){
  54. priority = defaultPriority;
  55. }
  56. } else {
  57. priority = defaultPriority;
  58. }
  59. return new ValidationResult(true, "URL is valid. Title: " + title + ", Description: " + description + ", Priority: " + priority);
  60. }
  61. private static String extractTitle(String url) {
  62. int startIndex = url.indexOf("task/");
  63. if (startIndex != -1) {
  64. startIndex += "task/".length();
  65. int endIndex = url.indexOf("/", startIndex);
  66. if (endIndex != -1) {
  67. return url.substring(startIndex, endIndex);
  68. }
  69. }
  70. return null;
  71. }
  72. private static String extractDescription(String url) {
  73. int startIndex = url.indexOf("description=");
  74. if (startIndex != -1) {
  75. startIndex += "description=".length();
  76. int endIndex = url.indexOf("&", startIndex);
  77. if (endIndex == -1) {
  78. endIndex = url.length();
  79. }
  80. return url.substring(startIndex, endIndex).trim();
  81. }
  82. return null;
  83. }
  84. private static String extractPriority(String url) {
  85. int startIndex = url.indexOf("priority=");
  86. if (startIndex != -1) {
  87. startIndex += "priority=".length();
  88. int endIndex = url.indexOf("&", startIndex);
  89. if (endIndex == -1) {
  90. endIndex = url.length();
  91. }
  92. return url.substring(startIndex, endIndex).trim();
  93. }
  94. return null;
  95. }
  96. // Helper class to store validation results
  97. public static class ValidationResult {
  98. private final boolean isValid;
  99. private final String message;
  100. public ValidationResult(boolean isValid, String message) {
  101. this.isValid = isValid;
  102. this.message = message;
  103. }
  104. public boolean isValid() {
  105. return isValid;
  106. }
  107. public String getMessage() {
  108. return message;
  109. }
  110. }
  111. public static void main(String[] args) {
  112. List<String> urls = new ArrayList<>();
  113. urls.add("https://example.com/task/my-task");
  114. urls.add("http://example.com/another

Add your comment