@Test public void shouldStoreTheValueReturnedByTheMapperInTheMapIfTheKeyDoesntExist() { Map<Integer, String> input = Literals.mapBuilderWithKeyValuePair(1, "one") .andKeyValuePair(2, "two") .andKeyValuePair(3, "three") .build(HashMap.class); Maps.getOrAdd(input, 12, new Mapper<Integer, String>() { @Override public String map(Integer integer) { return integer < 10 ? "lower value" : "higher value"; } }); assertThat(input.get(12), is("higher value")); }
public static <U, V> V getOrAdd(Map<U, V> map, U key, UnaryFunction<? super U, ? extends V> mapper) { if (map.containsKey(key)) { return map.get(key); } else { V newValue = mapper.call(key); map.put(key, newValue); return newValue; } }
Maps { public static <U, V> V getOrAdd(Map<U, V> map, U key, UnaryFunction<? super U, ? extends V> mapper) { if (map.containsKey(key)) { return map.get(key); } else { V newValue = mapper.call(key); map.put(key, newValue); return newValue; } } }
Maps { public static <U, V> V getOrAdd(Map<U, V> map, U key, UnaryFunction<? super U, ? extends V> mapper) { if (map.containsKey(key)) { return map.get(key); } else { V newValue = mapper.call(key); map.put(key, newValue); return newValue; } } private  Maps(); }
Maps { public static <U, V> V getOrAdd(Map<U, V> map, U key, UnaryFunction<? super U, ? extends V> mapper) { if (map.containsKey(key)) { return map.get(key); } else { V newValue = mapper.call(key); map.put(key, newValue); return newValue; } } private  Maps(); static V getOrAdd(Map<U, V> map, U key, UnaryFunction<? super U, ? extends V> mapper); static V getOrAdd(Map<U, V> map, U key, final NullaryFunction<? extends V> factory); static Mapper<? super Map<K, V>, Set<Map.Entry<K, V>>> toEntrySet(); }
Maps { public static <U, V> V getOrAdd(Map<U, V> map, U key, UnaryFunction<? super U, ? extends V> mapper) { if (map.containsKey(key)) { return map.get(key); } else { V newValue = mapper.call(key); map.put(key, newValue); return newValue; } } private  Maps(); static V getOrAdd(Map<U, V> map, U key, UnaryFunction<? super U, ? extends V> mapper); static V getOrAdd(Map<U, V> map, U key, final NullaryFunction<? extends V> factory); static Mapper<? super Map<K, V>, Set<Map.Entry<K, V>>> toEntrySet();  }