/** * Adds a new key and value to the map if the key is not already there. * If the key is already there, the value supplied is added to the existing value for the key and the result is inserted into the map. * * @param key The key in the map to increment or insert. * @param value The value to increment by or initialise to. */ public void upsert(final String key, final Long value) { final Long currentValue = get(key); if (null == currentValue) { put(key, value); } else { put(key, currentValue + value); } }
@Test public void shouldMergeFreqMaps() { // Given final FreqMapAggregator aggregator = new FreqMapAggregator(); final FreqMap freqMap1 = new FreqMap(); freqMap1.put("1", 2L); freqMap1.put("2", 3L); final FreqMap freqMap2 = new FreqMap(); freqMap2.put("2", 4L); freqMap2.put("3", 5L); // When final FreqMap result = aggregator.apply(freqMap1, freqMap2); // Then assertEquals((Long) 2L, result.get("1")); assertEquals((Long) 7L, result.get("2")); assertEquals((Long) 5L, result.get("3")); }
@Test public void shouldSkipEntryWithNullKey() throws SerialisationException { // Given final FreqMap freqMap = new FreqMap(); freqMap.put(null, 10L); freqMap.put("y", 5L); freqMap.put("z", 20L); // When final byte[] serialised = serialiser.serialise(freqMap); final FreqMap deserialised = serialiser.deserialise(serialised); assertFalse(deserialised.containsKey("x")); assertEquals((Long) 5L, deserialised.get("y")); assertEquals((Long) 20L, deserialised.get("z")); }
@Test public void shouldSkipEntryWithNullValues() throws SerialisationException { // Given final FreqMap freqMap = new FreqMap(); freqMap.put("v", null); freqMap.put("w", 5L); freqMap.put("x", null); freqMap.put("y", 20L); freqMap.put("z", null); // When final byte[] serialised = serialiser.serialise(freqMap); final FreqMap deserialised = serialiser.deserialise(serialised); assertFalse(deserialised.containsKey("v")); assertEquals((Long) 5L, deserialised.get("w")); assertFalse(deserialised.containsKey("x")); assertEquals((Long) 20L, deserialised.get("y")); assertFalse(deserialised.containsKey("z")); }
@Test public void shouldSerialiseDeserialiseFreqMapWithAnEmptyKey() throws SerialisationException { // Given final FreqMap freqMap = new FreqMap(); freqMap.put("", 10L); freqMap.put("y", 5L); freqMap.put("z", 20L); // When final byte[] serialised = serialiser.serialise(freqMap); final FreqMap deserialised = serialiser.deserialise(serialised); assertEquals((Long) 10L, deserialised.get("")); assertEquals((Long) 5L, deserialised.get("y")); assertEquals((Long) 20L, deserialised.get("z")); }
@Test public void shouldSerialiseDeserialiseFreqMapWithValues() throws SerialisationException { // Given final FreqMap freqMap = new FreqMap(); freqMap.put("x", 10L); freqMap.put("y", 5L); freqMap.put("z", 20L); // When final byte[] serialised = serialiser.serialise(freqMap); final FreqMap deserialised = serialiser.deserialise(serialised); // Then assertEquals((Long) 10L, deserialised.get("x")); assertEquals((Long) 5L, deserialised.get("y")); assertEquals((Long) 20L, deserialised.get("z")); }
@Test public void testUpsertUpdatesExistingKeyValue() { //given final String key = "test"; final Long initialValue = 3L; final Long increment = 11L; final Long expected = 14L; freqMap.put(key, initialValue); //when freqMap.upsert(key, increment); //then assertEquals(freqMap.get(key), expected); }
@Test public void testKeyExistsButValueNullIsHandled() { //given final String key = "test"; freqMap.put(key, null); final Long initialValue = 7L; final Long expectedValue = 8L; //when freqMap.upsert(key, 7L); freqMap.upsert(key); //then assertEquals(freqMap.get(key), expectedValue); } }
/** * Adds a new key and value to the map if the key is not already there. * If the key is already there, the value supplied is added to the existing value for the key and the result is inserted into the map. * * @param key The key in the map to increment or insert. * @param value The value to increment by or initialise to. */ public void upsert(final String key, final Long value) { final Long currentValue = get(key); if (null == currentValue) { put(key, value); } else { put(key, currentValue + value); } }
public void freqMapIsMoreThan2() { // --------------------------------------------------------- final MapFilter function = new MapFilter("key1", new IsMoreThan(2L)); // --------------------------------------------------------- final FreqMap map1 = new FreqMap(); map1.put("key1", 1L); final FreqMap map2 = new FreqMap(); map2.put("key1", 2L); final FreqMap map3 = new FreqMap(); map3.put("key1", 3L); final FreqMap map4 = new FreqMap(); map4.put("key1", 3L); map4.put("key2", 0L); final FreqMap map5 = new FreqMap(); map5.put("key2", 3L); runExample(function, map1, map2, map3, map4, map5); }
public void freqMapIsMoreThanOrEqualTo2() { // --------------------------------------------------------- final MapFilter function = new MapFilter("key1", new IsMoreThan(2L, true)); // --------------------------------------------------------- final FreqMap map1 = new FreqMap(); map1.put("key1", 1L); final FreqMap map2 = new FreqMap(); map2.put("key1", 2L); final FreqMap map3 = new FreqMap(); map3.put("key1", 3L); final FreqMap map4 = new FreqMap(); map4.put("key1", 3L); map4.put("key2", 0L); final FreqMap map5 = new FreqMap(); map5.put("key2", 3L); runExample(function, map1, map2, map3, map4, map5); }
public void freqMapIsMoreThan2() { // --------------------------------------------------------- final PredicateMap function = new PredicateMap("key1", new IsMoreThan(2L)); // --------------------------------------------------------- final FreqMap map1 = new FreqMap(); map1.put("key1", 1L); final FreqMap map2 = new FreqMap(); map2.put("key1", 2L); final FreqMap map3 = new FreqMap(); map3.put("key1", 3L); final FreqMap map4 = new FreqMap(); map4.put("key1", 3L); map4.put("key2", 0L); final FreqMap map5 = new FreqMap(); map5.put("key2", 3L); runExample(function, null, map1, map2, map3, map4, map5); }
public void freqMapIsMoreThanOrEqualTo2() { // --------------------------------------------------------- final PredicateMap function = new PredicateMap("key1", new IsMoreThan(2L, true)); // --------------------------------------------------------- final FreqMap map1 = new FreqMap(); map1.put("key1", 1L); final FreqMap map2 = new FreqMap(); map2.put("key1", 2L); final FreqMap map3 = new FreqMap(); map3.put("key1", 3L); final FreqMap map4 = new FreqMap(); map4.put("key1", 3L); map4.put("key2", 0L); final FreqMap map5 = new FreqMap(); map5.put("key2", 3L); runExample(function, null, map1, map2, map3, map4, map5); }