1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class DataNormalizer {
  4. private static final String DRY_RUN_FLAG = "dry_run";
  5. private static boolean isDryRun = false;
  6. public DataNormalizer(String dryRunFlag) {
  7. this.isDryRun = dryRunFlag != null && dryRunFlag.equalsIgnoreCase(DRY_RUN_FLAG);
  8. }
  9. /**
  10. * Normalizes a single data entry.
  11. * @param entry The data entry to normalize.
  12. * @return The normalized data entry, or the original entry if dry-run mode is enabled.
  13. */
  14. public Object normalize(Object entry) {
  15. if (isDryRun) {
  16. System.out.println("DRY RUN: Would normalize: " + entry); // Log what *would* be normalized
  17. return entry;
  18. }
  19. if (entry == null) {
  20. return null; // Handle null entries.
  21. }
  22. if (entry instanceof String) {
  23. String str = (String) entry;
  24. return str.trim().toLowerCase(); // Trim whitespace and convert to lowercase.
  25. } else if (entry instanceof Integer) {
  26. Integer num = (Integer) entry;
  27. return num; // No normalization needed for integers in this simplified example.
  28. } else if (entry instanceof Double) {
  29. Double dbl = (Double) entry;
  30. return dbl; // No normalization needed for doubles in this simplified example.
  31. } else if (entry instanceof Map) {
  32. Map<Object, Object> map = (Map<Object, Object>) entry;
  33. Map<Object, Object> normalizedMap = new HashMap<>();
  34. for (Map.Entry<Object, Object> entry2 : map.entrySet()) {
  35. Object key = entry2.getKey();
  36. Object value = entry2.getValue();
  37. Object normalizedKey = normalize(key);
  38. Object normalizedValue = normalize(value);
  39. normalizedMap.put(normalizedKey, normalizedValue);
  40. }
  41. return normalizedMap;
  42. } else {
  43. // For other types, return the original value if dry-run is enabled.
  44. if (!isDryRun) {
  45. System.out.println("Warning: Unhandled data type. Returning original: " + entry);
  46. }
  47. return entry;
  48. }
  49. }
  50. }

Add your comment