1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. public class LogFlattener {
  6. /**
  7. * Flattens a nested log entry structure into a single-level map.
  8. * Handles potential null values gracefully.
  9. * @param nestedLog The nested log entry data.
  10. * @return A flattened map representing the log entry.
  11. */
  12. public static Map<String, String> flattenLogEntry(Object nestedLog) {
  13. Map<String, String> flattened = new HashMap<>();
  14. flattenHelper(nestedLog, "", flattened);
  15. return flattened;
  16. }
  17. private static void flattenHelper(Object obj, String prefix, Map<String, String> flattened) {
  18. if (obj == null) {
  19. return; // Handle null values
  20. }
  21. if (obj instanceof Map) {
  22. Map<?, ?> map = (Map<?, ?>) obj;
  23. for (Map.Entry<?, ?> entry : map.entrySet()) {
  24. String key = prefix + entry.getKey().toString();
  25. flattenHelper(entry.getValue(), key + ".", flattened);
  26. }
  27. } else if (obj instanceof List) {
  28. List<?> list = (List<?>) obj;
  29. for (int i = 0; i < list.size(); i++) {
  30. String key = prefix + i;
  31. flattenHelper(list.get(i), key + ".", flattened);
  32. }
  33. } else {
  34. String key = prefix;
  35. flattened.put(key, obj.toString());
  36. }
  37. }
  38. /**
  39. * Simulates a validation check with a fixed retry interval.
  40. * @param logEntry The flattened log entry.
  41. * @param retryCount The number of retry attempts.
  42. * @param retryIntervalMillis The interval between retry attempts in milliseconds.
  43. * @return True if the validation check passes, false otherwise.
  44. */
  45. public static boolean validateLogEntry(Map<String, String> logEntry, int retryCount, long retryIntervalMillis) throws InterruptedException {
  46. // Simulate a validation check. In a real scenario, this would involve
  47. // querying a database, checking a configuration, etc.
  48. if (logEntry.containsKey("status") && logEntry.get("status").equals("error")) {
  49. System.out.println("Validation failed: Status is error.");
  50. if (retryCount > 0) {
  51. System.out.println("Retrying in " + retryIntervalMillis + "ms...");
  52. Thread.sleep(retryIntervalMillis);
  53. return validateLogEntry(logEntry, retryCount - 1, retryIntervalMillis);
  54. } else {
  55. System.out.println("Validation failed after multiple retries.");
  56. return false;
  57. }
  58. } else {
  59. System.out.println("Validation passed.");
  60. return true;
  61. }
  62. }
  63. public static void main(String[] args) {
  64. // Example usage
  65. Object nestedLog = new HashMap<>();
  66. Map<String, String> innerMap = new HashMap<>();
  67. innerMap.put("code", "404");
  68. innerMap.put("message", "Resource not found");
  69. nestedLog.put("status", "error");
  70. nestedLog.put("details", innerMap);
  71. nestedLog.put("tags", new List<>(){
  72. List<String> list = new ArrayList<>();
  73. {
  74. list.add("error");
  75. list.add("resource");
  76. }
  77. });
  78. Map<String, String> flattenedLog = flattenLogEntry(nestedLog);
  79. System.out.println("Flattened Log Entry: " + flattenedLog);
  80. try {
  81. boolean isValid = validateLogEntry(flattenedLog, 3, 1000); // Retry 3 times with 1 second interval
  82. System.out.println("Log entry is valid: " + isValid);
  83. } catch (InterruptedException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. }

Add your comment