1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.concurrent.ThreadLocalRandom;
  4. public class DataValidator {
  5. /**
  6. * Replaces values in a dataset with retry logic for validation checks.
  7. *
  8. * @param dataset The dataset to validate (represented as a Map).
  9. * @param validationFunction The function to apply for validation.
  10. * @param maxRetries The maximum number of retry attempts.
  11. * @return The modified dataset with potentially retried values.
  12. */
  13. public static Map<String, Object> validateWithRetry(Map<String, Object> dataset, ValidationFunction validationFunction, int maxRetries) {
  14. Map<String, Object> modifiedDataset = new HashMap<>(dataset); // Create a copy to avoid modifying the original
  15. for (Map.Entry<String, Object> entry : modifiedDataset.entrySet()) {
  16. String key = entry.getKey();
  17. Object value = entry.getValue();
  18. int retryCount = 0;
  19. boolean isValid = false;
  20. while (retryCount < maxRetries && !isValid) {
  21. try {
  22. if (validationFunction.validate(value)) {
  23. isValid = true;
  24. } else {
  25. retryCount++;
  26. // Simulate some delay or random modification if validation fails.
  27. if(retryCount > 1){
  28. value = modifyValue(value); //modify the value for further retry
  29. }
  30. }
  31. } catch (Exception e) {
  32. // Handle exceptions during validation (e.g., network errors).
  33. retryCount++;
  34. if(retryCount > 1){
  35. value = modifyValue(value);
  36. }
  37. // Log the error or handle appropriately.
  38. }
  39. }
  40. modifiedDataset.put(key, value); // Put back the value, even if not valid after max retries
  41. }
  42. return modifiedDataset;
  43. }
  44. /**
  45. * Simulates modifying a value for retry purposes. Can be customized.
  46. * @param value the value to modify
  47. * @return the modified value
  48. */
  49. private static Object modifyValue(Object value) {
  50. if (value instanceof Integer) {
  51. return ThreadLocalRandom.current().nextInt(0, 101); // Random integer between 0 and 100
  52. } else if (value instanceof String) {
  53. return "retry_" + value.toString();
  54. }
  55. return value; // Return original if type not handled
  56. }
  57. /**
  58. * Functional interface for validation logic.
  59. */
  60. public interface ValidationFunction {
  61. boolean validate(Object value);
  62. }
  63. public static void main(String[] args) {
  64. // Example Usage
  65. Map<String, Object> data = new HashMap<>();
  66. data.put("age", 30);
  67. data.put("email", "test@example.com");
  68. data.put("score", "abc"); //invalid score
  69. data.put("value",123);
  70. ValidationFunction ageValidator = value -> {
  71. if (value instanceof Integer) {
  72. return ((Integer) value) >= 0 && (Integer) value <= 120;
  73. }
  74. return false;
  75. };
  76. ValidationFunction emailValidator = value -> {
  77. if (value instanceof String) {
  78. return value.matches("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$");
  79. }
  80. return false;
  81. };
  82. Map<String, Object> validatedData = validateWithRetry(data, ageValidator, 3);
  83. System.out.println(validatedData);
  84. validatedData = validateWithRetry(data, emailValidator, 2);
  85. System.out.println(validatedData);
  86. }
  87. }

Add your comment