/** * 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); } }
@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; } }
@Override @SuppressWarnings("unchecked") public Pair<IntegerFreqMap, byte[]>[] getHistoricSerialisationPairs() { IntegerFreqMap freqMap = new IntegerFreqMap(); freqMap.put("x", 10); freqMap.put("y", 5); freqMap.put("z", 20); return new Pair[]{ new Pair(freqMap, new byte[]{120, 92, 44, 49, 48, 92, 44, 121, 92, 44, 53, 92, 44, 122, 92, 44, 50, 48}) }; }
@Override public IntegerFreqMap deserialise(final byte[] bytes) throws SerialisationException { IntegerFreqMap freqMap = new IntegerFreqMap(); if (bytes.length == 0) { return freqMap; } String stringMap; try { stringMap = new String(bytes, CommonConstants.ISO_8859_1_ENCODING); } catch (final UnsupportedEncodingException e) { throw new SerialisationException(e.getMessage(), e); } if (stringMap.isEmpty()) { //No values so return the empty map return freqMap; } String[] keyValues = stringMap.split(SEPERATOR_REGEX); if (keyValues.length % 2 != 0) { throw new SerialisationException("Uneven number of entries found for serialised frequency map, unable to deserialise."); } for (int i = 0; i < keyValues.length - 1; i += 2) { freqMap.put(keyValues[i], Integer.parseInt(keyValues[i + 1])); } return freqMap; }
@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")); }
@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")); }
/** * 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); } }
@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; } }
@Override public IntegerFreqMap deserialise(final byte[] bytes) throws SerialisationException { IntegerFreqMap freqMap = new IntegerFreqMap(); if (bytes.length == 0) { return freqMap; } String stringMap; try { stringMap = new String(bytes, CommonConstants.ISO_8859_1_ENCODING); } catch (final UnsupportedEncodingException e) { throw new SerialisationException(e.getMessage(), e); } if (stringMap.isEmpty()) { //No values so return the empty map return freqMap; } String[] keyValues = stringMap.split(SEPERATOR_REGEX); if (keyValues.length % 2 != 0) { throw new SerialisationException("Uneven number of entries found for serialised frequency map, unable to deserialise."); } for (int i = 0; i < keyValues.length - 1; i += 2) { freqMap.put(keyValues[i], Integer.parseInt(keyValues[i + 1])); } return freqMap; }