|
| 1 | +package com.javaexperiments; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Optional; |
| 6 | + |
| 7 | +/** |
| 8 | + * Utility class for safely accessing nested Map structures. |
| 9 | + * Ideal for processing dynamic data such as JSON payloads. |
| 10 | + */ |
| 11 | +public class NestedMapExtractor { |
| 12 | + |
| 13 | + /** |
| 14 | + * Safely extract a nested map from a parent map. |
| 15 | + * |
| 16 | + * @param map the parent map |
| 17 | + * @param key the key whose value should be a nested map |
| 18 | + * @return the nested map if present and valid, otherwise an empty map |
| 19 | + */ |
| 20 | + public static Map<String, Object> get(Map<?, ?> map, String key) { |
| 21 | + if (map == null) return new HashMap<>(); |
| 22 | + |
| 23 | + return Optional.ofNullable(key) |
| 24 | + .map(map::get) |
| 25 | + .filter(Map.class::isInstance) |
| 26 | + .map(value -> { |
| 27 | + @SuppressWarnings("unchecked") |
| 28 | + Map<String, Object> nested = (Map<String, Object>) value; |
| 29 | + return nested; |
| 30 | + }) |
| 31 | + .orElse(new HashMap<>()); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Safely extract a value of a specific type from a map. |
| 36 | + * |
| 37 | + * @param map the map to extract from |
| 38 | + * @param key the key to look up |
| 39 | + * @param clazz the expected type class |
| 40 | + * @param <T> the type of the value |
| 41 | + * @return the value if present and of the correct type, otherwise null |
| 42 | + */ |
| 43 | + public static <T> T get(Map<String, Object> map, String key, Class<T> clazz) { |
| 44 | + if (map == null || key == null || clazz == null) return null; |
| 45 | + |
| 46 | + Object value = map.get(key); |
| 47 | + if (clazz.isInstance(value)) { |
| 48 | + return clazz.cast(value); |
| 49 | + } |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Retrieves a String value from a nested "Something" map within the payload. |
| 55 | + * |
| 56 | + * @param payload the top-level payload map |
| 57 | + * @param key the key inside the "Something" context |
| 58 | + * @return the string value, or null if missing or not a string |
| 59 | + */ |
| 60 | + public static String getFromContext(Map<String, Object> payload, String key) { |
| 61 | + Map<String, Object> context = get(payload, "Something"); |
| 62 | + return get(context, key, String.class); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Demonstrates usage of the utility methods with various scenarios. |
| 67 | + */ |
| 68 | + public static void main(String[] args) { |
| 69 | + Map<String, Object> userDetails = new HashMap<>(); |
| 70 | + userDetails.put("username", "bruce.wayne"); |
| 71 | + userDetails.put("email", "brucewayne@google.com"); |
| 72 | + userDetails.put("accessLevel", 2); |
| 73 | + |
| 74 | + Map<String, Object> payload = new HashMap<>(); |
| 75 | + payload.put("Something", userDetails); |
| 76 | + |
| 77 | + // Example 1: Expected string |
| 78 | + String username = getFromContext(payload, "username"); |
| 79 | + System.out.println("Username: " + username); // bruce.wayne |
| 80 | + |
| 81 | + // Example 2: Type mismatch |
| 82 | + String accessLevel = getFromContext(payload, "accessLevel"); |
| 83 | + System.out.println("AccessLevel (as String): " + accessLevel); // null |
| 84 | + |
| 85 | + // Example 3: Missing key |
| 86 | + String nonExistent = getFromContext(payload, "nonExistentKey"); |
| 87 | + System.out.println("Non-existent key: " + nonExistent); // null |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments