import java.util.HashMap;
import java.util.Map;
public class DataNormalizer {
private static final String DRY_RUN_FLAG = "dry_run";
private static boolean isDryRun = false;
public DataNormalizer(String dryRunFlag) {
this.isDryRun = dryRunFlag != null && dryRunFlag.equalsIgnoreCase(DRY_RUN_FLAG);
}
/**
* Normalizes a single data entry.
* @param entry The data entry to normalize.
* @return The normalized data entry, or the original entry if dry-run mode is enabled.
*/
public Object normalize(Object entry) {
if (isDryRun) {
System.out.println("DRY RUN: Would normalize: " + entry); // Log what *would* be normalized
return entry;
}
if (entry == null) {
return null; // Handle null entries.
}
if (entry instanceof String) {
String str = (String) entry;
return str.trim().toLowerCase(); // Trim whitespace and convert to lowercase.
} else if (entry instanceof Integer) {
Integer num = (Integer) entry;
return num; // No normalization needed for integers in this simplified example.
} else if (entry instanceof Double) {
Double dbl = (Double) entry;
return dbl; // No normalization needed for doubles in this simplified example.
} else if (entry instanceof Map) {
Map<Object, Object> map = (Map<Object, Object>) entry;
Map<Object, Object> normalizedMap = new HashMap<>();
for (Map.Entry<Object, Object> entry2 : map.entrySet()) {
Object key = entry2.getKey();
Object value = entry2.getValue();
Object normalizedKey = normalize(key);
Object normalizedValue = normalize(value);
normalizedMap.put(normalizedKey, normalizedValue);
}
return normalizedMap;
} else {
// For other types, return the original value if dry-run is enabled.
if (!isDryRun) {
System.out.println("Warning: Unhandled data type. Returning original: " + entry);
}
return entry;
}
}
}
Add your comment