@Override public Long apply(final FreqMap freqMap) { if (null != freqMap) { return freqMap.get(key); } return null; }
@Override protected FreqMap _apply(final FreqMap a, final FreqMap b) { for (final Entry<String, Long> entry : b.entrySet()) { if (a.containsKey(entry.getKey())) { a.put(entry.getKey(), a.get(entry.getKey()) + entry.getValue()); } else { a.put(entry.getKey(), entry.getValue()); } } return a; } }
/** * 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 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 testUpsertOverloadedIncrementsDefaultValue() { //given final String key = "test"; final Long initialValue = 57L; final Long expected = 58L; freqMap.upsert(key, initialValue); //when freqMap.upsert(key); //then assertEquals(freqMap.get(key), expected); }
@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 testUpsertOverloadedCreateDefaultValue() { //given final String key = "test"; final Long expected = 1L; //when freqMap.upsert(key); //then assertTrue(freqMap.containsKey(key)); assertEquals(freqMap.get(key), expected); }
@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 testUpsertCreatesNewKeyValue() { //given final String key = "test"; final Long value = 6L; //when freqMap.upsert(key, value); //then assertTrue(freqMap.containsKey(key)); assertEquals(value, freqMap.get(key)); }
@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); } }
@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")); }
@Override public Long apply(final FreqMap freqMap) { if (null != freqMap) { return freqMap.get(key); } return null; }
@Override protected FreqMap _apply(final FreqMap a, final FreqMap b) { for (final Entry<String, Long> entry : b.entrySet()) { if (a.containsKey(entry.getKey())) { a.put(entry.getKey(), a.get(entry.getKey()) + entry.getValue()); } else { a.put(entry.getKey(), entry.getValue()); } } return a; } }
/** * 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); } }