@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 _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; } }
/** * Increments the value of an existing key by 1. * If the key doesn't exist, initialises the value to 1. * * @param key The key to increment or insert. */ public void upsert(final String key) { upsert(key, 1); } }
/** * 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 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 canSerialiseEmptyFreqMap() throws SerialisationException { byte[] b = serialiser.serialise(new IntegerFreqMap()); Object o = serialiser.deserialise(b); assertEquals(IntegerFreqMap.class, o.getClass()); assertEquals(0, ((IntegerFreqMap) o).size()); }
@Override public IntegerFreqMap deserialiseEmpty() throws SerialisationException { return new IntegerFreqMap(); }
@Override public byte[] serialise(final IntegerFreqMap map) throws SerialisationException { Set<Entry<String, Integer>> entrySet = map.entrySet(); StringBuilder builder = new StringBuilder(); int last = entrySet.size() - 1; int start = 0; for (final Entry<String, Integer> entry : entrySet) { Integer value = entry.getValue(); if (null == value) { continue; } builder.append(entry.getKey() + SEPERATOR + value); ++start; if (start > last) { break; } builder.append(SEPERATOR); } try { return builder.toString() .getBytes(CommonConstants.ISO_8859_1_ENCODING); } catch (final UnsupportedEncodingException e) { throw new SerialisationException(e.getMessage(), e); } }
@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")); }
/** * 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 void shouldDeserialiseEmpty() throws SerialisationException { assertEquals(new IntegerFreqMap(), serialiser.deserialiseEmpty()); } }
@Override public byte[] serialise(final IntegerFreqMap map) throws SerialisationException { Set<Entry<String, Integer>> entrySet = map.entrySet(); StringBuilder builder = new StringBuilder(); int last = entrySet.size() - 1; int start = 0; for (final Entry<String, Integer> entry : entrySet) { Integer value = entry.getValue(); if (null == value) { continue; } builder.append(entry.getKey() + SEPERATOR + value); ++start; if (start > last) { break; } builder.append(SEPERATOR); } try { return builder.toString() .getBytes(CommonConstants.ISO_8859_1_ENCODING); } catch (final UnsupportedEncodingException e) { throw new SerialisationException(e.getMessage(), e); } }
@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")); }
@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; }
@Override public IntegerFreqMap deserialiseEmpty() throws SerialisationException { return new IntegerFreqMap(); }
/** * Increments the value of an existing key by 1. * If the key doesn't exist, initialises the value to 1. * * @param key The key to increment or insert. */ public void upsert(final String key) { upsert(key, 1); } }
@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; }