1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class MetadataAnomalyChecker {
  4. /**
  5. * Checks metadata for anomalies based on default values.
  6. *
  7. * @param metadata A map containing metadata values. Key is the metadata field name,
  8. * value is the metadata value.
  9. * @param defaultValues A map containing default values for each metadata field.
  10. * @return A map of anomalous metadata fields. Key is the field name, value is the
  11. * anomalous value. Returns an empty map if no anomalies are found.
  12. */
  13. public static Map<String, Object> checkForAnomalies(Map<String, Object> metadata, Map<String, Object> defaultValues) {
  14. Map<String, Object> anomalies = new HashMap<>();
  15. if (metadata == null || defaultValues == null) {
  16. return anomalies; // Return empty map for null inputs
  17. }
  18. for (Map.Entry<String, Object> entry : metadata.entrySet()) {
  19. String fieldName = entry.getKey();
  20. Object metadataValue = entry.getValue();
  21. if (defaultValues.containsKey(fieldName)) {
  22. Object defaultValue = defaultValues.get(fieldName);
  23. if (!metadataValue.equals(defaultValue)) {
  24. anomalies.put(fieldName, metadataValue);
  25. }
  26. }
  27. }
  28. return anomalies;
  29. }
  30. public static void main(String[] args) {
  31. // Example Usage
  32. Map<String, Object> metadata = new HashMap<>();
  33. metadata.put("status", "active");
  34. metadata.put("owner", "john.doe");
  35. metadata.put("version", 1.2);
  36. Map<String, Object> defaultValues = new HashMap<>();
  37. defaultValues.put("status", "inactive");
  38. defaultValues.put("owner", "default_user");
  39. defaultValues.put("version", 1.0);
  40. Map<String, Object> anomalies = checkForAnomalies(metadata, defaultValues);
  41. if (!anomalies.isEmpty()) {
  42. System.out.println("Anomalies found:");
  43. for (Map.Entry<String, Object> entry : anomalies.entrySet()) {
  44. System.out.println(entry.getKey() + ": " + entry.getValue());
  45. }
  46. } else {
  47. System.out.println("No anomalies found.");
  48. }
  49. }
  50. }

Add your comment