@Override public IntegerFreqMap _apply(final IntegerFreqMap a, final IntegerFreqMap b) { for (final Entry<String, Integer> 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 Integer value) { final Integer currentValue = get(key); if (null == currentValue) { put(key, value); } else { put(key, currentValue + value); } }
@Test public void canSerialiseDeSerialiseFreqMapWithValues() throws SerialisationException { IntegerFreqMap freqMap = new IntegerFreqMap(); freqMap.put("x", 10); freqMap.put("y", 5); freqMap.put("z", 20); byte[] b = serialiser.serialise(freqMap); IntegerFreqMap o = (IntegerFreqMap) serialiser.deserialise(b); assertEquals(IntegerFreqMap.class, o.getClass()); assertEquals((Integer) 10, o.get("x")); assertEquals((Integer) 5, o.get("y")); assertEquals((Integer) 20, o.get("z")); }
@Test public void testSerialiserWillSkipEntryWithNullValue() throws SerialisationException { IntegerFreqMap freqMap = new IntegerFreqMap(); freqMap.put("x", null); freqMap.put("y", 5); freqMap.put("z", 20); byte[] b = serialiser.serialise(freqMap); IntegerFreqMap o = (IntegerFreqMap) serialiser.deserialise(b); assertEquals(IntegerFreqMap.class, o.getClass()); assertNull(o.get("x")); assertEquals((Integer) 5, o.get("y")); assertEquals((Integer) 20, o.get("z")); }
@Test public void shouldMergeFreqMaps() { // Given final IntegerFreqMapAggregator aggregator = new IntegerFreqMapAggregator(); final IntegerFreqMap freqMap1 = new IntegerFreqMap(); freqMap1.put("1", 2); freqMap1.put("2", 3); final IntegerFreqMap freqMap2 = new IntegerFreqMap(); freqMap2.put("2", 4); freqMap2.put("3", 5); // When final IntegerFreqMap result = aggregator.apply(freqMap1, freqMap2); // Then assertEquals((Integer) 2, result.get("1")); assertEquals((Integer) 7, result.get("2")); assertEquals((Integer) 5, result.get("3")); }
@Override public IntegerFreqMap _apply(final IntegerFreqMap a, final IntegerFreqMap b) { for (final Entry<String, Integer> 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 Integer value) { final Integer currentValue = get(key); if (null == currentValue) { put(key, value); } else { put(key, currentValue + value); } }