protected Map<String, Map> mergeMaps(Map<String, Map> configuration, Map<String, Map> servicesConfig) { if (servicesConfig == null) { return configuration; } MutableMap<String, Map> newConfigurationMap = MutableMap.copyOf(configuration); for (Map.Entry<String, Map> stringMapEntry : servicesConfig.entrySet()) { if(!newConfigurationMap.containsKey(stringMapEntry.getKey())) { newConfigurationMap.put(stringMapEntry.getKey(), stringMapEntry.getValue()); } else { if(stringMapEntry.getValue() != null) { // Ensuring that the configuration passed via yaml gets precedence over the one provided by the external service MutableMap<String, Map> tmpMap = MutableMap.copyOf(stringMapEntry.getValue()); tmpMap.putAll(newConfigurationMap.get(stringMapEntry.getKey())); newConfigurationMap.put(stringMapEntry.getKey(), ImmutableMap.copyOf(tmpMap)); } } } return newConfigurationMap; }
public Maybe<V> getMaybe(K key) { if (containsKey(key)) return Maybe.of(get(key)); return Maybe.absent("No entry for key '"+key+"' in this map"); }
@SuppressWarnings("rawtypes") @Test public void testCreateMapInList1() { MutableMap<Object, Object> map = Jsonya.at("countries").list().map().add("europe", "uk").getRootMap(); List l = (List)map.get("countries"); Assert.assertEquals( ((Map)l.get(0)).get("europe"), "uk" ); } @SuppressWarnings("rawtypes")
/** convenience for converting an object x to something which consists only of json primitives, doing * {@link #toString()} on anything which is not recognised. see {@link JsonPrimitiveDeepTranslator} */ public static Object convertToJsonPrimitive(Object x) { if (x==null) return null; if (x instanceof Map) return newInstancePrimitive().put((Map<?,?>)x).getRootMap(); return newInstancePrimitive().put("data", x).getRootMap().get("data"); }
@SuppressWarnings("rawtypes") @Test public void testCreateMapInList2() { MutableMap<Object, Object> map = Jsonya.at("countries").list().map().add("europe", "uk") .root().at("countries").add("antarctica") .root().at("countries").map().add("asia", (Object)null) .at("asia").list().add("china", "japan").getRootMap(); List l = (List)map.get("countries"); Assert.assertEquals( ((Map)l.get(0)).get("europe"), "uk" ); }