1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class UserDataNormalizer {
  4. /**
  5. * Normalizes user data, performs basic sanity checks, and returns a normalized map.
  6. * @param userData A map of user data.
  7. * @return A normalized map of user data or null if normalization fails.
  8. */
  9. public static Map<String, Object> normalizeUserData(Map<String, Object> userData) {
  10. if (userData == null || userData.isEmpty()) {
  11. return null; // Handle null or empty input
  12. }
  13. Map<String, Object> normalizedData = new HashMap<>();
  14. // Normalize and validate user ID
  15. Object userId = userData.get("userId");
  16. if (!(userId instanceof String)) {
  17. System.err.println("Error: userId must be a string.");
  18. return null;
  19. }
  20. normalizedData.put("userId", (String) userId);
  21. // Normalize and validate username
  22. Object username = userData.get("username");
  23. if (!(username instanceof String)) {
  24. System.err.println("Error: username must be a string.");
  25. return null;
  26. }
  27. normalizedData.put("username", (String) username);
  28. // Normalize and validate email
  29. Object email = userData.get("email");
  30. if (!(email instanceof String)) {
  31. System.err.println("Error: email must be a string.");
  32. return null;
  33. }
  34. String normalizedEmail = email.toString().trim().toLowerCase();
  35. if (!normalizedEmail.matches("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$")) {
  36. System.err.println("Error: Invalid email format.");
  37. return null;
  38. }
  39. normalizedData.put("email", normalizedEmail);
  40. // Normalize and validate age
  41. Object age = userData.get("age");
  42. if (!(age instanceof Integer)) {
  43. System.err.println("Error: age must be an integer.");
  44. return null;
  45. }
  46. int normalizedAge = Math.max(0, Math.min(150, (int) age)); // Ensure age is within reasonable bounds
  47. normalizedData.put("age", normalizedAge);
  48. // Normalize and validate registration date
  49. Object registrationDate = userData.get("registrationDate");
  50. if (!(registrationDate instanceof String)) {
  51. System.err.println("Error: registrationDate must be a string.");
  52. return null;
  53. }
  54. normalizedData.put("registrationDate", registrationDate);
  55. return normalizedData;
  56. }
  57. public static void main(String[] args) {
  58. // Example usage
  59. Map<String, Object> userData = new HashMap<>();
  60. userData.put("userId", "123");
  61. userData.put("username", "JohnDoe");
  62. userData.put("email", "john.doe@example.com");
  63. userData.put("age", 30);
  64. userData.put("registrationDate", "2023-01-15");
  65. Map<String, Object> normalizedData = normalizeUserData(userData);
  66. if (normalizedData != null) {
  67. System.out.println("Normalized User Data: " + normalizedData);
  68. } else {
  69. System.out.println("Normalization failed.");
  70. }
  71. //Example of failure
  72. Map<String, Object> badData = new HashMap<>();
  73. badData.put("userId", 123);
  74. badData.put("username", "JohnDoe");
  75. badData.put("email", 123);
  76. Map<String, Object> normalizedBadData = normalizeUserData(badData);
  77. if (normalizedBadData == null) {
  78. System.out.println("Bad data normalization failed as expected");
  79. }
  80. }
  81. }

Add your comment